1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunnu\Dropbox; |
4
|
|
|
|
5
|
|
|
use Kunnu\Dropbox\Exceptions\DropboxClientUnableToOpenTempFileException; |
6
|
|
|
use Kunnu\Dropbox\Exceptions\DropboxClientUnableToWriteToTempException; |
7
|
|
|
use Kunnu\Dropbox\Models\DeletedMetadata; |
8
|
|
|
use Kunnu\Dropbox\Models\File; |
9
|
|
|
use Kunnu\Dropbox\Models\Account; |
10
|
|
|
use Kunnu\Dropbox\Models\Thumbnail; |
11
|
|
|
use Kunnu\Dropbox\Models\AccountList; |
12
|
|
|
use Kunnu\Dropbox\Models\ModelFactory; |
13
|
|
|
use Kunnu\Dropbox\Models\FileMetadata; |
14
|
|
|
use Kunnu\Dropbox\Models\CopyReference; |
15
|
|
|
use Kunnu\Dropbox\Models\FolderMetadata; |
16
|
|
|
use Kunnu\Dropbox\Models\ModelCollection; |
17
|
|
|
use Kunnu\Dropbox\Authentication\OAuth2Client; |
18
|
|
|
use Kunnu\Dropbox\Store\PersistentDataStoreFactory; |
19
|
|
|
use Kunnu\Dropbox\Authentication\DropboxAuthHelper; |
20
|
|
|
use Kunnu\Dropbox\Exceptions\DropboxClientException; |
21
|
|
|
use Kunnu\Dropbox\Security\RandomStringGeneratorFactory; |
22
|
|
|
use Kunnu\Dropbox\Http\Clients\DropboxHttpClientFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Dropbox |
26
|
|
|
*/ |
27
|
|
|
class Dropbox |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Uploading a file with the 'uploadFile' method, with the file's |
31
|
|
|
* size less than this value (~8 MB), the simple `upload` method will be |
32
|
|
|
* used, if the file size exceed this value (~8 MB), the `startUploadSession`, |
33
|
|
|
* `appendUploadSession` & `finishUploadSession` methods will be used |
34
|
|
|
* to upload the file in chunks. |
35
|
|
|
* |
36
|
|
|
* @const int |
37
|
|
|
*/ |
38
|
|
|
const AUTO_CHUNKED_UPLOAD_THRESHOLD = 8000000; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The Chunk Size the file will be |
42
|
|
|
* split into and uploaded (~4 MB) |
43
|
|
|
* |
44
|
|
|
* @const int |
45
|
|
|
*/ |
46
|
|
|
const DEFAULT_CHUNK_SIZE = 4000000; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Response header containing file metadata |
50
|
|
|
* |
51
|
|
|
* @const string |
52
|
|
|
*/ |
53
|
|
|
const METADATA_HEADER = 'dropbox-api-result'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Prefix for writable temporary file |
57
|
|
|
* |
58
|
|
|
* @const string |
59
|
|
|
*/ |
60
|
|
|
const TMP_PREFIX = 'dropbox-temp-file'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The Dropbox App |
64
|
|
|
* |
65
|
|
|
* @var \Kunnu\Dropbox\DropboxApp |
66
|
|
|
*/ |
67
|
|
|
protected $app; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* OAuth2 Access Token |
71
|
|
|
* |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
protected $accessToken; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Dropbox Client |
78
|
|
|
* |
79
|
|
|
* @var \Kunnu\Dropbox\DropboxClient |
80
|
|
|
*/ |
81
|
|
|
protected $client; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* OAuth2 Client |
85
|
|
|
* |
86
|
|
|
* @var \Kunnu\Dropbox\Authentication\OAuth2Client |
87
|
|
|
*/ |
88
|
|
|
protected $oAuth2Client; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Random String Generator |
92
|
|
|
* |
93
|
|
|
* @var \Kunnu\Dropbox\Security\RandomStringGeneratorInterface |
94
|
|
|
*/ |
95
|
|
|
protected $randomStringGenerator; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Persistent Data Store |
99
|
|
|
* |
100
|
|
|
* @var \Kunnu\Dropbox\Store\PersistentDataStoreInterface |
101
|
|
|
*/ |
102
|
|
|
protected $persistentDataStore; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create a new Dropbox instance |
106
|
|
|
* |
107
|
|
|
* @param \Kunnu\Dropbox\DropboxApp |
108
|
|
|
* @param array $config Configuration Array |
109
|
|
|
*/ |
110
|
|
|
public function __construct(DropboxApp $app, array $config = []) |
111
|
|
|
{ |
112
|
|
|
//Configuration |
113
|
|
|
$config = array_merge([ |
114
|
|
|
'http_client_handler' => null, |
115
|
|
|
'random_string_generator' => null, |
116
|
|
|
'persistent_data_store' => null |
117
|
|
|
], $config); |
118
|
|
|
|
119
|
|
|
//Set the app |
120
|
|
|
$this->app = $app; |
121
|
|
|
|
122
|
|
|
//Set the access token |
123
|
|
|
$this->setAccessToken($app->getAccessToken()); |
124
|
|
|
|
125
|
|
|
//Make the HTTP Client |
126
|
|
|
$httpClient = DropboxHttpClientFactory::make($config['http_client_handler']); |
127
|
|
|
|
128
|
|
|
//Make and Set the DropboxClient |
129
|
|
|
$this->client = new DropboxClient($httpClient); |
130
|
|
|
|
131
|
|
|
//Make and Set the Random String Generator |
132
|
|
|
$this->randomStringGenerator = RandomStringGeneratorFactory::makeRandomStringGenerator($config['random_string_generator']); |
133
|
|
|
|
134
|
|
|
//Make and Set the Persistent Data Store |
135
|
|
|
$this->persistentDataStore = PersistentDataStoreFactory::makePersistentDataStore($config['persistent_data_store']); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get Dropbox Auth Helper |
140
|
|
|
* |
141
|
|
|
* @return \Kunnu\Dropbox\Authentication\DropboxAuthHelper |
142
|
|
|
*/ |
143
|
|
|
public function getAuthHelper() |
144
|
|
|
{ |
145
|
|
|
return new DropboxAuthHelper( |
146
|
|
|
$this->getOAuth2Client(), |
147
|
|
|
$this->getRandomStringGenerator(), |
148
|
|
|
$this->getPersistentDataStore() |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get OAuth2Client |
154
|
|
|
* |
155
|
|
|
* @return \Kunnu\Dropbox\Authentication\OAuth2Client |
156
|
|
|
*/ |
157
|
|
|
public function getOAuth2Client() |
158
|
|
|
{ |
159
|
|
|
if (!$this->oAuth2Client instanceof OAuth2Client) { |
160
|
|
|
return new OAuth2Client( |
161
|
|
|
$this->getApp(), |
162
|
|
|
$this->getClient(), |
163
|
|
|
$this->getRandomStringGenerator() |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->oAuth2Client; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the Dropbox App. |
172
|
|
|
* |
173
|
|
|
* @return \Kunnu\Dropbox\DropboxApp Dropbox App |
174
|
|
|
*/ |
175
|
|
|
public function getApp() |
176
|
|
|
{ |
177
|
|
|
return $this->app; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the Client |
182
|
|
|
* |
183
|
|
|
* @return \Kunnu\Dropbox\DropboxClient |
184
|
|
|
*/ |
185
|
|
|
public function getClient() |
186
|
|
|
{ |
187
|
|
|
return $this->client; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the Random String Generator |
192
|
|
|
* |
193
|
|
|
* @return \Kunnu\Dropbox\Security\RandomStringGeneratorInterface |
194
|
|
|
*/ |
195
|
|
|
public function getRandomStringGenerator() |
196
|
|
|
{ |
197
|
|
|
return $this->randomStringGenerator; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get Persistent Data Store |
202
|
|
|
* |
203
|
|
|
* @return \Kunnu\Dropbox\Store\PersistentDataStoreInterface |
204
|
|
|
*/ |
205
|
|
|
public function getPersistentDataStore() |
206
|
|
|
{ |
207
|
|
|
return $this->persistentDataStore; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the Metadata for a file or folder |
212
|
|
|
* |
213
|
|
|
* @param string $path Path of the file or folder |
214
|
|
|
* @param array $params Additional Params |
215
|
|
|
* |
216
|
|
|
* @return \Kunnu\Dropbox\Models\FileMetadata | \Kunnu\Dropbox\Models\FolderMetadata |
217
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
218
|
|
|
* |
219
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
220
|
|
|
* |
221
|
|
|
*/ |
222
|
|
|
public function getMetadata($path, array $params = []) |
223
|
|
|
{ |
224
|
|
|
//Root folder is unsupported |
225
|
|
|
if ($path === '/') { |
226
|
|
|
throw new DropboxClientException("Metadata for the root folder is unsupported."); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
//Set the path |
230
|
|
|
$params['path'] = $path; |
231
|
|
|
|
232
|
|
|
//Get File Metadata |
233
|
|
|
$response = $this->postToAPI('/files/get_metadata', $params); |
234
|
|
|
|
235
|
|
|
//Make and Return the Model |
236
|
|
|
return $this->makeModelFromResponse($response); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Make a HTTP POST Request to the API endpoint type |
241
|
|
|
* |
242
|
|
|
* @param string $endpoint API Endpoint to send Request to |
243
|
|
|
* @param array $params Request Query Params |
244
|
|
|
* @param string $accessToken Access Token to send with the Request |
245
|
|
|
* |
246
|
|
|
* @return \Kunnu\Dropbox\DropboxResponse |
247
|
|
|
*/ |
248
|
|
|
public function postToAPI($endpoint, array $params = [], $accessToken = null) |
249
|
|
|
{ |
250
|
|
|
return $this->sendRequest("POST", $endpoint, 'api', $params, $accessToken); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Make Request to the API |
255
|
|
|
* |
256
|
|
|
* @param string $method HTTP Request Method |
257
|
|
|
* @param string $endpoint API Endpoint to send Request to |
258
|
|
|
* @param string $endpointType Endpoint type ['api'|'content'] |
259
|
|
|
* @param array $params Request Query Params |
260
|
|
|
* @param string $accessToken Access Token to send with the Request |
261
|
|
|
* @param DropboxFile $responseFile Save response to the file |
262
|
|
|
* |
263
|
|
|
* @return \Kunnu\Dropbox\DropboxResponse |
264
|
|
|
* |
265
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
266
|
|
|
*/ |
267
|
|
|
public function sendRequest( |
268
|
|
|
$method, |
269
|
|
|
$endpoint, |
270
|
|
|
$endpointType = 'api', |
271
|
|
|
array $params = [], |
272
|
|
|
$accessToken = null, |
273
|
|
|
DropboxFile $responseFile = null |
274
|
|
|
) { |
275
|
|
|
//Access Token |
276
|
|
|
$accessToken = $this->getAccessToken() ? $this->getAccessToken() : $accessToken; |
277
|
|
|
|
278
|
|
|
//Make a DropboxRequest object |
279
|
|
|
$request = new DropboxRequest($method, $endpoint, $accessToken, $endpointType, $params); |
280
|
|
|
|
281
|
|
|
//Make a DropboxResponse object if a response should be saved to the file |
282
|
|
|
$response = $responseFile ? new DropboxResponseToFile($request, $responseFile) : null; |
283
|
|
|
|
284
|
|
|
//Send Request through the DropboxClient |
285
|
|
|
//Fetch and return the Response |
286
|
|
|
return $this->getClient()->sendRequest($request, $response); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get the Access Token. |
291
|
|
|
* |
292
|
|
|
* @return string Access Token |
293
|
|
|
*/ |
294
|
|
|
public function getAccessToken() |
295
|
|
|
{ |
296
|
|
|
return $this->accessToken; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Set the Access Token. |
301
|
|
|
* |
302
|
|
|
* @param string $accessToken Access Token |
303
|
|
|
* |
304
|
|
|
* @return \Kunnu\Dropbox\Dropbox Dropbox Client |
305
|
|
|
*/ |
306
|
|
|
public function setAccessToken($accessToken) |
307
|
|
|
{ |
308
|
|
|
$this->accessToken = $accessToken; |
309
|
|
|
|
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Make Model from DropboxResponse |
315
|
|
|
* |
316
|
|
|
* @param DropboxResponse $response |
317
|
|
|
* |
318
|
|
|
* @return \Kunnu\Dropbox\Models\ModelInterface |
319
|
|
|
* |
320
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
321
|
|
|
*/ |
322
|
|
|
public function makeModelFromResponse(DropboxResponse $response) |
323
|
|
|
{ |
324
|
|
|
//Get the Decoded Body |
325
|
|
|
$body = $response->getDecodedBody(); |
326
|
|
|
|
327
|
|
|
if (is_null($body)) { |
328
|
|
|
$body = []; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
//Make and Return the Model |
332
|
|
|
return ModelFactory::make($body); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Get the contents of a Folder |
337
|
|
|
* |
338
|
|
|
* @param string $path Path to the folder. Defaults to root. |
339
|
|
|
* @param array $params Additional Params |
340
|
|
|
* |
341
|
|
|
* @return \Kunnu\Dropbox\Models\MetadataCollection |
342
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
343
|
|
|
* |
344
|
|
|
*/ |
345
|
|
View Code Duplication |
public function listFolder($path = null, array $params = []) |
|
|
|
|
346
|
|
|
{ |
347
|
|
|
//Specify the root folder as an |
348
|
|
|
//empty string rather than as "/" |
349
|
|
|
if ($path === '/') { |
350
|
|
|
$path = ""; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
//Set the path |
354
|
|
|
$params['path'] = $path; |
355
|
|
|
|
356
|
|
|
//Get File Metadata |
357
|
|
|
$response = $this->postToAPI('/files/list_folder', $params); |
358
|
|
|
|
359
|
|
|
//Make and Return the Model |
360
|
|
|
return $this->makeModelFromResponse($response); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Paginate through all files and retrieve updates to the folder, |
365
|
|
|
* using the cursor retrieved from listFolder or listFolderContinue |
366
|
|
|
* |
367
|
|
|
* @param string $cursor The cursor returned by your |
368
|
|
|
* last call to listFolder or listFolderContinue |
369
|
|
|
* |
370
|
|
|
* @return \Kunnu\Dropbox\Models\MetadataCollection |
371
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
372
|
|
|
* |
373
|
|
|
*/ |
374
|
|
|
public function listFolderContinue($cursor) |
375
|
|
|
{ |
376
|
|
|
$response = $this->postToAPI('/files/list_folder/continue', ['cursor' => $cursor]); |
377
|
|
|
|
378
|
|
|
//Make and Return the Model |
379
|
|
|
return $this->makeModelFromResponse($response); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Get a cursor for the folder's state. |
384
|
|
|
* |
385
|
|
|
* @param string $path Path to the folder. Defaults to root. |
386
|
|
|
* @param array $params Additional Params |
387
|
|
|
* |
388
|
|
|
* @return string The Cursor for the folder's state |
389
|
|
|
* |
390
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
391
|
|
|
* |
392
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-get_latest_cursor |
393
|
|
|
* |
394
|
|
|
*/ |
395
|
|
|
public function listFolderLatestCursor($path, array $params = []) |
396
|
|
|
{ |
397
|
|
|
//Specify the root folder as an |
398
|
|
|
//empty string rather than as "/" |
399
|
|
|
if ($path === '/') { |
400
|
|
|
$path = ""; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
//Set the path |
404
|
|
|
$params['path'] = $path; |
405
|
|
|
|
406
|
|
|
//Fetch the cursor |
407
|
|
|
$response = $this->postToAPI('/files/list_folder/get_latest_cursor', $params); |
408
|
|
|
|
409
|
|
|
//Retrieve the cursor |
410
|
|
|
$body = $response->getDecodedBody(); |
411
|
|
|
$cursor = isset($body['cursor']) ? $body['cursor'] : false; |
412
|
|
|
|
413
|
|
|
//No cursor returned |
414
|
|
|
if (!$cursor) { |
415
|
|
|
throw new DropboxClientException("Could not retrieve cursor. Something went wrong."); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
//Return the cursor |
419
|
|
|
return $cursor; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Get Revisions of a File |
424
|
|
|
* |
425
|
|
|
* @param string $path Path to the file |
426
|
|
|
* @param array $params Additional Params |
427
|
|
|
* |
428
|
|
|
* @return \Kunnu\Dropbox\Models\ModelCollection |
429
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_revisions |
430
|
|
|
* |
431
|
|
|
*/ |
432
|
|
|
public function listRevisions($path, array $params = []) |
433
|
|
|
{ |
434
|
|
|
//Set the Path |
435
|
|
|
$params['path'] = $path; |
436
|
|
|
|
437
|
|
|
//Fetch the Revisions |
438
|
|
|
$response = $this->postToAPI('/files/list_revisions', $params); |
439
|
|
|
|
440
|
|
|
//The file metadata of the entries, returned by this |
441
|
|
|
//endpoint doesn't include a '.tag' attribute, which |
442
|
|
|
//is used by the ModelFactory to resolve the correct |
443
|
|
|
//model. But since we know that revisions returned |
444
|
|
|
//are file metadata objects, we can explicitly cast |
445
|
|
|
//them as \Kunnu\Dropbox\Models\FileMetadata manually. |
446
|
|
|
$body = $response->getDecodedBody(); |
447
|
|
|
$entries = isset($body['entries']) ? $body['entries'] : []; |
448
|
|
|
$processedEntries = []; |
449
|
|
|
|
450
|
|
|
foreach ($entries as $entry) { |
451
|
|
|
$processedEntries[] = new FileMetadata($entry); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return new ModelCollection($processedEntries); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Search a folder for files/folders |
459
|
|
|
* |
460
|
|
|
* @param string $path Path to search |
461
|
|
|
* @param string $query Search Query |
462
|
|
|
* @param array $params Additional Params |
463
|
|
|
* |
464
|
|
|
* @return \Kunnu\Dropbox\Models\SearchResults |
465
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-search |
466
|
|
|
* |
467
|
|
|
*/ |
468
|
|
View Code Duplication |
public function search($path, $query, array $params = []) |
|
|
|
|
469
|
|
|
{ |
470
|
|
|
//Specify the root folder as an |
471
|
|
|
//empty string rather than as "/" |
472
|
|
|
if ($path === '/') { |
473
|
|
|
$path = ""; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
//Set the path and query |
477
|
|
|
$params['path'] = $path; |
478
|
|
|
$params['query'] = $query; |
479
|
|
|
|
480
|
|
|
//Fetch Search Results |
481
|
|
|
$response = $this->postToAPI('/files/search', $params); |
482
|
|
|
|
483
|
|
|
//Make and Return the Model |
484
|
|
|
return $this->makeModelFromResponse($response); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Create a folder at the given path |
489
|
|
|
* |
490
|
|
|
* @param string $path Path to create |
491
|
|
|
* @param boolean $autorename Auto Rename File |
492
|
|
|
* |
493
|
|
|
* @return \Kunnu\Dropbox\Models\FolderMetadata |
494
|
|
|
* |
495
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
496
|
|
|
* |
497
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
498
|
|
|
* |
499
|
|
|
*/ |
500
|
|
View Code Duplication |
public function createFolder($path, $autorename = false) |
|
|
|
|
501
|
|
|
{ |
502
|
|
|
//Path cannot be null |
503
|
|
|
if (is_null($path)) { |
504
|
|
|
throw new DropboxClientException("Path cannot be null."); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
//Create Folder |
508
|
|
|
$response = $this->postToAPI('/files/create_folder', ['path' => $path, 'autorename' => $autorename]); |
509
|
|
|
|
510
|
|
|
//Fetch the Metadata |
511
|
|
|
$body = $response->getDecodedBody(); |
512
|
|
|
|
513
|
|
|
//Make and Return the Model |
514
|
|
|
return new FolderMetadata($body); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Delete a file or folder at the given path |
519
|
|
|
* |
520
|
|
|
* @param string $path Path to file/folder to delete |
521
|
|
|
* |
522
|
|
|
* @return \Kunnu\Dropbox\Models\DeletedMetadata |
523
|
|
|
* |
524
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
525
|
|
|
* |
526
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
527
|
|
|
* |
528
|
|
|
*/ |
529
|
|
View Code Duplication |
public function delete($path) |
|
|
|
|
530
|
|
|
{ |
531
|
|
|
//Path cannot be null |
532
|
|
|
if (is_null($path)) { |
533
|
|
|
throw new DropboxClientException("Path cannot be null."); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
//Delete |
537
|
|
|
$response = $this->postToAPI('/files/delete_v2', ['path' => $path]); |
538
|
|
|
$body = $response->getDecodedBody(); |
539
|
|
|
|
540
|
|
|
//Response doesn't have Metadata |
541
|
|
|
if (!isset($body['metadata']) || !is_array($body['metadata'])) { |
542
|
|
|
throw new DropboxClientException("Invalid Response."); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
return new DeletedMetadata($body['metadata']); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Move a file or folder to a different location |
550
|
|
|
* |
551
|
|
|
* @param string $fromPath Path to be moved |
552
|
|
|
* @param string $toPath Path to be moved to |
553
|
|
|
* |
554
|
|
|
* @return \Kunnu\Dropbox\Models\DeletedMetadata|\Kunnu\Dropbox\Models\FileMetadata |
555
|
|
|
* |
556
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
557
|
|
|
* |
558
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-move |
559
|
|
|
* |
560
|
|
|
*/ |
561
|
|
View Code Duplication |
public function move($fromPath, $toPath) |
|
|
|
|
562
|
|
|
{ |
563
|
|
|
//From and To paths cannot be null |
564
|
|
|
if (is_null($fromPath) || is_null($toPath)) { |
565
|
|
|
throw new DropboxClientException("From and To paths cannot be null."); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
//Response |
569
|
|
|
$response = $this->postToAPI('/files/move', ['from_path' => $fromPath, 'to_path' => $toPath]); |
570
|
|
|
|
571
|
|
|
//Make and Return the Model |
572
|
|
|
return $this->makeModelFromResponse($response); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Copy a file or folder to a different location |
577
|
|
|
* |
578
|
|
|
* @param string $fromPath Path to be copied |
579
|
|
|
* @param string $toPath Path to be copied to |
580
|
|
|
* |
581
|
|
|
* @return \Kunnu\Dropbox\Models\DeletedMetadata|\Kunnu\Dropbox\Models\FileMetadata |
582
|
|
|
* |
583
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
584
|
|
|
* |
585
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy |
586
|
|
|
* |
587
|
|
|
*/ |
588
|
|
View Code Duplication |
public function copy($fromPath, $toPath) |
|
|
|
|
589
|
|
|
{ |
590
|
|
|
//From and To paths cannot be null |
591
|
|
|
if (is_null($fromPath) || is_null($toPath)) { |
592
|
|
|
throw new DropboxClientException("From and To paths cannot be null."); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
//Response |
596
|
|
|
$response = $this->postToAPI('/files/copy', ['from_path' => $fromPath, 'to_path' => $toPath]); |
597
|
|
|
|
598
|
|
|
//Make and Return the Model |
599
|
|
|
return $this->makeModelFromResponse($response); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Restore a file to the specific version |
604
|
|
|
* |
605
|
|
|
* @param string $path Path to the file to restore |
606
|
|
|
* @param string $rev Revision to store for the file |
607
|
|
|
* |
608
|
|
|
* @return \Kunnu\Dropbox\Models\DeletedMetadata|\Kunnu\Dropbox\Models\FileMetadata|\Kunnu\Dropbox\Models\FolderMetadata |
609
|
|
|
* |
610
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
611
|
|
|
* |
612
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-restore |
613
|
|
|
* |
614
|
|
|
*/ |
615
|
|
View Code Duplication |
public function restore($path, $rev) |
|
|
|
|
616
|
|
|
{ |
617
|
|
|
//Path and Revision cannot be null |
618
|
|
|
if (is_null($path) || is_null($rev)) { |
619
|
|
|
throw new DropboxClientException("Path and Revision cannot be null."); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
//Response |
623
|
|
|
$response = $this->postToAPI('/files/restore', ['path' => $path, 'rev' => $rev]); |
624
|
|
|
|
625
|
|
|
//Fetch the Metadata |
626
|
|
|
$body = $response->getDecodedBody(); |
627
|
|
|
|
628
|
|
|
//Make and Return the Model |
629
|
|
|
return new FileMetadata($body); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Get Copy Reference |
634
|
|
|
* |
635
|
|
|
* @param string $path Path to the file or folder to get a copy reference to |
636
|
|
|
* |
637
|
|
|
* @return \Kunnu\Dropbox\Models\CopyReference |
638
|
|
|
* |
639
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
640
|
|
|
* |
641
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_reference-get |
642
|
|
|
* |
643
|
|
|
*/ |
644
|
|
View Code Duplication |
public function getCopyReference($path) |
|
|
|
|
645
|
|
|
{ |
646
|
|
|
//Path cannot be null |
647
|
|
|
if (is_null($path)) { |
648
|
|
|
throw new DropboxClientException("Path cannot be null."); |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
//Get Copy Reference |
652
|
|
|
$response = $this->postToAPI('/files/copy_reference/get', ['path' => $path]); |
653
|
|
|
$body = $response->getDecodedBody(); |
654
|
|
|
|
655
|
|
|
//Make and Return the Model |
656
|
|
|
return new CopyReference($body); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Save Copy Reference |
661
|
|
|
* |
662
|
|
|
* @param string $path Path to the file or folder to get a copy reference to |
663
|
|
|
* @param string $copyReference Copy reference returned by getCopyReference |
664
|
|
|
* |
665
|
|
|
* @return \Kunnu\Dropbox\Models\FileMetadata|\Kunnu\Dropbox\Models\FolderMetadata |
666
|
|
|
* |
667
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
668
|
|
|
* |
669
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_reference-save |
670
|
|
|
* |
671
|
|
|
*/ |
672
|
|
|
public function saveCopyReference($path, $copyReference) |
673
|
|
|
{ |
674
|
|
|
//Path and Copy Reference cannot be null |
675
|
|
|
if (is_null($path) || is_null($copyReference)) { |
676
|
|
|
throw new DropboxClientException("Path and Copy Reference cannot be null."); |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
//Save Copy Reference |
680
|
|
|
$response = $this->postToAPI('/files/copy_reference/save', |
681
|
|
|
['path' => $path, 'copy_reference' => $copyReference]); |
682
|
|
|
$body = $response->getDecodedBody(); |
683
|
|
|
|
684
|
|
|
//Response doesn't have Metadata |
685
|
|
|
if (!isset($body['metadata']) || !is_array($body['metadata'])) { |
686
|
|
|
throw new DropboxClientException("Invalid Response."); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
//Make and return the Model |
690
|
|
|
return ModelFactory::make($body['metadata']); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* Get a temporary link to stream contents of a file |
695
|
|
|
* |
696
|
|
|
* @param string $path Path to the file you want a temporary link to |
697
|
|
|
* |
698
|
|
|
* https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
699
|
|
|
* |
700
|
|
|
* @return \Kunnu\Dropbox\Models\TemporaryLink |
701
|
|
|
* |
702
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
703
|
|
|
*/ |
704
|
|
View Code Duplication |
public function getTemporaryLink($path) |
|
|
|
|
705
|
|
|
{ |
706
|
|
|
//Path cannot be null |
707
|
|
|
if (is_null($path)) { |
708
|
|
|
throw new DropboxClientException("Path cannot be null."); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
//Get Temporary Link |
712
|
|
|
$response = $this->postToAPI('/files/get_temporary_link', ['path' => $path]); |
713
|
|
|
|
714
|
|
|
//Make and Return the Model |
715
|
|
|
return $this->makeModelFromResponse($response); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Save a specified URL into a file in user's Dropbox |
720
|
|
|
* |
721
|
|
|
* @param string $path Path where the URL will be saved |
722
|
|
|
* @param string $url URL to be saved |
723
|
|
|
* |
724
|
|
|
* @return string Async Job ID |
725
|
|
|
* |
726
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
727
|
|
|
* |
728
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-save_url |
729
|
|
|
* |
730
|
|
|
*/ |
731
|
|
View Code Duplication |
public function saveUrl($path, $url) |
|
|
|
|
732
|
|
|
{ |
733
|
|
|
//Path and URL cannot be null |
734
|
|
|
if (is_null($path) || is_null($url)) { |
735
|
|
|
throw new DropboxClientException("Path and URL cannot be null."); |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
//Save URL |
739
|
|
|
$response = $this->postToAPI('/files/save_url', ['path' => $path, 'url' => $url]); |
740
|
|
|
$body = $response->getDecodedBody(); |
741
|
|
|
|
742
|
|
|
if (!isset($body['async_job_id'])) { |
743
|
|
|
throw new DropboxClientException("Could not retrieve Async Job ID."); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
//Return the Async Job ID |
747
|
|
|
return $body['async_job_id']; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
/** |
751
|
|
|
* Save a specified URL into a file in user's Dropbox |
752
|
|
|
* |
753
|
|
|
* @param $asyncJobId |
754
|
|
|
* |
755
|
|
|
* @return \Kunnu\Dropbox\Models\FileMetadata|string Status (failed|in_progress) or FileMetadata (if complete) |
756
|
|
|
* |
757
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
758
|
|
|
* |
759
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-save_url-check_job_status |
760
|
|
|
* |
761
|
|
|
*/ |
762
|
|
|
public function checkJobStatus($asyncJobId) |
763
|
|
|
{ |
764
|
|
|
//Async Job ID cannot be null |
765
|
|
|
if (is_null($asyncJobId)) { |
766
|
|
|
throw new DropboxClientException("Async Job ID cannot be null."); |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
//Get Job Status |
770
|
|
|
$response = $this->postToAPI('/files/save_url/check_job_status', ['async_job_id' => $asyncJobId]); |
771
|
|
|
$body = $response->getDecodedBody(); |
772
|
|
|
|
773
|
|
|
//Status |
774
|
|
|
$status = isset($body['.tag']) ? $body['.tag'] : ''; |
775
|
|
|
|
776
|
|
|
//If status is complete |
777
|
|
|
if ($status === 'complete') { |
778
|
|
|
return new FileMetadata($body); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
//Return the status |
782
|
|
|
return $status; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Upload a File to Dropbox |
787
|
|
|
* |
788
|
|
|
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file |
789
|
|
|
* @param string $path Path to upload the file to |
790
|
|
|
* @param array $params Additional Params |
791
|
|
|
* |
792
|
|
|
* @return \Kunnu\Dropbox\Models\FileMetadata |
793
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
794
|
|
|
* |
795
|
|
|
*/ |
796
|
|
|
public function upload($dropboxFile, $path, array $params = []) |
797
|
|
|
{ |
798
|
|
|
//Make Dropbox File |
799
|
|
|
$dropboxFile = $this->makeDropboxFile($dropboxFile); |
800
|
|
|
|
801
|
|
|
//If the file is larger than the Chunked Upload Threshold |
802
|
|
|
if ($dropboxFile->getSize() > static::AUTO_CHUNKED_UPLOAD_THRESHOLD) { |
803
|
|
|
//Upload the file in sessions/chunks |
804
|
|
|
return $this->uploadChunked($dropboxFile, $path, null, null, $params); |
805
|
|
|
} |
806
|
|
|
|
807
|
|
|
//Simple file upload |
808
|
|
|
return $this->simpleUpload($dropboxFile, $path, $params); |
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* Make DropboxFile Object using the given $data string as the file contents. |
814
|
|
|
* @param $path |
815
|
|
|
* @param $data |
816
|
|
|
* @return DropboxFile |
817
|
|
|
* @throws DropboxClientException |
818
|
|
|
* @throws DropboxClientUnableToWriteToTempException |
819
|
|
|
*/ |
820
|
|
|
public function makeDropboxFileFromString($path, $data) |
821
|
|
|
{ |
822
|
|
|
//create a temp file with a prefix |
823
|
|
|
$tmpfname = tempnam(sys_get_temp_dir(), static::TMP_PREFIX); |
824
|
|
|
|
825
|
|
|
if (!is_writable($tmpfname)) { // Test if the file is writable |
826
|
|
|
throw new DropboxClientUnableToWriteToTempException("Cannot write to {$tmpfname}"); |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
$handle = fopen($tmpfname, DropboxFile::MODE_WRITE); |
830
|
|
|
|
831
|
|
|
if (!is_resource($handle)) { // Test if PHP could open the file |
832
|
|
|
throw new DropboxClientUnableToOpenTempFileException("Could not open {$tmpfname} on writing mode."); |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
fwrite($handle, $data); |
836
|
|
|
|
837
|
|
|
return DropboxFile::createByStream($path, $handle, DropboxFile::MODE_WRITE); |
838
|
|
|
} |
839
|
|
|
|
840
|
|
|
/** |
841
|
|
|
* Make DropboxFile Object |
842
|
|
|
* |
843
|
|
|
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file |
844
|
|
|
* @param int $maxLength Max Bytes to read from the file |
845
|
|
|
* @param int $offset Seek to specified offset before reading |
846
|
|
|
* @param string $mode The type of access |
847
|
|
|
* |
848
|
|
|
* @return \Kunnu\Dropbox\DropboxFile |
849
|
|
|
*/ |
850
|
|
|
public function makeDropboxFile($dropboxFile, $maxLength = null, $offset = null, $mode = DropboxFile::MODE_READ) |
851
|
|
|
{ |
852
|
|
|
//Uploading file by file path |
853
|
|
|
if (!$dropboxFile instanceof DropboxFile) { |
854
|
|
|
//Create a DropboxFile Object |
855
|
|
|
$dropboxFile = new DropboxFile($dropboxFile, $mode); |
856
|
|
|
} elseif ($mode !== $dropboxFile->getMode()) { |
857
|
|
|
//Reopen the file with expected mode |
858
|
|
|
$dropboxFile->close(); |
859
|
|
|
$dropboxFile = new DropboxFile($dropboxFile->getFilePath(), $mode); |
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
if (!is_null($offset)) { |
863
|
|
|
$dropboxFile->setOffset($offset); |
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
if (!is_null($maxLength)) { |
867
|
|
|
$dropboxFile->setMaxLength($maxLength); |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
//Return the DropboxFile Object |
871
|
|
|
return $dropboxFile; |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
/** |
875
|
|
|
* Upload file in sessions/chunks |
876
|
|
|
* |
877
|
|
|
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file |
878
|
|
|
* @param string $path Path to save the file to, on Dropbox |
879
|
|
|
* @param int $fileSize The size of the file |
880
|
|
|
* @param int $chunkSize The amount of data to upload in each chunk |
881
|
|
|
* @param array $params Additional Params |
882
|
|
|
* |
883
|
|
|
* @return \Kunnu\Dropbox\Models\FileMetadata |
884
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
885
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
886
|
|
|
* |
887
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
888
|
|
|
*/ |
889
|
|
|
public function uploadChunked($dropboxFile, $path, $fileSize = null, $chunkSize = null, array $params = array()) |
890
|
|
|
{ |
891
|
|
|
//Make Dropbox File |
892
|
|
|
$dropboxFile = $this->makeDropboxFile($dropboxFile); |
893
|
|
|
|
894
|
|
|
//No file size specified explicitly |
895
|
|
|
if (is_null($fileSize)) { |
896
|
|
|
$fileSize = $dropboxFile->getSize(); |
897
|
|
|
} |
898
|
|
|
|
899
|
|
|
//No chunk size specified, use default size |
900
|
|
|
if (is_null($chunkSize)) { |
901
|
|
|
$chunkSize = static::DEFAULT_CHUNK_SIZE; |
902
|
|
|
} |
903
|
|
|
|
904
|
|
|
//If the fileSize is smaller |
905
|
|
|
//than the chunk size, we'll |
906
|
|
|
//make the chunk size relatively |
907
|
|
|
//smaller than the file size |
908
|
|
|
if ($fileSize <= $chunkSize) { |
909
|
|
|
$chunkSize = intval($fileSize / 2); |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
//Start the Upload Session with the file path |
913
|
|
|
//since the DropboxFile object will be created |
914
|
|
|
//again using the new chunk size. |
915
|
|
|
$sessionId = $this->startUploadSession($dropboxFile->getFilePath(), $chunkSize); |
916
|
|
|
|
917
|
|
|
//Uploaded |
918
|
|
|
$uploaded = $chunkSize; |
919
|
|
|
|
920
|
|
|
//Remaining |
921
|
|
|
$remaining = $fileSize - $chunkSize; |
922
|
|
|
|
923
|
|
|
//While the remaining bytes are |
924
|
|
|
//more than the chunk size, append |
925
|
|
|
//the chunk to the upload session. |
926
|
|
|
while ($remaining > $chunkSize) { |
927
|
|
|
//Append the next chunk to the Upload session |
928
|
|
|
$sessionId = $this->appendUploadSession($dropboxFile, $sessionId, $uploaded, $chunkSize); |
929
|
|
|
|
930
|
|
|
//Update remaining and uploaded |
931
|
|
|
$uploaded = $uploaded + $chunkSize; |
932
|
|
|
$remaining = $remaining - $chunkSize; |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
//Finish the Upload Session and return the Uploaded File Metadata |
936
|
|
|
return $this->finishUploadSession($dropboxFile, $sessionId, $uploaded, $remaining, $path, $params); |
937
|
|
|
} |
938
|
|
|
|
939
|
|
|
/** |
940
|
|
|
* Start an Upload Session |
941
|
|
|
* |
942
|
|
|
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file |
943
|
|
|
* @param int $chunkSize Size of file chunk to upload |
944
|
|
|
* @param boolean $close Closes the session for "appendUploadSession" |
945
|
|
|
* |
946
|
|
|
* @return string Unique identifier for the upload session |
947
|
|
|
* |
948
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
949
|
|
|
* |
950
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
951
|
|
|
* |
952
|
|
|
*/ |
953
|
|
|
public function startUploadSession($dropboxFile, $chunkSize = -1, $close = false) |
954
|
|
|
{ |
955
|
|
|
//Make Dropbox File with the given chunk size |
956
|
|
|
$dropboxFile = $this->makeDropboxFile($dropboxFile, $chunkSize); |
957
|
|
|
|
958
|
|
|
//Set the close param |
959
|
|
|
$params = [ |
960
|
|
|
'close' => $close ? true : false, |
961
|
|
|
'file' => $dropboxFile |
962
|
|
|
]; |
963
|
|
|
|
964
|
|
|
//Upload File |
965
|
|
|
$file = $this->postToContent('/files/upload_session/start', $params); |
966
|
|
|
$body = $file->getDecodedBody(); |
967
|
|
|
|
968
|
|
|
//Cannot retrieve Session ID |
969
|
|
|
if (!isset($body['session_id'])) { |
970
|
|
|
throw new DropboxClientException("Could not retrieve Session ID."); |
971
|
|
|
} |
972
|
|
|
|
973
|
|
|
//Return the Session ID |
974
|
|
|
return $body['session_id']; |
975
|
|
|
} |
976
|
|
|
|
977
|
|
|
/** |
978
|
|
|
* Make a HTTP POST Request to the Content endpoint type |
979
|
|
|
* |
980
|
|
|
* @param string $endpoint Content Endpoint to send Request to |
981
|
|
|
* @param array $params Request Query Params |
982
|
|
|
* @param string $accessToken Access Token to send with the Request |
983
|
|
|
* @param DropboxFile $responseFile Save response to the file |
984
|
|
|
* |
985
|
|
|
* @return \Kunnu\Dropbox\DropboxResponse |
986
|
|
|
*/ |
987
|
|
|
public function postToContent($endpoint, array $params = [], $accessToken = null, DropboxFile $responseFile = null) |
988
|
|
|
{ |
989
|
|
|
return $this->sendRequest("POST", $endpoint, 'content', $params, $accessToken, $responseFile); |
990
|
|
|
} |
991
|
|
|
|
992
|
|
|
/** |
993
|
|
|
* Append more data to an Upload Session |
994
|
|
|
* |
995
|
|
|
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file |
996
|
|
|
* @param string $sessionId Session ID returned by `startUploadSession` |
997
|
|
|
* @param int $offset The amount of data that has been uploaded so far |
998
|
|
|
* @param int $chunkSize The amount of data to upload |
999
|
|
|
* @param boolean $close Closes the session for futher "appendUploadSession" calls |
1000
|
|
|
* |
1001
|
|
|
* @return string Unique identifier for the upload session |
1002
|
|
|
* |
1003
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
1004
|
|
|
* |
1005
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
1006
|
|
|
* |
1007
|
|
|
*/ |
1008
|
|
|
public function appendUploadSession($dropboxFile, $sessionId, $offset, $chunkSize, $close = false) |
1009
|
|
|
{ |
1010
|
|
|
//Make Dropbox File |
1011
|
|
|
$dropboxFile = $this->makeDropboxFile($dropboxFile, $chunkSize, $offset); |
1012
|
|
|
|
1013
|
|
|
//Session ID, offset, chunkSize and path cannot be null |
1014
|
|
View Code Duplication |
if (is_null($sessionId) || is_null($offset) || is_null($chunkSize)) { |
1015
|
|
|
throw new DropboxClientException("Session ID, offset and chunk size cannot be null"); |
1016
|
|
|
} |
1017
|
|
|
|
1018
|
|
|
$params = []; |
1019
|
|
|
|
1020
|
|
|
//Set the File |
1021
|
|
|
$params['file'] = $dropboxFile; |
1022
|
|
|
|
1023
|
|
|
//Set the Cursor: Session ID and Offset |
1024
|
|
|
$params['cursor'] = ['session_id' => $sessionId, 'offset' => $offset]; |
1025
|
|
|
|
1026
|
|
|
//Set the close param |
1027
|
|
|
$params['close'] = $close ? true : false; |
1028
|
|
|
|
1029
|
|
|
//Since this endpoint doesn't have |
1030
|
|
|
//any return values, we'll disable the |
1031
|
|
|
//response validation for this request. |
1032
|
|
|
$params['validateResponse'] = false; |
1033
|
|
|
|
1034
|
|
|
//Upload File |
1035
|
|
|
$this->postToContent('/files/upload_session/append_v2', $params); |
1036
|
|
|
|
1037
|
|
|
//Make and Return the Model |
1038
|
|
|
return $sessionId; |
1039
|
|
|
} |
1040
|
|
|
|
1041
|
|
|
/** |
1042
|
|
|
* Finish an upload session and save the uploaded data to the given file path |
1043
|
|
|
* |
1044
|
|
|
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file |
1045
|
|
|
* @param string $sessionId Session ID returned by `startUploadSession` |
1046
|
|
|
* @param int $offset The amount of data that has been uploaded so far |
1047
|
|
|
* @param int $remaining The amount of data that is remaining |
1048
|
|
|
* @param string $path Path to save the file to, on Dropbox |
1049
|
|
|
* @param array $params Additional Params |
1050
|
|
|
* |
1051
|
|
|
* @return \Kunnu\Dropbox\Models\FileMetadata |
1052
|
|
|
* |
1053
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
1054
|
|
|
* |
1055
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
1056
|
|
|
* |
1057
|
|
|
*/ |
1058
|
|
|
public function finishUploadSession($dropboxFile, $sessionId, $offset, $remaining, $path, array $params = []) |
1059
|
|
|
{ |
1060
|
|
|
//Make Dropbox File |
1061
|
|
|
$dropboxFile = $this->makeDropboxFile($dropboxFile, $remaining, $offset); |
1062
|
|
|
|
1063
|
|
|
//Session ID, offset, remaining and path cannot be null |
1064
|
|
View Code Duplication |
if (is_null($sessionId) || is_null($path) || is_null($offset) || is_null($remaining)) { |
1065
|
|
|
throw new DropboxClientException("Session ID, offset, remaining and path cannot be null"); |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
$queryParams = []; |
1069
|
|
|
|
1070
|
|
|
//Set the File |
1071
|
|
|
$queryParams['file'] = $dropboxFile; |
1072
|
|
|
|
1073
|
|
|
//Set the Cursor: Session ID and Offset |
1074
|
|
|
$queryParams['cursor'] = ['session_id' => $sessionId, 'offset' => $offset]; |
1075
|
|
|
|
1076
|
|
|
//Set the path |
1077
|
|
|
$params['path'] = $path; |
1078
|
|
|
//Set the Commit |
1079
|
|
|
$queryParams['commit'] = $params; |
1080
|
|
|
|
1081
|
|
|
//Upload File |
1082
|
|
|
$file = $this->postToContent('/files/upload_session/finish', $queryParams); |
1083
|
|
|
$body = $file->getDecodedBody(); |
1084
|
|
|
|
1085
|
|
|
//Make and Return the Model |
1086
|
|
|
return new FileMetadata($body); |
1087
|
|
|
} |
1088
|
|
|
|
1089
|
|
|
/** |
1090
|
|
|
* Upload a File to Dropbox in a single request |
1091
|
|
|
* |
1092
|
|
|
* @param string|DropboxFile $dropboxFile DropboxFile object or Path to file |
1093
|
|
|
* @param string $path Path to upload the file to |
1094
|
|
|
* @param array $params Additional Params |
1095
|
|
|
* |
1096
|
|
|
* @return \Kunnu\Dropbox\Models\FileMetadata |
1097
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
1098
|
|
|
* |
1099
|
|
|
*/ |
1100
|
|
|
public function simpleUpload($dropboxFile, $path, array $params = []) |
1101
|
|
|
{ |
1102
|
|
|
//Make Dropbox File |
1103
|
|
|
$dropboxFile = $this->makeDropboxFile($dropboxFile); |
1104
|
|
|
|
1105
|
|
|
//Set the path and file |
1106
|
|
|
$params['path'] = $path; |
1107
|
|
|
$params['file'] = $dropboxFile; |
1108
|
|
|
|
1109
|
|
|
//Upload File |
1110
|
|
|
$file = $this->postToContent('/files/upload', $params); |
1111
|
|
|
$body = $file->getDecodedBody(); |
1112
|
|
|
|
1113
|
|
|
//Make and Return the Model |
1114
|
|
|
return new FileMetadata($body); |
1115
|
|
|
} |
1116
|
|
|
|
1117
|
|
|
/** |
1118
|
|
|
* Get a thumbnail for an image |
1119
|
|
|
* |
1120
|
|
|
* @param string $path Path to the file you want a thumbnail to |
1121
|
|
|
* @param string $size Size for the thumbnail image ['thumb','small','medium','large','huge'] |
1122
|
|
|
* @param string $format Format for the thumbnail image ['jpeg'|'png'] |
1123
|
|
|
* |
1124
|
|
|
* @return \Kunnu\Dropbox\Models\Thumbnail |
1125
|
|
|
* |
1126
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
1127
|
|
|
* |
1128
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
1129
|
|
|
* |
1130
|
|
|
*/ |
1131
|
|
|
public function getThumbnail($path, $size = 'small', $format = 'jpeg') |
1132
|
|
|
{ |
1133
|
|
|
//Path cannot be null |
1134
|
|
|
if (is_null($path)) { |
1135
|
|
|
throw new DropboxClientException("Path cannot be null."); |
1136
|
|
|
} |
1137
|
|
|
|
1138
|
|
|
//Invalid Format |
1139
|
|
|
if (!in_array($format, ['jpeg', 'png'])) { |
1140
|
|
|
throw new DropboxClientException("Invalid format. Must either be 'jpeg' or 'png'."); |
1141
|
|
|
} |
1142
|
|
|
|
1143
|
|
|
//Thumbnail size |
1144
|
|
|
$size = $this->getThumbnailSize($size); |
1145
|
|
|
|
1146
|
|
|
//Get Thumbnail |
1147
|
|
|
$response = $this->postToContent('/files/get_thumbnail', |
1148
|
|
|
['path' => $path, 'format' => $format, 'size' => $size]); |
1149
|
|
|
|
1150
|
|
|
//Get file metadata from response headers |
1151
|
|
|
$metadata = $this->getMetadataFromResponseHeaders($response); |
1152
|
|
|
|
1153
|
|
|
//File Contents |
1154
|
|
|
$contents = $response->getBody(); |
1155
|
|
|
|
1156
|
|
|
//Make and return a Thumbnail model |
1157
|
|
|
return new Thumbnail($metadata, $contents); |
1158
|
|
|
} |
1159
|
|
|
|
1160
|
|
|
/** |
1161
|
|
|
* Get thumbnail size |
1162
|
|
|
* |
1163
|
|
|
* @param string $size Thumbnail Size |
1164
|
|
|
* |
1165
|
|
|
* @return string |
1166
|
|
|
*/ |
1167
|
|
|
protected function getThumbnailSize($size) |
1168
|
|
|
{ |
1169
|
|
|
$thumbnailSizes = [ |
1170
|
|
|
'thumb' => 'w32h32', |
1171
|
|
|
'small' => 'w64h64', |
1172
|
|
|
'medium' => 'w128h128', |
1173
|
|
|
'large' => 'w640h480', |
1174
|
|
|
'huge' => 'w1024h768' |
1175
|
|
|
]; |
1176
|
|
|
|
1177
|
|
|
return isset($thumbnailSizes[$size]) ? $thumbnailSizes[$size] : $thumbnailSizes['small']; |
1178
|
|
|
} |
1179
|
|
|
|
1180
|
|
|
/** |
1181
|
|
|
* Get metadata from response headers |
1182
|
|
|
* |
1183
|
|
|
* @param DropboxResponse $response |
1184
|
|
|
* |
1185
|
|
|
* @return array |
1186
|
|
|
*/ |
1187
|
|
|
protected function getMetadataFromResponseHeaders(DropboxResponse $response) |
1188
|
|
|
{ |
1189
|
|
|
//Response Headers |
1190
|
|
|
$headers = $response->getHeaders(); |
1191
|
|
|
|
1192
|
|
|
//Empty metadata for when |
1193
|
|
|
//metadata isn't returned |
1194
|
|
|
$metadata = []; |
1195
|
|
|
|
1196
|
|
|
//If metadata is available |
1197
|
|
|
if (isset($headers[static::METADATA_HEADER])) { |
1198
|
|
|
//File Metadata |
1199
|
|
|
$data = $headers[static::METADATA_HEADER]; |
1200
|
|
|
|
1201
|
|
|
//The metadata is present in the first index |
1202
|
|
|
//of the metadata response header array |
1203
|
|
|
if (is_array($data) && isset($data[0])) { |
1204
|
|
|
$data = $data[0]; |
1205
|
|
|
} |
1206
|
|
|
|
1207
|
|
|
//Since the metadata is returned as a json string |
1208
|
|
|
//it needs to be decoded into an associative array |
1209
|
|
|
$metadata = json_decode((string)$data, true); |
1210
|
|
|
} |
1211
|
|
|
|
1212
|
|
|
//Return the metadata |
1213
|
|
|
return $metadata; |
1214
|
|
|
} |
1215
|
|
|
|
1216
|
|
|
/** |
1217
|
|
|
* Download a File |
1218
|
|
|
* |
1219
|
|
|
* @param string $path Path to the file you want to download |
1220
|
|
|
* @param null|string|DropboxFile $dropboxFile DropboxFile object or Path to target file |
1221
|
|
|
* |
1222
|
|
|
* @return \Kunnu\Dropbox\Models\File |
1223
|
|
|
* |
1224
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
1225
|
|
|
* |
1226
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
1227
|
|
|
* |
1228
|
|
|
*/ |
1229
|
|
View Code Duplication |
public function download($path, $dropboxFile = null) |
|
|
|
|
1230
|
|
|
{ |
1231
|
|
|
//Path cannot be null |
1232
|
|
|
if (is_null($path)) { |
1233
|
|
|
throw new DropboxClientException("Path cannot be null."); |
1234
|
|
|
} |
1235
|
|
|
|
1236
|
|
|
//Make Dropbox File if target is specified |
1237
|
|
|
$dropboxFile = $dropboxFile ? $this->makeDropboxFile($dropboxFile, null, null, DropboxFile::MODE_WRITE) : null; |
1238
|
|
|
|
1239
|
|
|
//Download File |
1240
|
|
|
$response = $this->postToContent('/files/download', ['path' => $path], null, $dropboxFile); |
1241
|
|
|
|
1242
|
|
|
//Get file metadata from response headers |
1243
|
|
|
$metadata = $this->getMetadataFromResponseHeaders($response); |
1244
|
|
|
|
1245
|
|
|
//File Contents |
1246
|
|
|
$contents = $dropboxFile ? $this->makeDropboxFile($dropboxFile) : $response->getBody(); |
1247
|
|
|
|
1248
|
|
|
//Make and return a File model |
1249
|
|
|
return new File($metadata, $contents); |
1250
|
|
|
} |
1251
|
|
|
|
1252
|
|
|
/** |
1253
|
|
|
* Download a folder as a zip file |
1254
|
|
|
* |
1255
|
|
|
* @param string $path Path to the file you want to download |
1256
|
|
|
* @param null|string|DropboxFile $dropboxFile DropboxFile object or Path to target file |
1257
|
|
|
* |
1258
|
|
|
* @return \Kunnu\Dropbox\Models\File |
1259
|
|
|
* |
1260
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
1261
|
|
|
* |
1262
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-download_zip |
1263
|
|
|
* |
1264
|
|
|
*/ |
1265
|
|
View Code Duplication |
public function downloadZip($path, $dropboxFile = null) |
|
|
|
|
1266
|
|
|
{ |
1267
|
|
|
//Path cannot be null |
1268
|
|
|
if (is_null($path)) { |
1269
|
|
|
throw new DropboxClientException("Path cannot be null."); |
1270
|
|
|
} |
1271
|
|
|
|
1272
|
|
|
//Make Dropbox File if target is specified |
1273
|
|
|
$dropboxFile = $dropboxFile ? $this->makeDropboxFile($dropboxFile, null, null, DropboxFile::MODE_WRITE) : null; |
1274
|
|
|
|
1275
|
|
|
//Download File |
1276
|
|
|
$response = $this->postToContent('/files/download_zip', ['path' => $path], null, $dropboxFile); |
1277
|
|
|
|
1278
|
|
|
//Get file metadata from response headers |
1279
|
|
|
$metadata = $this->getMetadataFromResponseHeaders($response); |
1280
|
|
|
|
1281
|
|
|
//File Contents |
1282
|
|
|
$contents = $dropboxFile ? $this->makeDropboxFile($dropboxFile) : $response->getBody(); |
1283
|
|
|
|
1284
|
|
|
//Make and return a File model |
1285
|
|
|
return new File($metadata, $contents); |
1286
|
|
|
} |
1287
|
|
|
|
1288
|
|
|
/** |
1289
|
|
|
* Get Current Account |
1290
|
|
|
* |
1291
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
1292
|
|
|
* |
1293
|
|
|
* @return \Kunnu\Dropbox\Models\Account |
1294
|
|
|
*/ |
1295
|
|
|
public function getCurrentAccount() |
1296
|
|
|
{ |
1297
|
|
|
//Get current account |
1298
|
|
|
$response = $this->postToAPI('/users/get_current_account', []); |
1299
|
|
|
$body = $response->getDecodedBody(); |
1300
|
|
|
|
1301
|
|
|
//Make and return the model |
1302
|
|
|
return new Account($body); |
1303
|
|
|
} |
1304
|
|
|
|
1305
|
|
|
/** |
1306
|
|
|
* Get Account |
1307
|
|
|
* |
1308
|
|
|
* @param string $account_id Account ID of the account to get details for |
1309
|
|
|
* |
1310
|
|
|
* @return \Kunnu\Dropbox\Models\Account |
1311
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_account |
1312
|
|
|
* |
1313
|
|
|
*/ |
1314
|
|
|
public function getAccount($account_id) |
1315
|
|
|
{ |
1316
|
|
|
//Get account |
1317
|
|
|
$response = $this->postToAPI('/users/get_account', ['account_id' => $account_id]); |
1318
|
|
|
$body = $response->getDecodedBody(); |
1319
|
|
|
|
1320
|
|
|
//Make and return the model |
1321
|
|
|
return new Account($body); |
1322
|
|
|
} |
1323
|
|
|
|
1324
|
|
|
/** |
1325
|
|
|
* Get Multiple Accounts in one call |
1326
|
|
|
* |
1327
|
|
|
* @param array $account_ids IDs of the accounts to get details for |
1328
|
|
|
* |
1329
|
|
|
* @return \Kunnu\Dropbox\Models\AccountList |
1330
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_account_batch |
1331
|
|
|
* |
1332
|
|
|
*/ |
1333
|
|
|
public function getAccounts(array $account_ids = []) |
1334
|
|
|
{ |
1335
|
|
|
//Get account |
1336
|
|
|
$response = $this->postToAPI('/users/get_account_batch', ['account_ids' => $account_ids]); |
1337
|
|
|
$body = $response->getDecodedBody(); |
1338
|
|
|
|
1339
|
|
|
//Make and return the model |
1340
|
|
|
return new AccountList($body); |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
|
|
/** |
1344
|
|
|
* Get Space Usage for the current user's account |
1345
|
|
|
* |
1346
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_space_usage |
1347
|
|
|
* |
1348
|
|
|
* @return array |
1349
|
|
|
*/ |
1350
|
|
|
public function getSpaceUsage() |
1351
|
|
|
{ |
1352
|
|
|
//Get space usage |
1353
|
|
|
$response = $this->postToAPI('/users/get_space_usage', []); |
1354
|
|
|
$body = $response->getDecodedBody(); |
1355
|
|
|
|
1356
|
|
|
//Return the decoded body |
1357
|
|
|
return $body; |
1358
|
|
|
} |
1359
|
|
|
} |
1360
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.