1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\FlysystemDropbox; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
class DropboxClient |
9
|
|
|
{ |
10
|
|
|
protected $accessToken; |
11
|
|
|
|
12
|
|
|
protected $client; |
13
|
|
|
|
14
|
|
|
public function __construct(string $accessToken) |
15
|
|
|
{ |
16
|
|
|
$this->accessToken = $accessToken; |
17
|
|
|
|
18
|
|
|
$this->client = new Client([ |
19
|
|
|
'base_uri' => 'https://api.dropboxapi.com/2/', |
20
|
|
|
'headers' => [ |
21
|
|
|
'Authorization' => "Bearer {$this->accessToken}", |
22
|
|
|
], |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function move(string $path, string $newPath): array |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$path = $this->normalizePath($path); |
29
|
|
|
$newPath = $this->normalizePath($newPath); |
30
|
|
|
|
31
|
|
|
$response = $this->client->post('files/move', [ |
32
|
|
|
'json' => [ |
33
|
|
|
'from_path' => $path, |
34
|
|
|
'to_path' => $newPath, |
35
|
|
|
] |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
return json_decode($response->getBody(), true); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function copy(string $path, string $newPath): array |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$path = $this->normalizePath($path); |
44
|
|
|
$newPath = $this->normalizePath($newPath); |
45
|
|
|
|
46
|
|
|
$response = $this->client->post('files/copy', [ |
47
|
|
|
'json' => [ |
48
|
|
|
'from_path' => $path, |
49
|
|
|
'to_path' => $newPath, |
50
|
|
|
] |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
return json_decode($response->getBody(), true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function delete(string $path): array |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$path = $this->normalizePath($path); |
59
|
|
|
|
60
|
|
|
$response = $this->client->post('files/delete', [ |
61
|
|
|
'json' => [ |
62
|
|
|
'path' => $path, |
63
|
|
|
] |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
return json_decode($response->getBody(), true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $path |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function createFolder(string $path) |
75
|
|
|
{ |
76
|
|
|
$path = $this->normalizePath($path); |
77
|
|
|
|
78
|
|
|
$response = $this->client->post('files/create_folder', [ |
79
|
|
|
'json' => [ |
80
|
|
|
'path' => $path, |
81
|
|
|
] |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$result = json_decode($response->getBody(), true); |
85
|
|
|
|
86
|
|
|
$result['.tag'] = 'folder'; |
87
|
|
|
|
88
|
|
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function getMetadata(string $path) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$path = $this->normalizePath($path); |
94
|
|
|
|
95
|
|
|
$response = $this->client->post('files/get_metadata', [ |
96
|
|
|
'json' => [ |
97
|
|
|
'path' => $path, |
98
|
|
|
] |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
return json_decode($response->getBody(), true); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function listContents($path = '', $recursive = false): array |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$path = $this->normalizePath($path); |
107
|
|
|
|
108
|
|
|
$response = $this->client->post('files/list_folder', [ |
109
|
|
|
'json' => [ |
110
|
|
|
'path' => $path, |
111
|
|
|
'recursive' => $recursive, |
112
|
|
|
] |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
return json_decode($response->getBody(), true); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function getTemporaryLink(string $path): string |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$path = $this->normalizePath($path); |
121
|
|
|
|
122
|
|
|
$response = $this->client->post('files/get_temporary_link', [ |
123
|
|
|
'json' => [ |
124
|
|
|
'path' => $path, |
125
|
|
|
], |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
$body = json_decode($response->getBody(), true); |
129
|
|
|
|
130
|
|
|
return $body['link']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
134
|
|
|
{ |
135
|
|
|
$dropboxApiArguments = [ |
136
|
|
|
'path' => $this->normalizePath($path), |
137
|
|
|
'format' => $format, |
138
|
|
|
'size' => $size |
139
|
|
|
]; |
140
|
|
|
|
141
|
|
|
$response = $this->client->post('https://content.dropboxapi.com/2/files/get_thumbnail', [ |
142
|
|
|
'headers' => [ |
143
|
|
|
'Dropbox-API-Arg' => json_encode($dropboxApiArguments), |
144
|
|
|
], |
145
|
|
|
]); |
146
|
|
|
|
147
|
|
|
return (string) $response->getBody(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function uploadFromString(string $path, string $mode, string $contents) |
151
|
|
|
{ |
152
|
|
|
$dropboxApiArguments = [ |
153
|
|
|
'path' => $this->normalizePath($path), |
154
|
|
|
'mode' => $mode, |
155
|
|
|
'autorename' => true, |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$response = $this->client->post('https://content.dropboxapi.com/2/files/upload', [ |
159
|
|
|
'headers' => [ |
160
|
|
|
'Dropbox-API-Arg' => json_encode($dropboxApiArguments), |
161
|
|
|
'Content-Type' => 'application/octet-stream', |
162
|
|
|
], |
163
|
|
|
'body' => $contents, |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
$metadata = json_decode($response->getBody(), true); |
167
|
|
|
|
168
|
|
|
$metadata['.tag'] = 'file'; |
169
|
|
|
|
170
|
|
|
return $metadata; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function normalizePath(string $path): string |
174
|
|
|
{ |
175
|
|
|
$path = trim($path,'/'); |
176
|
|
|
|
177
|
|
|
if ($path === '') { |
178
|
|
|
return ''; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return '/'.$path; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
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.