-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDropboxAPI.module
More file actions
192 lines (153 loc) · 5.41 KB
/
DropboxAPI.module
File metadata and controls
192 lines (153 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php namespace ProcessWire;
/**
* DropboxAPI
*
* Copyright 2017:
*
* Clip Magic - Prue Rowland
* Web: www.clipmagic.com.au
* Email: admin@clipmagic.com.au
*
* ProcessWire 3.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
use Kunnu\Dropbox\Exceptions;
class DropboxAPI extends WireData implements Module, ConfigurableModule
{
public function __construct()
{
// set our default values before ProcessWire configures the module
//OAuth
$this->app_key = '';
$this->app_secret = '';
$this->authorization_code = '';
$this->callback_url = '';
require_once dirname(__FILE__) . '/dropbox-php-sdk/vendor/autoload.php';
}
#---------------------
# Module init
#---------------------
/**
*
*/
public function ___init()
{
//Configure Dropbox Application
$this->app = new \Kunnu\Dropbox\DropboxApp($this->app_key, $this->app_secret, $this->authorization_code);
//Configure Dropbox service
$this->dropbox = new \Kunnu\Dropbox\Dropbox($this->app);
//DropboxAuthHelper
$this->authHelper = $this->dropbox->getAuthHelper();
return $this;
}
public function ___ready()
{
}
#---------------------
# Interface methods
#---------------------
static public function getModuleInfo()
{
return array(
'title' => 'Dropbox PHP API v2 (Unofficial) by Kunal Varma',
'author' => 'Clip magic',
'version' => '0.0.2',
'summary' => 'ProcessWire module wrapper for Dropbox v2 PHP API. All kudos to Kunal Varma. See https://github.com/kunalvarma05/dropbox-php-sdk for more info.',
'singular' => true,
'autoload' => false
);
}
public static function getModuleConfigInputfields(array $data)
{
$fields = new InputfieldWrapper();
// OAuth fieldset
$fieldset = wire('modules')->get('InputfieldFieldset');
$fieldset->label = 'OAuth Login';
// ask for the client_id
$field = wire('modules')->get('InputfieldText');
$field->name = 'app_key';
$field->label = "Enter your App key";
if (isset($data['app_key']))
$field->value = $data['app_key'];
$fieldset->add($field);
// ask for the app secret
$field = wire('modules')->get('InputfieldText');
$field->name = 'app_secret';
$field->collapsed = collapsedYes;
$field->label = "Enter your app secret";
if (isset($data['app_secret']))
$field->value = $data['app_secret'];
$fieldset->add($field);
// ask for the authorization code
$field = wire('modules')->get('InputfieldText');
$field->name = 'authorization_code';
$field->collapsed = collapsedYes;
$field->label = "Enter your authorization code";
if (isset($data['authorization_code']))
$field->value = $data['authorization_code'];
$fieldset->add($field);
// ask for the callback url
$field = wire('modules')->get('InputfieldURL');
$field->name = 'callback_url';
$field->label = "Callback URL";
$field->description = __("Specify the full URL to the callback page");
if (isset($data['callback_url']))
$field->value = $data['callback_url'];
$fieldset->add($field);
$fieldset->add($field);
$fields->add($fieldset);
return $fields;
}
/***************************************************************************
* Calls to the API
***************************************************************************/
public static function api($function_name, $args = null)
{
if (!empty($args) && is_array($args))
$args = implode(",", $args);
$out = call_user_func($function_name, $args);
return $out;
}
/**
* Upload a File to Dropbox
*
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file
* @param string $pathFrom Path to local file to be uploaded in file name
* @param string $pathTo Path to Dropbox folder inc file name save file to
* @param array $params Additional Params
*
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
*
* @return \Kunnu\Dropbox\Models\FileMetadata or string, either
* - (object) file metadata, eg to display name: $myFileMetadataObj->getName());
* - (string) "in_progress"
* - (string) "failed"
*/
public function upload($pathFrom, $pathTo, $options)
{
if (!$pathFrom)
throw new WireException (__('No originating file path specified'));
if (!$pathTo)
throw new WireException (__('No destination Dropbox file path specified'));
$result = $this->_upload($pathFrom, $pathTo, $options);
return $result;
}
private function _upload($pathFrom, $pathTo, $options)
{
$dropboxFile = new DropboxFile\DropboxFile($pathFrom);
$result = $this->dropbox->upload($dropboxFile, $pathTo, $options);
return $result;
}
public function getDropBoxFile($filePath, $mode)
{
$result = new DropboxFile($filePath, $mode);
return $result;
}
}