|
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 Kunnu\Dropbox\Models\FileMetadata; |
|
9
|
|
|
use STS\StorageConnect\Drivers\AbstractAdapter; |
|
10
|
|
|
use STS\StorageConnect\Exceptions\UploadException; |
|
11
|
|
|
use STS\StorageConnect\Models\CloudStorage; |
|
12
|
|
|
use STS\StorageConnect\Models\Quota; |
|
13
|
|
|
use STS\StorageConnect\UploadRequest; |
|
14
|
|
|
use STS\StorageConnect\UploadResponse; |
|
15
|
|
|
|
|
16
|
|
|
class Adapter extends AbstractAdapter |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $driver = "dropbox"; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $providerClass = Provider::class; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $user |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function mapUserDetails( $user ) |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
'name' => $user->user['name']['display_name'], |
|
37
|
|
|
'email' => $user->user['email'] |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return \STS\StorageConnect\Models\Quota |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getQuota() |
|
45
|
|
|
{ |
|
46
|
|
|
$usage = $this->service()->getSpaceUsage(); |
|
47
|
|
|
|
|
48
|
|
|
return new Quota(array_get($usage, "allocation.allocated", 0), array_get($usage, "used", 0)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param UploadRequest $request |
|
53
|
|
|
* |
|
54
|
|
|
* @return UploadResponse |
|
55
|
|
|
* |
|
56
|
|
|
*/ |
|
57
|
|
|
public function upload( UploadRequest $request ) |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
|
|
if (starts_with($request->getSourcePath(), "http")) { |
|
61
|
|
|
return new UploadResponse($request, $this->service()->saveUrl($request->getDestinationPath(), $request->getSourcePath()), true); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return new UploadResponse($request, $this->service()->upload(new File($request->getSourcePath()), $request->getDestinationPath(), [ |
|
65
|
|
|
'mode' => 'overwrite' |
|
66
|
|
|
])); |
|
67
|
|
|
} catch (DropboxClientException $e) { |
|
68
|
|
|
throw $this->handleUploadException($e, new UploadException($request, $e)); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param DropboxClientException $dropbox |
|
74
|
|
|
* @param UploadException $upload |
|
75
|
|
|
* |
|
76
|
|
|
* @return UploadException |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function handleUploadException( 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
|
|
|
* @param UploadResponse $response |
|
109
|
|
|
* |
|
110
|
|
|
* @return UploadResponse |
|
111
|
|
|
*/ |
|
112
|
|
|
public function checkUploadStatus( UploadResponse $response ) |
|
113
|
|
|
{ |
|
114
|
|
|
$result = $this->service()->checkJobStatus($response->getOriginal()); |
|
115
|
|
|
|
|
116
|
|
|
if($response instanceof FileMetadata) { |
|
117
|
|
|
return new UploadResponse( $response->getRequest(), $result ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if($response == "in_progress") { |
|
121
|
|
|
$response->incrementStatusCheck(); |
|
122
|
|
|
return $response; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return Dropbox |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function makeService() |
|
132
|
|
|
{ |
|
133
|
|
|
$service = new Dropbox( |
|
134
|
|
|
new DropboxApp($this->config['client_id'], $this->config['client_secret']), |
|
135
|
|
|
['random_string_generator' => 'openssl'] |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
$service->setAccessToken(array_get($this->token, "access_token")); |
|
139
|
|
|
|
|
140
|
|
|
return $service; |
|
141
|
|
|
} |
|
142
|
|
|
} |