|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Srmklive\Dropbox; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
trait UploadContent |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
|
10
|
|
|
* or if the resource size could not be determined (eg. a popen() stream). |
|
11
|
|
|
* |
|
12
|
|
|
* @param string|resource $contents |
|
13
|
|
|
* |
|
14
|
|
|
* @return bool |
|
15
|
|
|
*/ |
|
16
|
|
|
protected function shouldUploadChunk($contents) |
|
17
|
|
|
{ |
|
18
|
|
|
$size = is_string($contents) ? strlen($contents) : fstat($contents)['size']; |
|
19
|
|
|
|
|
20
|
|
|
if ($this->isPipe($contents)) { |
|
21
|
|
|
return true; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
if ($size === null) { |
|
25
|
|
|
return true; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return $size > static::MAX_CHUNK_SIZE; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Check if the contents is a pipe stream (not seekable, no size defined). |
|
33
|
|
|
* |
|
34
|
|
|
* @param string|resource $contents |
|
35
|
|
|
* |
|
36
|
|
|
* @return bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function isPipe($contents) |
|
39
|
|
|
{ |
|
40
|
|
|
return is_resource($contents) ? (fstat($contents)['mode'] & 010000) != 0 : false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Upload file split in chunks. This allows uploading large files, since |
|
45
|
|
|
* Dropbox API v2 limits the content size to 150MB. |
|
46
|
|
|
* |
|
47
|
|
|
* The chunk size will affect directly the memory usage, so be careful. |
|
48
|
|
|
* Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $path |
|
51
|
|
|
* @param string|resource $contents |
|
52
|
|
|
* @param string $mode |
|
53
|
|
|
* @param int $chunkSize |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function uploadChunk($path, $contents, $mode = 'add', $chunkSize = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$chunkSize = empty($chunkSize) ? static::MAX_CHUNK_SIZE : $chunkSize; |
|
60
|
|
|
$stream = $contents; |
|
61
|
|
|
|
|
62
|
|
|
// This method relies on resources, so we need to convert strings to resource |
|
63
|
|
|
if (is_string($contents)) { |
|
64
|
|
|
$stream = fopen('php://memory', 'r+'); |
|
65
|
|
|
fwrite($stream, $contents); |
|
66
|
|
|
rewind($stream); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$data = self::readChunk($stream, $chunkSize); |
|
|
|
|
|
|
70
|
|
|
$cursor = null; |
|
71
|
|
|
|
|
72
|
|
|
while (!((strlen($data) < $chunkSize) || feof($stream))) { |
|
73
|
|
|
// Start upload session on first iteration, then just append on subsequent iterations |
|
74
|
|
|
$cursor = isset($cursor) ? $this->appendContentToUploadSession($data, $cursor) : $this->startUploadSession($data); |
|
75
|
|
|
$data = self::readChunk($stream, $chunkSize); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// If there's no cursor here, our stream is small enough to a single request |
|
79
|
|
|
if (!isset($cursor)) { |
|
80
|
|
|
$cursor = $this->startUploadSession($data); |
|
81
|
|
|
$data = ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->finishUploadSession($data, $cursor, $path, $mode); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Upload sessions allow you to upload a single file in one or more requests, |
|
89
|
|
|
* for example where the size of the file is greater than 150 MB. |
|
90
|
|
|
* This call starts a new upload session with the given data. |
|
91
|
|
|
* |
|
92
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $contents |
|
95
|
|
|
* @param bool $close |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Srmklive\Dropbox\DropboxUploadCounter |
|
98
|
|
|
*/ |
|
99
|
|
|
public function startUploadSession($contents, $close = false) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->setupRequest( |
|
|
|
|
|
|
102
|
|
|
compact('close') |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$this->apiEndpoint = 'files/upload_session/start'; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$this->content = $contents; |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$response = json_decode( |
|
110
|
|
|
$this->doDropboxApiContentRequest()->getBody(), |
|
|
|
|
|
|
111
|
|
|
true |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
return new DropboxUploadCounter($response['session_id'], strlen($contents)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Append more data to an upload session. |
|
119
|
|
|
* When the parameter close is set, this call will close the session. |
|
120
|
|
|
* A single request should not upload more than 150 MB. |
|
121
|
|
|
* |
|
122
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $contents |
|
125
|
|
|
* @param DropboxUploadCounter $cursor |
|
126
|
|
|
* @param bool $close |
|
127
|
|
|
* |
|
128
|
|
|
* @return \Srmklive\Dropbox\DropboxUploadCounter |
|
129
|
|
|
*/ |
|
130
|
|
|
public function appendContentToUploadSession($contents, DropboxUploadCounter $cursor, $close = false) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->setupRequest(compact('cursor', 'close')); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
$this->apiEndpoint = 'files/upload_session/append_v2'; |
|
135
|
|
|
|
|
136
|
|
|
$this->content = $contents; |
|
137
|
|
|
|
|
138
|
|
|
$this->doDropboxApiContentRequest()->getBody(); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
$cursor->offset += strlen($contents); |
|
141
|
|
|
|
|
142
|
|
|
return $cursor; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Finish an upload session and save the uploaded data to the given file path. |
|
147
|
|
|
* A single request should not upload more than 150 MB. |
|
148
|
|
|
* |
|
149
|
|
|
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $contents |
|
152
|
|
|
* @param \Srmklive\Dropbox\DropboxUploadCounter $cursor |
|
153
|
|
|
* @param string $path |
|
154
|
|
|
* @param string|array $mode |
|
155
|
|
|
* @param bool $autorename |
|
156
|
|
|
* @param bool $mute |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public function finishUploadSession($contents, DropboxUploadCounter $cursor, $path, $mode = 'add', $autorename = false, $mute = false) |
|
161
|
|
|
{ |
|
162
|
|
|
$arguments = compact('cursor'); |
|
163
|
|
|
$arguments['commit'] = compact('path', 'mode', 'autorename', 'mute'); |
|
164
|
|
|
|
|
165
|
|
|
$this->setupRequest($arguments); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$this->apiEndpoint = 'files/upload_session/finish'; |
|
168
|
|
|
|
|
169
|
|
|
$this->content = $contents; |
|
170
|
|
|
|
|
171
|
|
|
$response = $this->doDropboxApiContentRequest(); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
$metadata = json_decode($response->getBody(), true); |
|
174
|
|
|
|
|
175
|
|
|
$metadata['.tag'] = 'file'; |
|
176
|
|
|
|
|
177
|
|
|
return $metadata; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Sometimes fread() returns less than the request number of bytes (for example, when reading |
|
182
|
|
|
* from network streams). This function repeatedly calls fread until the requested number of |
|
183
|
|
|
* bytes have been read or we've reached EOF. |
|
184
|
|
|
* |
|
185
|
|
|
* @param resource $stream |
|
186
|
|
|
* @param int $chunkSize |
|
187
|
|
|
* |
|
188
|
|
|
* @throws \Exception |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
protected static function readChunk($stream, $chunkSize) |
|
193
|
|
|
{ |
|
194
|
|
|
$chunk = ''; |
|
195
|
|
|
while (!feof($stream) && $chunkSize > 0) { |
|
196
|
|
|
$part = fread($stream, $chunkSize); |
|
197
|
|
|
|
|
198
|
|
|
if ($part === false) { |
|
199
|
|
|
throw new \Exception('Error reading from $stream.'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$chunk .= $part; |
|
203
|
|
|
$chunkSize -= strlen($part); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $chunk; |
|
207
|
|
|
} |
|
208
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.