-
-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathFile.php
More file actions
568 lines (477 loc) · 11.4 KB
/
File.php
File metadata and controls
568 lines (477 loc) · 11.4 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
namespace TusPhp;
use Carbon\Carbon;
use TusPhp\Cache\Cacheable;
use TusPhp\Exception\FileException;
use TusPhp\Exception\ConnectionException;
use TusPhp\Exception\OutOfRangeException;
class File
{
/** @const Max chunk size */
public const CHUNK_SIZE = 8192; // 8 kilobytes.
/** @const Input stream */
public const INPUT_STREAM = 'php://input';
/** @const Read binary mode */
public const READ_BINARY = 'rb';
/** @const Append binary mode */
public const APPEND_BINARY = 'ab';
/** @const Read and write mode */
public const APPEND_WRITE = 'a+';
/** @var string */
protected $key;
/** @var string */
protected $checksum;
/** @var string */
protected $name;
/** @var Cacheable */
protected $cache;
/** @var int */
protected $offset;
/** @var string */
protected $location;
/** @var string */
protected $filePath;
/** @var int */
protected $fileSize;
/** @var string[] */
private $uploadMetadata = [];
/**
* File constructor.
*
* @param string|null $name
* @param Cacheable|null $cache
*/
public function __construct(?string $name = null, ?Cacheable $cache = null)
{
$this->name = $name;
$this->cache = $cache;
}
/**
* Set file meta.
*
* @param int $offset
* @param int $fileSize
* @param string $filePath
* @param string|null $location
*
* @return File
*/
public function setMeta(int $offset, int $fileSize, string $filePath, ?string $location = null): self
{
$this->offset = $offset;
$this->fileSize = $fileSize;
$this->filePath = $filePath;
$this->location = $location;
return $this;
}
/**
* Set name.
*
* @param string $name
*
* @return File
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* Get name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Set file size.
*
* @param int $size
*
* @return File
*/
public function setFileSize(int $size): self
{
$this->fileSize = $size;
return $this;
}
/**
* Get file size.
*
* @return int
*/
public function getFileSize(): int
{
return $this->fileSize;
}
/**
* Set key.
*
* @param string $key
*
* @return File
*/
public function setKey(string $key): self
{
$this->key = $key;
return $this;
}
/**
* Get key.
*
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* Set checksum.
*
* @param string $checksum
*
* @return File
*/
public function setChecksum(string $checksum): self
{
$this->checksum = $checksum;
return $this;
}
/**
* Get checksum.
*
* @return string
*/
public function getChecksum(): string
{
return $this->checksum;
}
/**
* Set offset.
*
* @param int $offset
*
* @return File
*/
public function setOffset(int $offset): self
{
$this->offset = $offset;
return $this;
}
/**
* Get offset.
*
* @return int
*/
public function getOffset(): int
{
return $this->offset;
}
/**
* Set location.
*
* @param string $location
*
* @return File
*/
public function setLocation(string $location): self
{
$this->location = $location;
return $this;
}
/**
* Get location.
*
* @return string
*/
public function getLocation(): string
{
return $this->location;
}
/**
* Set absolute file location.
*
* @param string $path
*
* @return File
*/
public function setFilePath(string $path): self
{
$this->filePath = $path;
return $this;
}
/**
* Get absolute location.
*
* @return string
*/
public function getFilePath(): string
{
return $this->filePath;
}
/**
* @param string[] $metadata
*
* @return File
*/
public function setUploadMetadata(array $metadata): self
{
$this->uploadMetadata = $metadata;
return $this;
}
/**
* Get input stream.
*
* @return string
*/
public function getInputStream(): string
{
return self::INPUT_STREAM;
}
/**
* Get file meta.
*
* @return array
*/
public function details(): array
{
$now = Carbon::now();
return [
'name' => $this->name,
'size' => $this->fileSize,
'offset' => $this->offset,
'checksum' => $this->checksum,
'location' => $this->location,
'file_path' => $this->filePath,
'metadata' => $this->uploadMetadata,
'created_at' => $now->format($this->cache::RFC_7231),
'expires_at' => $now->addSeconds($this->cache->getTtl())->format($this->cache::RFC_7231),
];
}
/**
* Upload file to server.
*
* @param int $totalBytes
*
* @throws ConnectionException
*
* @return int
*/
public function upload(int $totalBytes): int
{
if ($this->offset === $totalBytes) {
return $this->offset;
}
$input = $this->open($this->getInputStream(), self::READ_BINARY);
$output = $this->open($this->getFilePath(), self::APPEND_BINARY);
$key = $this->getKey();
try {
$this->seek($output, $this->offset);
while ( ! feof($input)) {
if (CONNECTION_NORMAL !== connection_status()) {
throw new ConnectionException('Connection aborted by user.');
}
$data = $this->read($input, self::CHUNK_SIZE);
$bytes = $this->write($output, $data, self::CHUNK_SIZE);
$this->offset += $bytes;
$this->cache->set($key, ['offset' => $this->offset]);
if ($this->offset > $totalBytes) {
throw new OutOfRangeException('The uploaded file is corrupt.');
}
if ($this->offset === $totalBytes) {
break;
}
}
} finally {
$this->close($input);
$this->close($output);
}
return $this->offset;
}
/**
* Open file in given mode.
*
* @param string $filePath
* @param string $mode
*
* @throws FileException
*
* @return resource
*/
public function open(string $filePath, string $mode)
{
$this->exists($filePath, $mode);
$ptr = @fopen($filePath, $mode);
if (false === $ptr) {
throw new FileException("Unable to open $filePath.");
}
return $ptr;
}
/**
* Check if file to read exists.
*
* @param string $filePath
* @param string $mode
*
* @throws FileException
*
* @return bool
*/
public function exists(string $filePath, string $mode = self::READ_BINARY): bool
{
if (self::INPUT_STREAM === $filePath) {
return true;
}
if (self::READ_BINARY === $mode && ! file_exists($filePath)) {
throw new FileException('File not found.');
}
return true;
}
/**
* Move file pointer to given offset.
*
* @param resource $handle
* @param int $offset
* @param int $whence
*
* @throws FileException
*
* @return int
*/
public function seek($handle, int $offset, int $whence = SEEK_SET): int
{
$position = fseek($handle, $offset, $whence);
if (-1 === $position) {
throw new FileException('Cannot move pointer to desired position.');
}
return $position;
}
/**
* Read data from file.
*
* @param resource $handle
* @param int $chunkSize
*
* @throws FileException
*
* @return string
*/
public function read($handle, int $chunkSize): string
{
$data = fread($handle, $chunkSize);
if (false === $data) {
throw new FileException('Cannot read file.');
}
return $data;
}
/**
* Write data to file.
*
* @param resource $handle
* @param string $data
* @param int|null $length
*
* @throws FileException
*
* @return int
*/
public function write($handle, string $data, $length = null): int
{
$bytesWritten = \is_int($length) ? fwrite($handle, $data, $length) : fwrite($handle, $data);
if (false === $bytesWritten) {
throw new FileException('Cannot write to a file.');
}
return $bytesWritten;
}
/**
* Merge 2 or more files.
*
* @param array $files File data with meta info.
*
* @return int
*/
public function merge(array $files): int
{
$destination = $this->getFilePath();
$firstFile = array_shift($files);
// First partial file can directly be copied.
$this->copy($firstFile['file_path'], $destination);
$this->offset = $firstFile['offset'];
$this->fileSize = filesize($firstFile['file_path']);
$handle = $this->open($destination, self::APPEND_BINARY);
foreach ($files as $file) {
if ( ! file_exists($file['file_path'])) {
throw new FileException('File to be merged not found.');
}
$this->fileSize += $this->write($handle, file_get_contents($file['file_path']));
$this->offset += $file['offset'];
}
$this->close($handle);
return $this->fileSize;
}
/**
* Copy file from source to destination.
*
* @param string $source
* @param string $destination
*
* @return bool
*/
public function copy(string $source, string $destination): bool
{
$status = @copy($source, $destination);
if (false === $status) {
throw new FileException(sprintf('Cannot copy source (%s) to destination (%s).', $source, $destination));
}
return $status;
}
/**
* Delete file and/or folder.
*
* @param array $files
* @param bool $folder
*
* @return bool
*/
public function delete(array $files, bool $folder = false): bool
{
$status = $this->deleteFiles($files);
if ($status && $folder) {
return rmdir(\dirname(current($files)));
}
return $status;
}
/**
* Delete multiple files.
*
* @param array $files
*
* @return bool
*/
public function deleteFiles(array $files): bool
{
if (empty($files)) {
return false;
}
$status = true;
foreach ($files as $file) {
if (file_exists($file)) {
$status = $status && unlink($file);
}
}
return $status;
}
/**
* Close file.
*
* @param $handle
*
* @return bool
*/
public function close($handle): bool
{
return fclose($handle);
}
}