|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodingSocks\ChunkUploader\Driver; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use CodingSocks\ChunkUploader\Helper\ChunkHelpers; |
| 7 | +use CodingSocks\ChunkUploader\Identifier\Identifier; |
| 8 | +use CodingSocks\ChunkUploader\Range\PluploadRange; |
| 9 | +use CodingSocks\ChunkUploader\Response\PercentageJsonResponse; |
| 10 | +use CodingSocks\ChunkUploader\StorageConfig; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use Illuminate\Http\UploadedFile; |
| 13 | +use InvalidArgumentException; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 16 | +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
| 17 | + |
| 18 | +class PluploadUploadDriver extends UploadDriver |
| 19 | +{ |
| 20 | + use ChunkHelpers; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \CodingSocks\ChunkUploader\Identifier\Identifier |
| 24 | + */ |
| 25 | + private $identifier; |
| 26 | + |
| 27 | + /** |
| 28 | + * PluploadUploadDriver constructor. |
| 29 | + * |
| 30 | + * @param \CodingSocks\ChunkUploader\Identifier\Identifier $identifier |
| 31 | + */ |
| 32 | + public function __construct(Identifier $identifier) |
| 33 | + { |
| 34 | + $this->identifier = $identifier; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritDoc |
| 40 | + */ |
| 41 | + public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response |
| 42 | + { |
| 43 | + if ($this->isRequestMethodIn($request, [Request::METHOD_POST])) { |
| 44 | + return $this->save($request, $config, $fileUploaded); |
| 45 | + } |
| 46 | + |
| 47 | + throw new MethodNotAllowedHttpException([ |
| 48 | + Request::METHOD_POST, |
| 49 | + ]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param \Illuminate\Http\Request $request |
| 54 | + * @param \CodingSocks\ChunkUploader\StorageConfig $config |
| 55 | + * @param \Closure|null $fileUploaded |
| 56 | + * |
| 57 | + * @return mixed |
| 58 | + */ |
| 59 | + private function save(Request $request, StorageConfig $config, ?Closure $fileUploaded) |
| 60 | + { |
| 61 | + $file = $request->file('file'); |
| 62 | + |
| 63 | + $this->validateUploadedFile($file); |
| 64 | + |
| 65 | + $this->validateChunkRequest($request); |
| 66 | + |
| 67 | + return $this->saveChunk($file, $request, $config, $fileUploaded); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param \Illuminate\Http\Request $request |
| 72 | + */ |
| 73 | + private function validateChunkRequest(Request $request): void |
| 74 | + { |
| 75 | + $request->validate([ |
| 76 | + 'name' => 'required', |
| 77 | + 'chunk' => 'required|integer', |
| 78 | + 'chunks' => 'required|integer', |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param \Illuminate\Http\UploadedFile $file |
| 84 | + * @param \Illuminate\Http\Request $request |
| 85 | + * @param \CodingSocks\ChunkUploader\StorageConfig $config |
| 86 | + * @param \Closure|null $fileUploaded |
| 87 | + * |
| 88 | + * @return \Symfony\Component\HttpFoundation\Response |
| 89 | + */ |
| 90 | + private function saveChunk(UploadedFile $file, Request $request, StorageConfig $config, Closure $fileUploaded = null): Response |
| 91 | + { |
| 92 | + try { |
| 93 | + $range = new PluploadRange($request); |
| 94 | + } catch (InvalidArgumentException $e) { |
| 95 | + throw new BadRequestHttpException($e->getMessage(), $e); |
| 96 | + } |
| 97 | + |
| 98 | + $uuid = $this->identifier->generateFileIdentifier($range->getTotal(), $file->getClientOriginalName()); |
| 99 | + |
| 100 | + $chunks = $this->storeChunk($config, $range, $file, $uuid); |
| 101 | + |
| 102 | + if (!$range->isLast()) { |
| 103 | + return new PercentageJsonResponse($range->getPercentage()); |
| 104 | + } |
| 105 | + |
| 106 | + $targetFilename = $file->hashName(); |
| 107 | + |
| 108 | + $path = $this->mergeChunks($config, $chunks, $targetFilename); |
| 109 | + |
| 110 | + if ($config->sweep()) { |
| 111 | + $this->deleteChunkDirectory($config, $uuid); |
| 112 | + } |
| 113 | + |
| 114 | + $this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded); |
| 115 | + |
| 116 | + return new PercentageJsonResponse($range->getPercentage()); |
| 117 | + } |
| 118 | +} |
0 commit comments