Passed
Pull Request — master (#193)
by
unknown
02:05
created

Server::setMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
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
    const TUS_EXTENSION_CREATION = 'creation';
26
27
    /** @const string Tus Termination Extension */
28
    const TUS_EXTENSION_TERMINATION = 'termination';
29
30
    /** @const string Tus Checksum Extension */
31
    const TUS_EXTENSION_CHECKSUM = 'checksum';
32
33
    /** @const string Tus Expiration Extension */
34
    const TUS_EXTENSION_EXPIRATION = 'expiration';
35
36
    /** @const string Tus Concatenation Extension */
37
    const TUS_EXTENSION_CONCATENATION = 'concatenation';
38
39
    /** @const array All supported tus extensions */
40
    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
    const HTTP_CHECKSUM_MISMATCH = 460;
50
51
    /** @const string Default checksum algorithm */
52
    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
        list($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())) {
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
        $this->event()->dispatch(
381
            UploadCreated::NAME,
382
            new UploadCreated($file, $this->getRequest(), $this->getResponse())
383
        );
384
385
        return $this->response->send(
386
            null,
387
            HttpResponse::HTTP_CREATED,
388
            [
389
                'Location' => $location,
390
                'Upload-Expires' => $this->cache->get($uploadKey)['expires_at'],
391
            ]
392
        );
393
    }
394
395
    /**
396
     * Handle file concatenation.
397
     *
398
     * @param string $fileName
399
     * @param string $filePath
400
     *
401
     * @return HttpResponse
402
     */
403
    protected function handleConcatenation(string $fileName, string $filePath) : HttpResponse
404
    {
405
        $partials  = $this->getRequest()->extractPartials();
406
        $uploadKey = $this->getUploadKey();
407
        $files     = $this->getPartialsMeta($partials);
408
        $filePaths = \array_column($files, 'file_path');
409
        $location  = $this->getRequest()->url() . $this->getApiPath() . '/' . $uploadKey;
410
411
        $file = $this->buildFile([
412
            'name' => $fileName,
413
            'offset' => 0,
414
            'size' => 0,
415
            'file_path' => $filePath,
416
            'location' => $location,
417
        ])->setFilePath($filePath)->setKey($uploadKey)->setUploadMetadata($this->getRequest()->extractAllMeta());
418
419
        $file->setOffset($file->merge($files));
420
421
        // Verify checksum.
422
        $checksum = $this->getServerChecksum($filePath);
423
424
        if ($checksum !== $this->getClientChecksum()) {
425
            return $this->response->send(null, self::HTTP_CHECKSUM_MISMATCH);
426
        }
427
428
        $file->setChecksum($checksum);
429
        $this->cache->set($uploadKey, $file->details() + ['upload_type' => self::UPLOAD_TYPE_FINAL]);
430
431
        // Cleanup.
432
        if ($file->delete($filePaths, true)) {
433
            $this->cache->deleteAll($partials);
434
        }
435
436
        $this->event()->dispatch(
437
            UploadMerged::NAME,
438
            new UploadMerged($file, $this->getRequest(), $this->getResponse())
439
        );
440
441
        return $this->response->send(
442
            ['data' => ['checksum' => $checksum]],
443
            HttpResponse::HTTP_CREATED,
444
            [
445
                'Location' => $location,
446
            ]
447
        );
448
    }
449
450
    /**
451
     * Handle PATCH request.
452
     *
453
     * @return HttpResponse
454
     */
455
    protected function handlePatch() : HttpResponse
456
    {
457
        $uploadKey = $this->request->key();
458
459
        if ( ! $meta = $this->cache->get($uploadKey)) {
460
            return $this->response->send(null, HttpResponse::HTTP_GONE);
461
        }
462
463
        $status = $this->verifyPatchRequest($meta);
464
465
        if (HttpResponse::HTTP_OK !== $status) {
466
            return $this->response->send(null, $status);
467
        }
468
469
        $file     = $this->buildFile($meta)->setUploadMetadata($meta['metadata'] ?? []);
470
        $checksum = $meta['checksum'];
471
472
        try {
473
            $fileSize = $file->getFileSize();
474
            $offset   = $file->setKey($uploadKey)->setChecksum($checksum)->upload($fileSize);
475
476
            // If upload is done, verify checksum.
477
            if ($offset === $fileSize) {
478
                if ( ! $this->verifyChecksum($checksum, $meta['file_path'])) {
479
                    return $this->response->send(null, self::HTTP_CHECKSUM_MISMATCH);
480
                }
481
482
                $this->event()->dispatch(
483
                    UploadComplete::NAME,
484
                    new UploadComplete($file, $this->getRequest(), $this->getResponse())
485
                );
486
            } else {
487
                $this->event()->dispatch(
488
                    UploadProgress::NAME,
489
                    new UploadProgress($file, $this->getRequest(), $this->getResponse())
490
                );
491
            }
492
        } catch (FileException $e) {
493
            return $this->response->send($e->getMessage(), HttpResponse::HTTP_UNPROCESSABLE_ENTITY);
494
        } catch (OutOfRangeException $e) {
495
            return $this->response->send(null, HttpResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
496
        } catch (ConnectionException $e) {
497
            return $this->response->send(null, HttpResponse::HTTP_CONTINUE);
498
        }
499
500
        return $this->response->send(null, HttpResponse::HTTP_NO_CONTENT, [
501
            'Content-Type' => self::HEADER_CONTENT_TYPE,
502
            'Upload-Expires' => $this->cache->get($uploadKey)['expires_at'],
503
            'Upload-Offset' => $offset,
504
        ]);
505
    }
506
507
    /**
508
     * Verify PATCH request.
509
     *
510
     * @param array $meta
511
     *
512
     * @return int
513
     */
514
    protected function verifyPatchRequest(array $meta) : int
515
    {
516
        if (self::UPLOAD_TYPE_FINAL === $meta['upload_type']) {
517
            return HttpResponse::HTTP_FORBIDDEN;
518
        }
519
520
        $uploadOffset = $this->request->header('upload-offset');
521
522
        if ($uploadOffset && $uploadOffset !== (string) $meta['offset']) {
523
            return HttpResponse::HTTP_CONFLICT;
524
        }
525
526
        $contentType = $this->request->header('Content-Type');
527
528
        if ($contentType !== self::HEADER_CONTENT_TYPE) {
529
            return HTTPRESPONSE::HTTP_UNSUPPORTED_MEDIA_TYPE;
530
        }
531
532
        return HttpResponse::HTTP_OK;
533
    }
534
535
    /**
536
     * Handle GET request.
537
     *
538
     * As per RFC7231, we need to treat HEAD and GET as an identical request.
539
     * All major PHP frameworks follows the same and silently transforms each
540
     * HEAD requests to GET.
541
     *
542
     * @return BinaryFileResponse|HttpResponse
543
     */
544
    protected function handleGet()
545
    {
546
        // We will treat '/files/<key>/get' as a download request.
547
        if ('get' === $this->request->key()) {
548
            return $this->handleDownload();
549
        }
550
551
        return $this->handleHead();
552
    }
553
554
    /**
555
     * Handle Download request.
556
     *
557
     * @return BinaryFileResponse|HttpResponse
558
     */
559
    protected function handleDownload()
560
    {
561
        $path = \explode('/', \str_replace('/get', '', $this->request->path()));
562
        $key  = \end($path);
563
564
        if ( ! $fileMeta = $this->cache->get($key)) {
565
            return $this->response->send('404 upload not found.', HttpResponse::HTTP_NOT_FOUND);
566
        }
567
568
        $resource = $fileMeta['file_path'] ?? null;
569
        $fileName = $fileMeta['name'] ?? null;
570
571
        if ( ! $resource || ! \file_exists($resource)) {
572
            return $this->response->send('404 upload not found.', HttpResponse::HTTP_NOT_FOUND);
573
        }
574
575
        return $this->response->download($resource, $fileName);
576
    }
577
578
    /**
579
     * Handle DELETE request.
580
     *
581
     * @return HttpResponse
582
     */
583
    protected function handleDelete() : HttpResponse
584
    {
585
        $key      = $this->request->key();
586
        $fileMeta = $this->cache->get($key);
587
        $resource = $fileMeta['file_path'] ?? null;
588
589
        if ( ! $resource) {
590
            return $this->response->send(null, HttpResponse::HTTP_NOT_FOUND);
591
        }
592
593
        $isDeleted = $this->cache->delete($key);
594
595
        if ( ! $isDeleted || ! \file_exists($resource)) {
596
            return $this->response->send(null, HttpResponse::HTTP_GONE);
597
        }
598
599
        \unlink($resource);
600
601
        return $this->response->send(null, HttpResponse::HTTP_NO_CONTENT, [
602
            'Tus-Extension' => self::TUS_EXTENSION_TERMINATION,
603
        ]);
604
    }
605
606
    /**
607
     * Get required headers for head request.
608
     *
609
     * @param array $fileMeta
610
     *
611
     * @return array
612
     */
613
    protected function getHeadersForHeadRequest(array $fileMeta) : array
614
    {
615
        $headers = [
616
            'Upload-Length' => (int) $fileMeta['size'],
617
            'Upload-Offset' => (int) $fileMeta['offset'],
618
            'Cache-Control' => 'no-store',
619
        ];
620
621
        if (self::UPLOAD_TYPE_FINAL === $fileMeta['upload_type'] && $fileMeta['size'] !== $fileMeta['offset']) {
622
            unset($headers['Upload-Offset']);
623
        }
624
625
        if (self::UPLOAD_TYPE_NORMAL !== $fileMeta['upload_type']) {
626
            $headers += ['Upload-Concat' => $fileMeta['upload_type']];
627
        }
628
629
        return $headers;
630
    }
631
632
    /**
633
     * Build file object.
634
     *
635
     * @param array $meta
636
     *
637
     * @return File
638
     */
639
    protected function buildFile(array $meta) : File
640
    {
641
        $file = new File($meta['name'], $this->cache);
642
643
        if (\array_key_exists('offset', $meta)) {
644
            $file->setMeta($meta['offset'], $meta['size'], $meta['file_path'], $meta['location']);
645
        }
646
647
        return $file;
648
    }
649
650
    /**
651
     * Get list of supported hash algorithms.
652
     *
653
     * @return string
654
     */
655
    protected function getSupportedHashAlgorithms() : string
656
    {
657
        $supportedAlgorithms = \hash_algos();
658
659
        $algorithms = [];
660
        foreach ($supportedAlgorithms as $hashAlgo) {
661
            if (false !== \strpos($hashAlgo, ',')) {
662
                $algorithms[] = "'{$hashAlgo}'";
663
            } else {
664
                $algorithms[] = $hashAlgo;
665
            }
666
        }
667
668
        return \implode(',', $algorithms);
669
    }
670
671
    /**
672
     * Verify and get upload checksum from header.
673
     *
674
     * @return string|HttpResponse
675
     */
676
    protected function getClientChecksum()
677
    {
678
        $checksumHeader = $this->getRequest()->header('Upload-Checksum');
679
680
        if (empty($checksumHeader)) {
681
            return '';
682
        }
683
684
        list($checksumAlgorithm, $checksum) = \explode(' ', $checksumHeader);
685
686
        $checksum = \base64_decode($checksum);
687
688
        if ( ! \in_array($checksumAlgorithm, \hash_algos()) || false === $checksum) {
689
            return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
690
        }
691
692
        return $checksum;
693
    }
694
695
    /**
696
     * Get expired but incomplete uploads.
697
     *
698
     * @param array|null $contents
699
     *
700
     * @return bool
701
     */
702
    protected function isExpired($contents) : bool
703
    {
704
        $isExpired = empty($contents['expires_at']) || Carbon::parse($contents['expires_at'])->lt(Carbon::now());
705
706
        if ($isExpired && $contents['offset'] !== $contents['size']) {
707
            return true;
708
        }
709
710
        return false;
711
    }
712
713
    /**
714
     * Get path for partial upload.
715
     *
716
     * @param string $key
717
     *
718
     * @return string
719
     */
720
    protected function getPathForPartialUpload(string $key) : string
721
    {
722
        list($actualKey, /* $partialUploadKey */) = \explode(self::PARTIAL_UPLOAD_NAME_SEPARATOR, $key);
723
724
        $path = $this->uploadDir . '/' . $actualKey . '/';
725
726
        if ( ! \file_exists($path)) {
727
            \mkdir($path);
728
        }
729
730
        return $path;
731
    }
732
733
    /**
734
     * Get metadata of partials.
735
     *
736
     * @param array $partials
737
     *
738
     * @return array
739
     */
740
    protected function getPartialsMeta(array $partials) : array
741
    {
742
        $files = [];
743
744
        foreach ($partials as $partial) {
745
            $fileMeta = $this->getCache()->get($partial);
746
747
            $files[] = $fileMeta;
748
        }
749
750
        return $files;
751
    }
752
753
    /**
754
     * Delete expired resources.
755
     *
756
     * @return array
757
     */
758
    public function handleExpiration() : array
759
    {
760
        $deleted   = [];
761
        $cacheKeys = $this->cache->keys();
762
763
        foreach ($cacheKeys as $key) {
764
            $fileMeta = $this->cache->get($key, true);
765
766
            if ( ! $this->isExpired($fileMeta)) {
767
                continue;
768
            }
769
770
            if ( ! $this->cache->delete($key)) {
771
                continue;
772
            }
773
774
            if (\is_writable($fileMeta['file_path'])) {
775
                \unlink($fileMeta['file_path']);
776
            }
777
778
            $deleted[] = $fileMeta;
779
        }
780
781
        return $deleted;
782
    }
783
784
    /**
785
     * Verify max upload size.
786
     *
787
     * @return bool
788
     */
789
    protected function verifyUploadSize() : bool
790
    {
791
        $maxUploadSize = $this->getMaxUploadSize();
792
793
        if ($maxUploadSize > 0 && $this->getRequest()->header('Upload-Length') > $maxUploadSize) {
794
            return false;
795
        }
796
797
        return true;
798
    }
799
800
    /**
801
     * Verify checksum if available.
802
     *
803
     * @param string $checksum
804
     * @param string $filePath
805
     *
806
     * @return bool
807
     */
808
    protected function verifyChecksum(string $checksum, string $filePath) : bool
809
    {
810
        // Skip if checksum is empty.
811
        if (empty($checksum)) {
812
            return true;
813
        }
814
815
        return $checksum === $this->getServerChecksum($filePath);
816
    }
817
818
    /**
819
     * No other methods are allowed.
820
     *
821
     * @param string $method
822
     * @param array  $params
823
     *
824
     * @return HttpResponse
825
     */
826
    public function __call(string $method, array $params)
827
    {
828
        return $this->response->send(null, HttpResponse::HTTP_BAD_REQUEST);
829
    }
830
}
831