Passed
Pull Request — master (#213)
by Ankit
01:59
created

Server::getMaxUploadSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 Ramsey\Uuid\Uuid;
10
use TusPhp\Cache\Cacheable;
11
use TusPhp\Events\UploadMerged;
12
use TusPhp\Events\UploadCreated;
13
use TusPhp\Events\UploadComplete;
14
use TusPhp\Events\UploadProgress;
15
use TusPhp\Middleware\Middleware;
16
use TusPhp\Exception\FileException;
17
use TusPhp\Exception\ConnectionException;
18
use TusPhp\Exception\OutOfRangeException;
19
use Symfony\Component\HttpFoundation\BinaryFileResponse;
20
use Symfony\Component\HttpFoundation\Response as HttpResponse;
21
22
class Server extends AbstractTus
23
{
24
    /** @const string Tus Creation Extension */
25
    public const TUS_EXTENSION_CREATION = 'creation';
26
27
    /** @const string Tus Termination Extension */
28
    public const TUS_EXTENSION_TERMINATION = 'termination';
29
30
    /** @const string Tus Checksum Extension */
31
    public const TUS_EXTENSION_CHECKSUM = 'checksum';
32
33
    /** @const string Tus Expiration Extension */
34
    public const TUS_EXTENSION_EXPIRATION = 'expiration';
35
36
    /** @const string Tus Concatenation Extension */
37
    public const TUS_EXTENSION_CONCATENATION = 'concatenation';
38
39
    /** @const array All supported tus extensions */
40
    public const TUS_EXTENSIONS = [
41
        self::TUS_EXTENSION_CREATION,
42
        self::TUS_EXTENSION_TERMINATION,
43
        self::TUS_EXTENSION_CHECKSUM,
44
        self::TUS_EXTENSION_EXPIRATION,
45
        self::TUS_EXTENSION_CONCATENATION,
46
    ];
47
48
    /** @const int 460 Checksum Mismatch */
49
    private const HTTP_CHECKSUM_MISMATCH = 460;
50
51
    /** @const string Default checksum algorithm */
52
    private const DEFAULT_CHECKSUM_ALGORITHM = 'sha256';
53
54
    /** @var Request */
55
    protected $request;
56
57
    /** @var Response */
58
    protected $response;
59
60
    /** @var string */
61
    protected $uploadDir;
62
63
    /** @var string */
64
    protected $uploadKey;
65
66
    /** @var Middleware */
67
    protected $middleware;
68
69
    /**
70
     * @var int Max upload size in bytes
71
     *          Default 0, no restriction.
72
     */
73
    protected $maxUploadSize = 0;
74
75
    /**
76
     * TusServer constructor.
77
     *
78
     * @param Cacheable|string $cacheAdapter
79
     *
80
     * @throws \ReflectionException
81
     */
82
    public function __construct($cacheAdapter = 'file')
83
    {
84
        $this->request    = new Request;
85
        $this->response   = new Response;
86
        $this->middleware = new Middleware;
87
        $this->uploadDir  = \dirname(__DIR__, 2) . '/' . 'uploads';
88
89
        $this->setCache($cacheAdapter);
90
    }
91
92
    /**
93
     * Set upload dir.
94
     *
95
     * @param string $path
96
     *
97
     * @return Server
98
     */
99
    public function setUploadDir(string $path) : self
100
    {
101
        $this->uploadDir = $path;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Get upload dir.
108
     *
109
     * @return string
110
     */
111
    public function getUploadDir() : string
112
    {
113
        return $this->uploadDir;
114
    }
115
116
    /**
117
     * Get request.
118
     *
119
     * @return Request
120
     */
121
    public function getRequest() : Request
122
    {
123
        return $this->request;
124
    }
125
126
    /**
127
     * Get request.
128
     *
129
     * @return Response
130
     */
131
    public function getResponse() : Response
132
    {
133
        return $this->response;
134
    }
135
136
    /**
137
     * Get file checksum.
138
     *
139
     * @param string $filePath
140
     *
141
     * @return string
142
     */
143
    public function getServerChecksum(string $filePath) : string
144
    {
145
        return hash_file($this->getChecksumAlgorithm(), $filePath);
146
    }
147
148
    /**
149
     * Get checksum algorithm.
150
     *
151
     * @return string|null
152
     */
153
    public function getChecksumAlgorithm() : ?string
154
    {
155
        $checksumHeader = $this->getRequest()->header('Upload-Checksum');
156
157
        if (empty($checksumHeader)) {
158
            return self::DEFAULT_CHECKSUM_ALGORITHM;
159
        }
160
161
        [$checksumAlgorithm, /* $checksum */] = explode(' ', $checksumHeader);
162
163
        return $checksumAlgorithm;
164
    }
165
166
    /**
167
     * Set upload key.
168
     *
169
     * @param string $key
170
     *
171
     * @return Server
172
     */
173
    public function setUploadKey(string $key) : self
174
    {
175
        $this->uploadKey = $key;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Get upload key from header.
182
     *
183
     * @return string|HttpResponse
184
     */
185
    public function getUploadKey()
186
    {
187
        if ( ! empty($this->uploadKey)) {
188
            return $this->uploadKey;
189
        }
190
191
        $key = $this->getRequest()->header('Upload-Key') ?? Uuid::uuid4()->toString();
192
193
        if (empty($key)) {
194
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
195
        }
196
197
        $this->uploadKey = $key;
198
199
        return $this->uploadKey;
200
    }
201
202
    /**
203
     * Set middleware.
204
     *
205
     * @param Middleware $middleware
206
     *
207
     * @return self
208
     */
209
    public function setMiddleware(Middleware $middleware) : self
210
    {
211
        $this->middleware = $middleware;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Get middleware.
218
     *
219
     * @return Middleware
220
     */
221
    public function middleware() : Middleware
222
    {
223
        return $this->middleware;
224
    }
225
226
    /**
227
     * Set max upload size.
228
     *
229
     * @param int $uploadSize
230
     *
231
     * @return Server
232
     */
233
    public function setMaxUploadSize(int $uploadSize) : self
234
    {
235
        $this->maxUploadSize = $uploadSize;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Get max upload size.
242
     *
243
     * @return int
244
     */
245
    public function getMaxUploadSize() : int
246
    {
247
        return $this->maxUploadSize;
248
    }
249
250
    /**
251
     * Handle all HTTP request.
252
     *
253
     * @return HttpResponse|BinaryFileResponse
254
     */
255
    public function serve()
256
    {
257
        $this->applyMiddleware();
258
259
        $requestMethod = $this->getRequest()->method();
260
261
        if ( ! \in_array($requestMethod, $this->getRequest()->allowedHttpVerbs(), true)) {
262
            return $this->response->send(null, HttpResponse::HTTP_METHOD_NOT_ALLOWED);
263
        }
264
265
        $clientVersion = $this->getRequest()->header('Tus-Resumable');
266
267
        if ($clientVersion && $clientVersion !== self::TUS_PROTOCOL_VERSION) {
268
            return $this->response->send(null, HttpResponse::HTTP_PRECONDITION_FAILED, [
269
                'Tus-Version' => self::TUS_PROTOCOL_VERSION,
270
            ]);
271
        }
272
273
        $method = 'handle' . ucfirst(strtolower($requestMethod));
274
275
        return $this->{$method}();
276
    }
277
278
    /**
279
     * Apply middleware.
280
     *
281
     * @return void
282
     */
283
    protected function applyMiddleware()
284
    {
285
        $middleware = $this->middleware()->list();
286
287
        foreach ($middleware as $m) {
288
            $m->handle($this->getRequest(), $this->getResponse());
289
        }
290
    }
291
292
    /**
293
     * Handle OPTIONS request.
294
     *
295
     * @return HttpResponse
296
     */
297
    protected function handleOptions() : HttpResponse
298
    {
299
        $headers = [
300
            'Allow' => implode(',', $this->request->allowedHttpVerbs()),
301
            'Tus-Version' => self::TUS_PROTOCOL_VERSION,
302
            'Tus-Extension' => implode(',', self::TUS_EXTENSIONS),
303
            'Tus-Checksum-Algorithm' => $this->getSupportedHashAlgorithms(),
304
        ];
305
306
        $maxUploadSize = $this->getMaxUploadSize();
307
308
        if ($maxUploadSize > 0) {
309
            $headers['Tus-Max-Size'] = $maxUploadSize;
310
        }
311
312
        return $this->response->send(null, HttpResponse::HTTP_OK, $headers);
313
    }
314
315
    /**
316
     * Handle HEAD request.
317
     *
318
     * @return HttpResponse
319
     */
320
    protected function handleHead() : HttpResponse
321
    {
322
        $key = $this->request->key();
323
324
        if ( ! $fileMeta = $this->cache->get($key)) {
325
            return $this->response->send(null, HttpResponse::HTTP_NOT_FOUND);
326
        }
327
328
        $offset = $fileMeta['offset'] ?? false;
329
330
        if (false === $offset) {
331
            return $this->response->send(null, HttpResponse::HTTP_GONE);
332
        }
333
334
        return $this->response->send(null, HttpResponse::HTTP_OK, $this->getHeadersForHeadRequest($fileMeta));
335
    }
336
337
    /**
338
     * Handle POST request.
339
     *
340
     * @return HttpResponse
341
     */
342
    protected function handlePost() : HttpResponse
343
    {
344
        $fileName   = $this->getRequest()->extractFileName();
345
        $uploadType = self::UPLOAD_TYPE_NORMAL;
346
347
        if (empty($fileName)) {
348
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
349
        }
350
351
        if ( ! $this->verifyUploadSize()) {
352
            return $this->response->send(null, HttpResponse::HTTP_REQUEST_ENTITY_TOO_LARGE);
353
        }
354
355
        $uploadKey = $this->getUploadKey();
356
        $filePath  = $this->uploadDir . '/' . $fileName;
357
358
        if ($this->getRequest()->isFinal()) {
359
            return $this->handleConcatenation($fileName, $filePath);
360
        }
361
362
        if ($this->getRequest()->isPartial()) {
363
            $filePath   = $this->getPathForPartialUpload($uploadKey) . $fileName;
364
            $uploadType = self::UPLOAD_TYPE_PARTIAL;
365
        }
366
367
        $checksum = $this->getClientChecksum();
368
        $location = $this->getRequest()->url() . $this->getApiPath() . '/' . $uploadKey;
369
370
        $file = $this->buildFile([
371
            'name' => $fileName,
372
            'offset' => 0,
373
            'size' => $this->getRequest()->header('Upload-Length'),
374
            'file_path' => $filePath,
375
            'location' => $location,
376
        ])->setKey($uploadKey)->setChecksum($checksum)->setUploadMetadata($this->getRequest()->extractAllMeta());
377
378
        $this->cache->set($uploadKey, $file->details() + ['upload_type' => $uploadType]);
379
380
        $headers = [
381
            'Location' => $location,
382
            'Upload-Expires' => $this->cache->get($uploadKey)['expires_at'],
383
        ];
384
385
        $this->event()->dispatch(UploadCreated::NAME, new UploadCreated($file, $this->getRequest(), $this->getResponse()->setHeaders($headers)));
386
387
        return $this->response->send(null, HttpResponse::HTTP_CREATED, $headers);
388
    }
389
390
    /**
391
     * Handle file concatenation.
392
     *
393
     * @param string $fileName
394
     * @param string $filePath
395
     *
396
     * @return HttpResponse
397
     */
398
    protected function handleConcatenation(string $fileName, string $filePath) : HttpResponse
399
    {
400
        $partials  = $this->getRequest()->extractPartials();
401
        $uploadKey = $this->getUploadKey();
402
        $files     = $this->getPartialsMeta($partials);
403
        $filePaths = array_column($files, 'file_path');
404
        $location  = $this->getRequest()->url() . $this->getApiPath() . '/' . $uploadKey;
405
406
        $file = $this->buildFile([
407
            'name' => $fileName,
408
            'offset' => 0,
409
            'size' => 0,
410
            'file_path' => $filePath,
411
            'location' => $location,
412
        ])->setFilePath($filePath)->setKey($uploadKey)->setUploadMetadata($this->getRequest()->extractAllMeta());
413
414
        $file->setOffset($file->merge($files));
415
416
        // Verify checksum.
417
        $checksum = $this->getServerChecksum($filePath);
418
419
        if ($checksum !== $this->getClientChecksum()) {
420
            return $this->response->send(null, self::HTTP_CHECKSUM_MISMATCH);
421
        }
422
423
        $file->setChecksum($checksum);
424
        $this->cache->set($uploadKey, $file->details() + ['upload_type' => self::UPLOAD_TYPE_FINAL]);
425
426
        // Cleanup.
427
        if ($file->delete($filePaths, true)) {
428
            $this->cache->deleteAll($partials);
429
        }
430
431
        $this->event()->dispatch(
432
            UploadMerged::NAME,
433
            new UploadMerged($file, $this->getRequest(), $this->getResponse())
434
        );
435
436
        return $this->response->send(
437
            ['data' => ['checksum' => $checksum]],
438
            HttpResponse::HTTP_CREATED,
439
            [
440
                'Location' => $location,
441
            ]
442
        );
443
    }
444
445
    /**
446
     * Handle PATCH request.
447
     *
448
     * @return HttpResponse
449
     */
450
    protected function handlePatch() : HttpResponse
451
    {
452
        $uploadKey = $this->request->key();
453
454
        if ( ! $meta = $this->cache->get($uploadKey)) {
455
            return $this->response->send(null, HttpResponse::HTTP_GONE);
456
        }
457
458
        $status = $this->verifyPatchRequest($meta);
459
460
        if (HttpResponse::HTTP_OK !== $status) {
461
            return $this->response->send(null, $status);
462
        }
463
464
        $file     = $this->buildFile($meta)->setUploadMetadata($meta['metadata'] ?? []);
465
        $checksum = $meta['checksum'];
466
467
        try {
468
            $fileSize = $file->getFileSize();
469
            $offset   = $file->setKey($uploadKey)->setChecksum($checksum)->upload($fileSize);
470
471
            // If upload is done, verify checksum.
472
            if ($offset === $fileSize) {
473
                if ( ! $this->verifyChecksum($checksum, $meta['file_path'])) {
474
                    return $this->response->send(null, self::HTTP_CHECKSUM_MISMATCH);
475
                }
476
477
                $this->event()->dispatch(
478
                    UploadComplete::NAME,
479
                    new UploadComplete($file, $this->getRequest(), $this->getResponse())
480
                );
481
            } else {
482
                $this->event()->dispatch(
483
                    UploadProgress::NAME,
484
                    new UploadProgress($file, $this->getRequest(), $this->getResponse())
485
                );
486
            }
487
        } catch (FileException $e) {
488
            return $this->response->send($e->getMessage(), HttpResponse::HTTP_UNPROCESSABLE_ENTITY);
489
        } catch (OutOfRangeException $e) {
490
            return $this->response->send(null, HttpResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
491
        } catch (ConnectionException $e) {
492
            return $this->response->send(null, HttpResponse::HTTP_CONTINUE);
493
        }
494
495
        return $this->response->send(null, HttpResponse::HTTP_NO_CONTENT, [
496
            'Content-Type' => self::HEADER_CONTENT_TYPE,
497
            'Upload-Expires' => $this->cache->get($uploadKey)['expires_at'],
498
            'Upload-Offset' => $offset,
499
        ]);
500
    }
501
502
    /**
503
     * Verify PATCH request.
504
     *
505
     * @param array $meta
506
     *
507
     * @return int
508
     */
509
    protected function verifyPatchRequest(array $meta) : int
510
    {
511
        if (self::UPLOAD_TYPE_FINAL === $meta['upload_type']) {
512
            return HttpResponse::HTTP_FORBIDDEN;
513
        }
514
515
        $uploadOffset = $this->request->header('upload-offset');
516
517
        if ($uploadOffset && $uploadOffset !== (string) $meta['offset']) {
518
            return HttpResponse::HTTP_CONFLICT;
519
        }
520
521
        $contentType = $this->request->header('Content-Type');
522
523
        if ($contentType !== self::HEADER_CONTENT_TYPE) {
524
            return HTTPRESPONSE::HTTP_UNSUPPORTED_MEDIA_TYPE;
525
        }
526
527
        return HttpResponse::HTTP_OK;
528
    }
529
530
    /**
531
     * Handle GET request.
532
     *
533
     * As per RFC7231, we need to treat HEAD and GET as an identical request.
534
     * All major PHP frameworks follows the same and silently transforms each
535
     * HEAD requests to GET.
536
     *
537
     * @return BinaryFileResponse|HttpResponse
538
     */
539
    protected function handleGet()
540
    {
541
        // We will treat '/files/<key>/get' as a download request.
542
        if ('get' === $this->request->key()) {
543
            return $this->handleDownload();
544
        }
545
546
        return $this->handleHead();
547
    }
548
549
    /**
550
     * Handle Download request.
551
     *
552
     * @return BinaryFileResponse|HttpResponse
553
     */
554
    protected function handleDownload()
555
    {
556
        $path = explode('/', str_replace('/get', '', $this->request->path()));
557
        $key  = end($path);
558
559
        if ( ! $fileMeta = $this->cache->get($key)) {
560
            return $this->response->send('404 upload not found.', HttpResponse::HTTP_NOT_FOUND);
561
        }
562
563
        $resource = $fileMeta['file_path'] ?? null;
564
        $fileName = $fileMeta['name'] ?? null;
565
566
        if ( ! $resource || ! file_exists($resource)) {
567
            return $this->response->send('404 upload not found.', HttpResponse::HTTP_NOT_FOUND);
568
        }
569
570
        return $this->response->download($resource, $fileName);
571
    }
572
573
    /**
574
     * Handle DELETE request.
575
     *
576
     * @return HttpResponse
577
     */
578
    protected function handleDelete() : HttpResponse
579
    {
580
        $key      = $this->request->key();
581
        $fileMeta = $this->cache->get($key);
582
        $resource = $fileMeta['file_path'] ?? null;
583
584
        if ( ! $resource) {
585
            return $this->response->send(null, HttpResponse::HTTP_NOT_FOUND);
586
        }
587
588
        $isDeleted = $this->cache->delete($key);
589
590
        if ( ! $isDeleted || ! file_exists($resource)) {
591
            return $this->response->send(null, HttpResponse::HTTP_GONE);
592
        }
593
594
        unlink($resource);
595
596
        return $this->response->send(null, HttpResponse::HTTP_NO_CONTENT, [
597
            'Tus-Extension' => self::TUS_EXTENSION_TERMINATION,
598
        ]);
599
    }
600
601
    /**
602
     * Get required headers for head request.
603
     *
604
     * @param array $fileMeta
605
     *
606
     * @return array
607
     */
608
    protected function getHeadersForHeadRequest(array $fileMeta) : array
609
    {
610
        $headers = [
611
            'Upload-Length' => (int) $fileMeta['size'],
612
            'Upload-Offset' => (int) $fileMeta['offset'],
613
            'Cache-Control' => 'no-store',
614
        ];
615
616
        if (self::UPLOAD_TYPE_FINAL === $fileMeta['upload_type'] && $fileMeta['size'] !== $fileMeta['offset']) {
617
            unset($headers['Upload-Offset']);
618
        }
619
620
        if (self::UPLOAD_TYPE_NORMAL !== $fileMeta['upload_type']) {
621
            $headers += ['Upload-Concat' => $fileMeta['upload_type']];
622
        }
623
624
        return $headers;
625
    }
626
627
    /**
628
     * Build file object.
629
     *
630
     * @param array $meta
631
     *
632
     * @return File
633
     */
634
    protected function buildFile(array $meta) : File
635
    {
636
        $file = new File($meta['name'], $this->cache);
637
638
        if (\array_key_exists('offset', $meta)) {
639
            $file->setMeta($meta['offset'], $meta['size'], $meta['file_path'], $meta['location']);
640
        }
641
642
        return $file;
643
    }
644
645
    /**
646
     * Get list of supported hash algorithms.
647
     *
648
     * @return string
649
     */
650
    protected function getSupportedHashAlgorithms() : string
651
    {
652
        $supportedAlgorithms = hash_algos();
653
654
        $algorithms = [];
655
        foreach ($supportedAlgorithms as $hashAlgo) {
656
            if (false !== strpos($hashAlgo, ',')) {
657
                $algorithms[] = "'{$hashAlgo}'";
658
            } else {
659
                $algorithms[] = $hashAlgo;
660
            }
661
        }
662
663
        return implode(',', $algorithms);
664
    }
665
666
    /**
667
     * Verify and get upload checksum from header.
668
     *
669
     * @return string|HttpResponse
670
     */
671
    protected function getClientChecksum()
672
    {
673
        $checksumHeader = $this->getRequest()->header('Upload-Checksum');
674
675
        if (empty($checksumHeader)) {
676
            return '';
677
        }
678
679
        [$checksumAlgorithm, $checksum] = explode(' ', $checksumHeader);
680
681
        $checksum = base64_decode($checksum);
682
683
        if (false === $checksum || ! \in_array($checksumAlgorithm, hash_algos(), true)) {
684
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
685
        }
686
687
        return $checksum;
688
    }
689
690
    /**
691
     * Get expired but incomplete uploads.
692
     *
693
     * @param array|null $contents
694
     *
695
     * @return bool
696
     */
697
    protected function isExpired($contents) : bool
698
    {
699
        $isExpired = empty($contents['expires_at']) || Carbon::parse($contents['expires_at'])->lt(Carbon::now());
700
701
        if ($isExpired && $contents['offset'] !== $contents['size']) {
702
            return true;
703
        }
704
705
        return false;
706
    }
707
708
    /**
709
     * Get path for partial upload.
710
     *
711
     * @param string $key
712
     *
713
     * @return string
714
     */
715
    protected function getPathForPartialUpload(string $key) : string
716
    {
717
        [$actualKey, /* $partialUploadKey */] = explode(self::PARTIAL_UPLOAD_NAME_SEPARATOR, $key);
718
719
        $path = $this->uploadDir . '/' . $actualKey . '/';
720
721
        if ( ! file_exists($path)) {
722
            mkdir($path);
723
        }
724
725
        return $path;
726
    }
727
728
    /**
729
     * Get metadata of partials.
730
     *
731
     * @param array $partials
732
     *
733
     * @return array
734
     */
735
    protected function getPartialsMeta(array $partials) : array
736
    {
737
        $files = [];
738
739
        foreach ($partials as $partial) {
740
            $fileMeta = $this->getCache()->get($partial);
741
742
            $files[] = $fileMeta;
743
        }
744
745
        return $files;
746
    }
747
748
    /**
749
     * Delete expired resources.
750
     *
751
     * @return array
752
     */
753
    public function handleExpiration() : array
754
    {
755
        $deleted   = [];
756
        $cacheKeys = $this->cache->keys();
757
758
        foreach ($cacheKeys as $key) {
759
            $fileMeta = $this->cache->get($key, true);
760
761
            if ( ! $this->isExpired($fileMeta)) {
762
                continue;
763
            }
764
765
            if ( ! $this->cache->delete($key)) {
766
                continue;
767
            }
768
769
            if (is_writable($fileMeta['file_path'])) {
770
                unlink($fileMeta['file_path']);
771
            }
772
773
            $deleted[] = $fileMeta;
774
        }
775
776
        return $deleted;
777
    }
778
779
    /**
780
     * Verify max upload size.
781
     *
782
     * @return bool
783
     */
784
    protected function verifyUploadSize() : bool
785
    {
786
        $maxUploadSize = $this->getMaxUploadSize();
787
788
        if ($maxUploadSize > 0 && $this->getRequest()->header('Upload-Length') > $maxUploadSize) {
789
            return false;
790
        }
791
792
        return true;
793
    }
794
795
    /**
796
     * Verify checksum if available.
797
     *
798
     * @param string $checksum
799
     * @param string $filePath
800
     *
801
     * @return bool
802
     */
803
    protected function verifyChecksum(string $checksum, string $filePath) : bool
804
    {
805
        // Skip if checksum is empty.
806
        if (empty($checksum)) {
807
            return true;
808
        }
809
810
        return $checksum === $this->getServerChecksum($filePath);
811
    }
812
813
    /**
814
     * No other methods are allowed.
815
     *
816
     * @param string $method
817
     * @param array  $params
818
     *
819
     * @return HttpResponse
820
     */
821
    public function __call(string $method, array $params)
822
    {
823
        return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
824
    }
825
}
826