1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace STS\StorageConnect\Drivers\Dropbox; |
4
|
|
|
|
5
|
|
|
use Kunnu\Dropbox\Dropbox; |
6
|
|
|
use Kunnu\Dropbox\DropboxApp; |
7
|
|
|
use Kunnu\Dropbox\Exceptions\DropboxClientException; |
8
|
|
|
use STS\StorageConnect\Drivers\AbstractAdapter; |
9
|
|
|
use STS\StorageConnect\Exceptions\UploadException; |
10
|
|
|
use STS\StorageConnect\Models\CloudStorage; |
11
|
|
|
use STS\StorageConnect\Types\Quota; |
12
|
|
|
|
13
|
|
|
class Adapter extends AbstractAdapter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $driver = "dropbox"; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $providerClass = Provider::class; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $user |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
protected function mapUserDetails($user) |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'name' => $user->user['name']['display_name'], |
34
|
|
|
'email' => $user->user['email'] |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return Quota |
40
|
|
|
*/ |
41
|
|
|
public function getQuota() |
42
|
|
|
{ |
43
|
|
|
$usage = $this->service()->getSpaceUsage(); |
44
|
|
|
|
45
|
|
|
return new Quota(array_get($usage, "allocation.allocated", 0), array_get($usage, "used", 0)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $sourcePath |
50
|
|
|
* @param $destinationPath |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
* @throws UploadException |
54
|
|
|
*/ |
55
|
|
|
public function upload($sourcePath, $destinationPath) |
56
|
|
|
{ |
57
|
|
|
$destinationPath = str_start($destinationPath, '/'); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
if (starts_with($sourcePath, "http")) { |
61
|
|
|
return $this->service()->saveUrl($destinationPath, $sourcePath); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->service()->upload($sourcePath, $destinationPath, [ |
65
|
|
|
'mode' => 'overwrite' |
66
|
|
|
]); |
67
|
|
|
} catch (DropboxClientException $e) { |
68
|
|
|
throw $this->handleException($e, new UploadException($sourcePath, $e)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param DropboxClientException $dropbox |
74
|
|
|
* @param UploadException $upload |
75
|
|
|
* |
76
|
|
|
* @return UploadException |
77
|
|
|
*/ |
78
|
|
|
protected function handleException(DropboxClientException $dropbox, UploadException $upload) |
79
|
|
|
{ |
80
|
|
|
// First check for connection failure |
81
|
|
|
if (str_contains($dropbox->getMessage(), "Connection timed out")) { |
82
|
|
|
return $upload->retry("Connection timeout"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// See if we have a parseable error |
86
|
|
|
$error = json_decode($dropbox->getMessage(), true); |
87
|
|
|
|
88
|
|
|
if (!is_array($error)) { |
89
|
|
|
return $upload->retry("Unknown error uploading file to Dropbox: " . $dropbox->getMessage()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (str_contains(array_get($error, 'error_summary'), "insufficient_space")) { |
93
|
|
|
return $upload->disable("Dropbox account is full", CloudStorage::SPACE_FULL); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (str_contains(array_get($error, 'error_summary'), "invalid_access_token")) { |
97
|
|
|
return $upload->disable("Dropbox integration is invalid", CloudStorage::INVALID_TOKEN); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (str_contains(array_get($error, 'error_summary'), 'too_many_write_operations')) { |
101
|
|
|
return $upload->retry("Hit rate limit"); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $upload->retry("Unknown Dropbox exception: " . $dropbox->getMessage()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Dropbox |
109
|
|
|
*/ |
110
|
|
|
protected function makeService() |
111
|
|
|
{ |
112
|
|
|
$service = new Dropbox( |
113
|
|
|
new DropboxApp($this->config['client_id'], $this->config['client_secret']), |
114
|
|
|
['random_string_generator' => 'openssl'] |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$service->setAccessToken(array_get($this->token, "access_token")); |
118
|
|
|
|
119
|
|
|
return $service; |
120
|
|
|
} |
121
|
|
|
} |