Completed
Pull Request — master (#18)
by Ankit
02:11
created

Server::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TusPhp\Tus;
4
5
use TusPhp\File;
6
use Carbon\Carbon;
7
use TusPhp\Request;
8
use TusPhp\Response;
9
use TusPhp\Cache\Cacheable;
10
use TusPhp\Exception\FileException;
11
use TusPhp\Exception\ConnectionException;
12
use TusPhp\Exception\OutOfRangeException;
13
use Illuminate\Http\Response as HttpResponse;
14
use Symfony\Component\HttpFoundation\BinaryFileResponse;
15
16
class Server extends AbstractTus
17
{
18
    /** @const string Tus Creation Extension */
19
    const TUS_EXTENSION_CREATION = 'creation';
20
21
    /** @const string Tus Termination Extension */
22
    const TUS_EXTENSION_TERMINATION = 'termination';
23
24
    /** @const string Tus Checksum Extension */
25
    const TUS_EXTENSION_CHECKSUM = 'checksum';
26
27
    /** @const string Tus Expiration Extension */
28
    const TUS_EXTENSION_EXPIRATION = 'expiration';
29
30
    /** @const string Tus Concatenation Extension */
31
    const TUS_EXTENSION_CONCATENATION = 'concatenation';
32
33
    /** @const int 460 Checksum Mismatch */
34
    const HTTP_CHECKSUM_MISMATCH = 460;
35
36
    /** @const string Default checksum algorithm */
37
    const DEFAULT_CHECKSUM_ALGORITHM = 'sha256';
38
39
    /** @const int 24 hours access control max age header */
40
    const HEADER_ACCESS_CONTROL_MAX_AGE = 86400;
41
42
    /** @var Request */
43
    protected $request;
44
45
    /** @var Response */
46
    protected $response;
47
48
    /** @var string */
49
    protected $uploadDir;
50
51
    /**
52
     * TusServer constructor.
53
     *
54
     * @param Cacheable|string $cacheAdapter
55
     */
56 3
    public function __construct($cacheAdapter = 'file')
57
    {
58 3
        $this->request   = new Request;
59 3
        $this->response  = new Response;
60 3
        $this->uploadDir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'uploads';
61
62 3
        $this->setCache($cacheAdapter);
63 3
    }
64
65
    /**
66
     * Set upload dir.
67
     *
68
     * @param string $path
69
     *
70
     * @return void
71
     */
72 2
    public function setUploadDir(string $path)
73
    {
74 2
        $this->uploadDir = $path;
75 2
    }
76
77
    /**
78
     * Get upload dir.
79
     *
80
     * @return string
81
     */
82 1
    public function getUploadDir() : string
83
    {
84 1
        return $this->uploadDir;
85
    }
86
87
    /**
88
     * Get request.
89
     *
90
     * @return Request
91
     */
92 1
    public function getRequest() : Request
93
    {
94 1
        return $this->request;
95
    }
96
97
    /**
98
     * Get request.
99
     *
100
     * @return Response
101
     */
102 1
    public function getResponse() : Response
103
    {
104 1
        return $this->response;
105
    }
106
107
    /**
108
     * Get file checksum.
109
     *
110
     * @param string $filePath
111
     *
112
     * @return string
113
     */
114 1
    public function getServerChecksum(string $filePath)
115
    {
116 1
        return hash_file($this->getChecksumAlgorithm(), $filePath);
117
    }
118
119
    /**
120
     * Get checksum algorithm.
121
     *
122
     * @return string|null
123
     */
124 1
    public function getChecksumAlgorithm()
125
    {
126 1
        $checksumHeader = $this->getRequest()->header('Upload-Checksum');
127
128 1
        if (empty($checksumHeader)) {
129 1
            return self::DEFAULT_CHECKSUM_ALGORITHM;
130
        }
131
132 1
        list($checksumAlgorithm) = explode(' ', $checksumHeader);
133
134 1
        return $checksumAlgorithm;
135
    }
136
137
    /**
138
     * Handle all HTTP request.
139
     *
140
     * @return null|HttpResponse
141
     */
142 2
    public function serve()
143
    {
144 2
        $method = $this->getRequest()->method();
145
146 2
        if ( ! in_array($method, $this->request->allowedHttpVerbs())) {
147 1
            return $this->response->send(null, HttpResponse::HTTP_METHOD_NOT_ALLOWED);
148
        }
149
150 1
        $method = 'handle' . ucfirst(strtolower($method));
151
152 1
        $this->{$method}();
153
154 1
        $this->exit();
155 1
    }
156
157
    /**
158
     * Exit from current php process.
159
     *
160
     * @codeCoverageIgnore
161
     */
162
    protected function exit()
163
    {
164
        exit(0);
165
    }
166
167
    /**
168
     * Handle OPTIONS request.
169
     *
170
     * @return HttpResponse
171
     */
172 1
    protected function handleOptions() : HttpResponse
173
    {
174 1
        return $this->response->send(
175 1
            null,
176 1
            HttpResponse::HTTP_OK,
177
            [
178 1
                'Access-Control-Allow-Origin' => $this->request->header('Origin'),
179 1
                'Allow' => $this->request->allowedHttpVerbs(),
180 1
                'Access-Control-Allow-Methods' => $this->request->allowedHttpVerbs(),
181 1
                'Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type, Upload-Key, Upload-Checksum, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata',
182 1
                'Access-Control-Max-Age' => self::HEADER_ACCESS_CONTROL_MAX_AGE,
183 1
                'Tus-Version' => self::TUS_PROTOCOL_VERSION,
184 1
                'Tus-Extension' => implode(',', [
185 1
                    self::TUS_EXTENSION_CREATION,
186 1
                    self::TUS_EXTENSION_TERMINATION,
187 1
                    self::TUS_EXTENSION_CHECKSUM,
188 1
                    self::TUS_EXTENSION_EXPIRATION,
189 1
                    self::TUS_EXTENSION_CONCATENATION,
190
                ]),
191 1
                'Tus-Checksum-Algorithm' => $this->getSupportedHashAlgorithms(),
192
            ]
193
        );
194
    }
195
196
    /**
197
     * Handle HEAD request.
198
     *
199
     * @return HttpResponse
200
     */
201 5
    protected function handleHead() : HttpResponse
202
    {
203 5
        $key = $this->request->key();
204
205 5
        if ( ! $fileMeta = $this->cache->get($key)) {
206 1
            return $this->response->send(null, HttpResponse::HTTP_NOT_FOUND);
207
        }
208
209 4
        $offset = $fileMeta['offset'] ?? false;
210
211 4
        if (false === $offset) {
212 1
            return $this->response->send(null, HttpResponse::HTTP_GONE);
213
        }
214
215 3
        return $this->response->send(null, HttpResponse::HTTP_OK, $this->getHeadersForHeadRequest($fileMeta));
216
    }
217
218
    /**
219
     * Handle POST request.
220
     *
221
     * @return HttpResponse
222
     */
223 4
    protected function handlePost() : HttpResponse
224
    {
225 4
        $fileName   = $this->getRequest()->extractFileName();
226 4
        $uploadType = self::UPLOAD_TYPE_NORMAL;
227
228 4
        if (empty($fileName)) {
229 1
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
230
        }
231
232 3
        $uploadKey = $this->getUploadKey();
233 3
        $filePath  = $this->uploadDir . DIRECTORY_SEPARATOR . $fileName;
234
235 3
        if ($this->getRequest()->isFinal()) {
236 1
            return $this->handleConcatenation($fileName, $filePath);
237
        }
238
239 2
        if ($this->getRequest()->isPartial()) {
240 1
            $filePath   = $this->getPathForPartialUpload($uploadKey) . $fileName;
241 1
            $uploadType = self::UPLOAD_TYPE_PARTIAL;
242
        }
243
244 2
        $checksum = $this->getClientChecksum();
245 2
        $location = $this->getRequest()->url() . '/' . basename($this->uploadDir) . '/' . $fileName;
246
247 2
        $file = $this->buildFile([
248 2
            'name' => $fileName,
249 2
            'offset' => 0,
250 2
            'size' => $this->getRequest()->header('Upload-Length'),
251 2
            'file_path' => $filePath,
252 2
            'location' => $location,
253 2
        ])->setChecksum($checksum);
254
255 2
        $this->cache->set($uploadKey, $file->details() + ['upload_type' => $uploadType]);
256
257 2
        return $this->response->send(
258 2
            ['data' => ['checksum' => $checksum]],
259 2
            HttpResponse::HTTP_CREATED,
260
            [
261 2
                'Location' => $location,
262 2
                'Upload-Expires' => $this->cache->get($uploadKey)['expires_at'],
263 2
                'Tus-Resumable' => self::TUS_PROTOCOL_VERSION,
264
            ]
265
        );
266
    }
267
268
    /**
269
     * Handle file concatenation.
270
     *
271
     * @param string $fileName
272
     * @param string $filePath
273
     *
274
     * @return HttpResponse
275
     */
276 2
    protected function handleConcatenation(string $fileName, string $filePath) : HttpResponse
277
    {
278 2
        $partials  = $this->getRequest()->extractPartials();
279 2
        $files     = $this->getPartialsMeta($partials);
280 2
        $filePaths = array_column($files, 'file_path');
281 2
        $location  = $this->getRequest()->url() . '/' . basename($this->uploadDir) . '/' . $fileName;
282
283 2
        $file = $this->buildFile([
284 2
            'name' => $fileName,
285 2
            'offset' => 0,
286 2
            'size' => 0,
287 2
            'file_path' => $filePath,
288 2
            'location' => $location,
289 2
        ])->setFilePath($filePath);
290
291 2
        $file->setOffset($file->merge($files));
292
293
        // Verify checksum.
294 2
        $checksum = $this->getServerChecksum($filePath);
295
296 2
        if ($checksum !== $this->getClientChecksum()) {
297 1
            return $this->response->send(null, self::HTTP_CHECKSUM_MISMATCH);
298
        }
299
300 1
        $this->cache->set($this->getUploadKey(), $file->details() + ['upload_type' => self::UPLOAD_TYPE_FINAL]);
301
302
        // Cleanup.
303 1
        if ($file->delete($filePaths, true)) {
304 1
            $this->cache->deleteAll($partials);
305
        }
306
307 1
        return $this->response->send(
308 1
            ['data' => ['checksum' => $checksum]],
309 1
            HttpResponse::HTTP_CREATED,
310
            [
311 1
                'Location' => $location,
312 1
                'Tus-Resumable' => self::TUS_PROTOCOL_VERSION,
313
            ]
314
        );
315
    }
316
317
    /**
318
     * Handle PATCH request.
319
     *
320
     * @return HttpResponse
321
     */
322 7
    protected function handlePatch() : HttpResponse
323
    {
324 7
        $uploadKey = $this->request->key();
325
326 7
        if ( ! $meta = $this->cache->get($uploadKey)) {
327 1
            return $this->response->send(null, HttpResponse::HTTP_GONE);
328
        }
329
330 6
        if (self::UPLOAD_TYPE_FINAL === $meta['upload_type']) {
331 1
            return $this->response->send(null, HttpResponse::HTTP_FORBIDDEN);
332
        }
333
334 5
        $file     = $this->buildFile($meta);
335 5
        $checksum = $meta['checksum'];
336
337
        try {
338 5
            $fileSize = $file->getFileSize();
339 5
            $offset   = $file->setKey($uploadKey)->setChecksum($checksum)->upload($fileSize);
340
341
            // If upload is done, verify checksum.
342 2
            if ($offset === $fileSize && $checksum !== $this->getServerChecksum($meta['file_path'])) {
343 2
                return $this->response->send(null, self::HTTP_CHECKSUM_MISMATCH);
344
            }
345 3
        } catch (FileException $e) {
346 1
            return $this->response->send($e->getMessage(), HttpResponse::HTTP_UNPROCESSABLE_ENTITY);
347 2
        } catch (OutOfRangeException $e) {
348 1
            return $this->response->send(null, HttpResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
349 1
        } catch (ConnectionException $e) {
350 1
            return $this->response->send(null, HttpResponse::HTTP_CONTINUE);
351
        }
352
353 1
        return $this->response->send(null, HttpResponse::HTTP_NO_CONTENT, [
354 1
            'Upload-Expires' => $this->cache->get($uploadKey)['expires_at'],
355 1
            'Upload-Offset' => $offset,
356 1
            'Tus-Resumable' => self::TUS_PROTOCOL_VERSION,
357
        ]);
358
    }
359
360
    /**
361
     * Handle GET request.
362
     *
363
     * @return BinaryFileResponse|HttpResponse
364
     */
365 4
    protected function handleGet()
366
    {
367 4
        $key = $this->request->key();
368
369 4
        if (empty($key)) {
370 1
            return $this->response->send('400 bad request.', HttpResponse::HTTP_BAD_REQUEST);
371
        }
372
373 3
        if ( ! $fileMeta = $this->cache->get($key)) {
374 1
            return $this->response->send('404 upload not found.', HttpResponse::HTTP_NOT_FOUND);
375
        }
376
377 2
        $resource = $fileMeta['file_path'] ?? null;
378 2
        $fileName = $fileMeta['name'] ?? null;
379
380 2
        if ( ! $resource || ! file_exists($resource)) {
381 1
            return $this->response->send('404 upload not found.', HttpResponse::HTTP_NOT_FOUND);
382
        }
383
384 1
        return $this->response->download($resource, $fileName);
385
    }
386
387
    /**
388
     * Handle DELETE request.
389
     *
390
     * @return HttpResponse
391
     */
392 3
    protected function handleDelete() : HttpResponse
393
    {
394 3
        $key      = $this->request->key();
395 3
        $fileMeta = $this->cache->get($key);
396 3
        $resource = $fileMeta['file_path'] ?? null;
397
398 3
        if ( ! $resource) {
399 1
            return $this->response->send(null, HttpResponse::HTTP_NOT_FOUND);
400
        }
401
402 2
        $isDeleted = $this->cache->delete($key);
403
404 2
        if ( ! $isDeleted || ! file_exists($resource)) {
405 1
            return $this->response->send(null, HttpResponse::HTTP_GONE);
406
        }
407
408 1
        unlink($resource);
409
410 1
        return $this->response->send(null, HttpResponse::HTTP_NO_CONTENT, [
411 1
            'Tus-Resumable' => self::TUS_PROTOCOL_VERSION,
412 1
            'Tus-Extension' => self::TUS_EXTENSION_TERMINATION,
413
        ]);
414
    }
415
416
    /**
417
     * Get required headers for head request.
418
     *
419
     * @param array $fileMeta
420
     *
421
     * @return array
422
     */
423 4
    protected function getHeadersForHeadRequest(array $fileMeta) : array
424
    {
425
        $headers = [
426 4
            'Upload-Offset' => (int) $fileMeta['offset'],
427 4
            'Cache-Control' => 'no-store',
428 4
            'Tus-Resumable' => self::TUS_PROTOCOL_VERSION,
429
        ];
430
431 4
        if (self::UPLOAD_TYPE_FINAL === $fileMeta['upload_type'] && $fileMeta['size'] !== $fileMeta['offset']) {
432 2
            unset($headers['Upload-Offset']);
433
        }
434
435 4
        if (self::UPLOAD_TYPE_NORMAL !== $fileMeta['upload_type']) {
436 3
            $headers += ['Upload-Concat' => $fileMeta['upload_type']];
437
        }
438
439 4
        return $headers;
440
    }
441
442
    /**
443
     * Build file object.
444
     *
445
     * @param array $meta
446
     *
447
     * @return File
448
     */
449 1
    protected function buildFile(array $meta) : File
450
    {
451 1
        $file = new File($meta['name'], $this->cache);
452
453 1
        if (array_key_exists('offset', $meta)) {
454 1
            $file->setMeta($meta['offset'], $meta['size'], $meta['file_path'], $meta['location']);
455
        }
456
457 1
        return $file;
458
    }
459
460
    /**
461
     * Get list of supported hash algorithms.
462
     *
463
     * @return string
464
     */
465 1
    protected function getSupportedHashAlgorithms()
466
    {
467 1
        $supportedAlgorithms = hash_algos();
468
469 1
        $algorithms = [];
470 1
        foreach ($supportedAlgorithms as $hashAlgo) {
471 1
            if (false !== strpos($hashAlgo, ',')) {
472 1
                $algorithms[] = "'{$hashAlgo}'";
473
            } else {
474 1
                $algorithms[] = $hashAlgo;
475
            }
476
        }
477
478 1
        return implode(',', $algorithms);
479
    }
480
481
    /**
482
     * Get upload key from header.
483
     *
484
     * @return string|HttpResponse
485
     */
486 2
    protected function getUploadKey()
487
    {
488 2
        $key = $this->getRequest()->header('Upload-Key');
489
490 2
        if (empty($key)) {
491 1
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
492
        }
493
494 1
        return base64_decode($key);
495
    }
496
497
    /**
498
     * Verify and get upload checksum from header.
499
     *
500
     * @return string|HttpResponse
501
     */
502 4
    protected function getClientChecksum()
503
    {
504 4
        $checksumHeader = $this->getRequest()->header('Upload-Checksum');
505
506 4
        if (empty($checksumHeader)) {
507 1
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
508
        }
509
510 3
        list($checksumAlgorithm, $checksum) = explode(' ', $checksumHeader);
511
512 3
        $checksum = base64_decode($checksum);
513
514 3
        if ( ! in_array($checksumAlgorithm, hash_algos()) || false === $checksum) {
515 2
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
516
        }
517
518 1
        return $checksum;
519
    }
520
521
    /**
522
     * Get expired but incomplete uploads.
523
     *
524
     * @param array|null $contents
525
     *
526
     * @return bool
527
     */
528 3
    protected function isExpired($contents) : bool
529
    {
530 3
        $isExpired = empty($contents['expires_at']) || Carbon::parse($contents['expires_at'])->lt(Carbon::now());
531
532 3
        if ($isExpired && $contents['offset'] !== $contents['size']) {
533 3
            return true;
534
        }
535
536 2
        return false;
537
    }
538
539
    /**
540
     * Get path for partial upload.
541
     *
542
     * @param string $key
543
     *
544
     * @return string
545
     */
546 1
    protected function getPathForPartialUpload(string $key) : string
547
    {
548 1
        list($actualKey) = explode(self::PARTIAL_UPLOAD_NAME_SEPARATOR, $key);
549
550 1
        $path = $this->uploadDir . DIRECTORY_SEPARATOR . $actualKey . DIRECTORY_SEPARATOR;
551
552 1
        if ( ! file_exists($path)) {
553 1
            mkdir($path);
554
        }
555
556 1
        return $path;
557
    }
558
559
    /**
560
     * Get metadata of partials.
561
     *
562
     * @param array $partials
563
     *
564
     * @return array
565
     */
566 3
    protected function getPartialsMeta(array $partials)
567
    {
568 3
        $files = [];
569
570 3
        foreach ($partials as $partial) {
571 3
            $fileMeta = $this->getCache()->get($partial);
572
573 3
            $files[] = $fileMeta;
574
        }
575
576 3
        return $files;
577
    }
578
579
    /**
580
     * Delete expired resources.
581
     *
582
     * @return array
583
     */
584 2
    public function handleExpiration()
585
    {
586 2
        $deleted   = [];
587 2
        $cacheKeys = $this->cache->keys();
588
589 2
        foreach ($cacheKeys as $key) {
590 2
            $fileMeta = $this->cache->get($key, true);
591
592 2
            if ( ! $this->isExpired($fileMeta)) {
593 1
                continue;
594
            }
595
596 2
            if ( ! $this->cache->delete($key)) {
597 1
                continue;
598
            }
599
600 1
            if (is_writable($fileMeta['file_path'])) {
601 1
                unlink($fileMeta['file_path']);
602
            }
603
604 1
            $deleted[] = $fileMeta;
605
        }
606
607 2
        return $deleted;
608
    }
609
610
    /**
611
     * No other methods are allowed.
612
     *
613
     * @param string $method
614
     * @param array  $params
615
     *
616
     * @return HttpResponse|BinaryFileResponse
617
     */
618 1
    public function __call(string $method, array $params)
619
    {
620 1
        return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
621
    }
622
}
623