-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathDropboxFile.php
336 lines (296 loc) · 7.46 KB
/
DropboxFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
namespace Kunnu\Dropbox;
use Kunnu\Dropbox\Exceptions\DropboxClientException;
/**
* DropboxFile
*/
class DropboxFile
{
const MODE_READ = 'r';
const MODE_WRITE = 'w';
/**
* Path of the file to upload
*
* @var string
*/
protected $path;
/**
* The maximum bytes to read. Defaults to -1 (read all the remaining buffer).
*
* @var int
*/
protected $maxLength = -1;
/**
* Seek to the specified offset before reading.
* If this number is negative, no seeking will
* occur and reading will start from the current.
*
* @var int
*/
protected $offset = -1;
/**
* File Stream
*
* @var \GuzzleHttp\Psr7\Stream
*/
protected $stream;
/**
* The type of access
*
* @var string
*/
protected $mode;
/**
* Flag to see if we created an instance using a stream
*
* @var bool
*/
private $isStream = false;
/**
* Create a new DropboxFile instance
*
* @param string $filePath Path of the file to upload
* @param string $mode The type of access
*/
public function __construct($filePath, $mode = self::MODE_READ)
{
$this->path = $filePath;
$this->mode = $mode;
}
/**
* Create a new DropboxFile instance using a file stream
*
* @param $fileName
* @param $resource
* @param string $mode
*
* @return DropboxFile
* @throws DropboxClientException
*/
public static function createByStream($fileName, $resource, $mode = self::MODE_READ)
{
// create a new stream and set it to the dropbox file
$stream = \GuzzleHttp\Psr7\Utils::streamFor($resource);
if (!$stream) {
throw new DropboxClientException('Failed to create DropboxFile instance. Unable to open the given resource.');
}
// Try to get the file path from the stream (we'll need this for uploading bigger files)
$filePath = $stream->getMetadata('uri');
if (!is_null($filePath)) {
$fileName = $filePath;
}
$dropboxFile = new self($fileName, $mode);
$dropboxFile->setStream($stream);
return $dropboxFile;
}
/**
* Create a new DropboxFile instance using a file path
*
* This behaves the same as the constructor but was added in order to
* match the syntax of the static createByStream function
*
* @see DropboxFile::createByStream()
*
* @param $filePath
* @param $mode
*
* @return DropboxFile
*/
public static function createByPath($filePath, $mode)
{
return new self($filePath, $mode);
}
/**
* Closes the stream when destructed.
*/
public function __destruct()
{
$this->close();
}
/**
* Close the file stream
*/
public function close()
{
if ($this->stream) {
$this->stream->close();
}
}
/**
* Set the offset to start reading
* the data from the stream
*
* @param int $offset
*/
public function setOffset($offset)
{
$this->offset = $offset;
}
/**
* Set the Max Length till where to read
* the data from the stream.
*
* @param int $maxLength
*/
public function setMaxLength($maxLength)
{
$this->maxLength = $maxLength;
}
/**
* Return the contents of the file
*
* @return string
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
*/
public function getContents()
{
$stream = $this->getStream();
// If an offset is provided
if ($this->offset !== -1) {
// Seek to the offset
$stream->seek($this->offset);
}
// If a max length is provided
if ($this->maxLength !== -1) {
// Read from the offset till the maxLength
return $stream->read($this->maxLength);
}
return $stream->getContents();
}
/**
* Get the Open File Stream
*
* @return \GuzzleHttp\Psr7\Stream
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
*/
public function getStream()
{
if (!$this->stream) {
$this->open();
}
return $this->stream;
}
/**
* Manually set the stream for this DropboxFile instance
*
* @param $stream
*/
public function setStream($stream)
{
$this->isStream = true;
$this->stream = $stream;
}
/**
* Opens the File Stream
*
* @throws DropboxClientException
*
* @return void
*/
public function open()
{
// File was created from a stream so don't open it again
if ($this->isCreatedFromStream()) {
return;
}
// File is not a remote file
if (!$this->isRemoteFile($this->path)) {
// File is not Readable
if ($this->isNotReadable()) {
throw new DropboxClientException('Failed to create DropboxFile instance. Unable to read resource: ' . $this->path . '.');
}
// File is not Writable
if ($this->isNotWritable()) {
throw new DropboxClientException('Failed to create DropboxFile instance. Unable to write resource: ' . $this->path . '.');
}
}
// Create a stream
$this->stream = \GuzzleHttp\Psr7\Utils::streamFor(fopen($this->path, $this->mode));
// Unable to create stream
if (!$this->stream) {
throw new DropboxClientException('Failed to create DropboxFile instance. Unable to open resource: ' . $this->path . '.');
}
}
/**
* @return bool
*/
protected function isCreatedFromStream()
{
return $this->stream && $this->isStream === true;
}
/**
* Returns true if the path to the file is remote
*
* @param string $pathToFile
*
* @return boolean
*/
protected function isRemoteFile($pathToFile)
{
return preg_match('/^(https?|ftp):\/\/.*/', $pathToFile) === 1;
}
/**
* @return bool
*/
protected function isNotReadable()
{
return self::MODE_READ === $this->mode && !is_readable($this->path);
}
/**
* @return bool
*/
protected function isNotWritable()
{
return self::MODE_WRITE === $this->mode && file_exists($this->path) && !is_writable($this->path);
}
/**
* Get the name of the file
*
* @return string
*/
public function getFileName()
{
return basename($this->path);
}
/**
* Get the path of the file
*
* @return string
*/
public function getFilePath()
{
return $this->path;
}
public function getStreamOrFilePath()
{
return $this->isCreatedFromStream() ? $this->getStream() : $this->getFilePath();
}
/**
* Get the mode of the file stream
*
* @return string
*/
public function getMode()
{
return $this->mode;
}
/**
* Get the size of the file
*
* @return int
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
*/
public function getSize()
{
return $this->getStream()->getSize();
}
/**
* Get mimetype of the file
*
* @return string
*/
public function getMimetype()
{
return \GuzzleHttp\Psr7\mimetype_from_filename($this->path) ?: 'text/plain';
}
}