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
|
|
|
public function listContents($directory = '', $recursive = false): array |
57
|
|
|
{ |
58
|
|
|
$directory = $directory === '/' ? '' : $directory; |
59
|
|
|
|
60
|
|
|
$response = $this->client->post('files/list_folder', [ |
61
|
|
|
'json' => [ |
62
|
|
|
'path' => $directory, |
63
|
|
|
'recursive' => $recursive, |
64
|
|
|
] |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
return json_decode($response->getBody(), true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getTemporaryLink(string $path): string |
71
|
|
|
{ |
72
|
|
|
$path = $this->normalizePath($path); |
73
|
|
|
|
74
|
|
|
$response = $this->client->post('files/get_temporary_link', [ |
75
|
|
|
'json' => [ |
76
|
|
|
'path' => $path, |
77
|
|
|
], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
$body = json_decode($response->getBody(), true); |
81
|
|
|
|
82
|
|
|
return $body['link']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
86
|
|
|
{ |
87
|
|
|
$dropboxApiArguments = [ |
88
|
|
|
'path' => $this->normalizePath($path), |
89
|
|
|
'format' => $format, |
90
|
|
|
'size' => $size |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$response = $this->client->post('https://content.dropboxapi.com/2/files/get_thumbnail', [ |
94
|
|
|
'headers' => [ |
95
|
|
|
'Dropbox-API-Arg' => json_encode($dropboxApiArguments), |
96
|
|
|
], |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
return (string) $response->getBody(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function uploadFromString(string $path, string $mode, string $contents) |
103
|
|
|
{ |
104
|
|
|
$dropboxApiArguments = [ |
105
|
|
|
'path' => $this->normalizePath($path), |
106
|
|
|
'mode' => $mode, |
107
|
|
|
'autorename' => true, |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
$response = $this->client->post('https://content.dropboxapi.com/2/files/upload', [ |
111
|
|
|
'headers' => [ |
112
|
|
|
'Dropbox-API-Arg' => json_encode($dropboxApiArguments), |
113
|
|
|
'Content-Type' => 'application/octet-stream', |
114
|
|
|
], |
115
|
|
|
'body' => $contents, |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$metadata = json_decode($response->getBody(), true); |
119
|
|
|
|
120
|
|
|
$metadata['.tag'] = 'file'; |
121
|
|
|
|
122
|
|
|
return $metadata; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function normalizePath(string $path): string |
126
|
|
|
{ |
127
|
|
|
$path = trim($path,'/'); |
128
|
|
|
|
129
|
|
|
if ($path === '') { |
130
|
|
|
return ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return '/'.$path; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.