From 02636ca1e643048761ba4315f7de2e65535088be Mon Sep 17 00:00:00 2001 From: allanmcarvalho Date: Fri, 22 Sep 2023 20:02:47 +0000 Subject: [PATCH] Fix styling --- src/FilePond/BeforeStore.php | 5 ++-- src/FilePond/FilePond.php | 2 +- src/FilePond/FilePondFactory.php | 9 ++---- src/FilePond/FilePondUploadedFile.php | 28 +++++++++++++------ src/FilePond/GarbageCollector.php | 3 +- src/FilePond/HasFilePondUpload.php | 4 +-- src/FilePond/ServerId.php | 16 +++++------ src/FilePond/StoreManyItem.php | 13 +++++---- .../Controllers/FilePond/ChunkController.php | 28 +++++++++++-------- .../Controllers/FilePond/DeleteController.php | 2 +- .../Controllers/FilePond/UploadController.php | 4 +-- src/LaravuewindServiceProvider.php | 1 + 12 files changed, 66 insertions(+), 49 deletions(-) diff --git a/src/FilePond/BeforeStore.php b/src/FilePond/BeforeStore.php index 93fa56d..61e95e5 100644 --- a/src/FilePond/BeforeStore.php +++ b/src/FilePond/BeforeStore.php @@ -6,9 +6,10 @@ abstract class BeforeStore { protected readonly FilePondUploadedFile $filePondUploadFile; - final public function setFilePondUploadFile (FilePondUploadedFile $filePondUploadFile): self + final public function setFilePondUploadFile(FilePondUploadedFile $filePondUploadFile): self { $this->filePondUploadFile = $filePondUploadFile; + return $this; } @@ -16,4 +17,4 @@ final public function setFilePondUploadFile (FilePondUploadedFile $filePondUploa * handle files modifications and return it's content string */ abstract public function handle(): string; -} \ No newline at end of file +} diff --git a/src/FilePond/FilePond.php b/src/FilePond/FilePond.php index ad65819..b804956 100755 --- a/src/FilePond/FilePond.php +++ b/src/FilePond/FilePond.php @@ -2,7 +2,6 @@ namespace Laravuewind\FilePond; - use Illuminate\Support\Collection; class FilePond @@ -31,6 +30,7 @@ public function getUpload(string|array|Collection $serverId, bool $alwaysCollect return FilePondUploadedFile::createFromServerId($this->factory, $this->factory->getServerId($serverId)); }) ->ensure(FilePondUploadedFile::class); + return $collection->count() > 1 || $alwaysCollection ? $collection : $collection->first(); } } diff --git a/src/FilePond/FilePondFactory.php b/src/FilePond/FilePondFactory.php index f273294..775429f 100755 --- a/src/FilePond/FilePondFactory.php +++ b/src/FilePond/FilePondFactory.php @@ -11,16 +11,12 @@ class FilePondFactory { - public function createFolderId(): string { return Str::random('32'); } /** - * @param string $folderId - * @param int $fileSize - * @return \Laravuewind\FilePond\ServerId * @throws \Exception */ public function createServerId(string $folderId, int $fileSize): ServerId @@ -43,9 +39,10 @@ public function diskName(): string { $defaultDisk = config('filesystems.default'); $disk = config('laravuewind.filepond.disk') ?? $defaultDisk; - if (!isset(config('filesystems.disks')[$disk])) { + if (! isset(config('filesystems.disks')[$disk])) { throw new Exception("Disk [$disk] not found in filesystems config"); } + return $disk; } @@ -71,8 +68,6 @@ public function getServerId(string $encrypted): ServerId } /** - * @param \Laravuewind\FilePond\ServerId $serverId - * @return bool * @throws \Exception */ public function removeUpload(ServerId $serverId): bool diff --git a/src/FilePond/FilePondUploadedFile.php b/src/FilePond/FilePondUploadedFile.php index b3b8e6b..af3fd7c 100644 --- a/src/FilePond/FilePondUploadedFile.php +++ b/src/FilePond/FilePondUploadedFile.php @@ -2,18 +2,20 @@ namespace Laravuewind\FilePond; +use const UPLOAD_ERR_OK; + use Illuminate\Http\UploadedFile; use Illuminate\Support\Collection; use Illuminate\Support\Str; -use const UPLOAD_ERR_OK; class FilePondUploadedFile extends UploadedFile { - public const EXTENDED_FILENAME_POSTFIX = "extended_file.tmp"; + public const EXTENDED_FILENAME_POSTFIX = 'extended_file.tmp'; protected ?BeforeStore $beforeStore = null; protected static Collection $registeredShutdowns; + protected static bool $removeUploadFileOnShutdownAfterStore = true; protected function __construct( @@ -34,23 +36,27 @@ protected function afterStore(false|string $result): false|string return false; } $this->setRemoveUploadFIleOnShutdown(); + return $result; } public function beforeStore(BeforeStore $beforeStore): self { $this->beforeStore = $beforeStore; + return $this; } - protected function callBeforeStore(): FilePondUploadedFile|null + protected function callBeforeStore(): ?FilePondUploadedFile { if ($this->beforeStore) { $content = $this->beforeStore ->setFilePondUploadFile($this) ->handle(); + return $this->createExtendedFilePondUploadedFile($content); } + return null; } @@ -87,8 +93,9 @@ public static function createFromServerId( public function isValid(): bool { - $isOk = UPLOAD_ERR_OK === $this->getError(); + $isOk = $this->getError() === UPLOAD_ERR_OK; $pathBegin = $this->factory->disk()->path($this->serverId->getFolderPath()); + return $isOk && str_starts_with($this->getPathname(), $pathBegin); } @@ -96,6 +103,7 @@ public function store($path = '', $options = []): false|string { $result = $this->callBeforeStore() ?->store($path, $options) ?? parent::store($path, $options); + return $this->afterStore($result); } @@ -103,6 +111,7 @@ public function storeAs($path, $name = null, $options = []): false|string { $result = $this->callBeforeStore() ?->storeAs($path, $name, $options) ?? parent::storeAs($path, $name, $options); + return $this->afterStore($result); } @@ -123,10 +132,11 @@ public function storeMany(array|Collection $items, string|array $options = []): $extendedOptions = $item->options() ?? $options; if ($item->name() && ! $extendedFile->storeAs($item->path(), $item->name(), $extendedOptions)) { $itsOk = false; - } elseif ( ! $item->name() && ! $extendedFile->store($item->path(), $extendedOptions)) { + } elseif (! $item->name() && ! $extendedFile->store($item->path(), $extendedOptions)) { $itsOk = false; } } + return $this->afterStore($itsOk); } @@ -134,6 +144,7 @@ public function storePublicly($path = '', $options = []): false|string { $result = $this->callBeforeStore() ?->storePublicly($path, $options) ?? parent::storePublicly($path, $options); + return $this->afterStore($result); } @@ -141,6 +152,7 @@ public function storePubliclyAs($path, $name = null, $options = []): false|strin { $result = $this->callBeforeStore() ?->storePubliclyAs($path, $name, $options) ?? parent::storePubliclyAs($path, $name, $options); + return $this->afterStore($result); } @@ -156,10 +168,10 @@ protected function setRemoveUploadFIleOnShutdown(): void self::$registeredShutdowns = collect(); } $folderId = $this->serverId->folderId; - if ( ! self::$registeredShutdowns->has($folderId)) { + if (! self::$registeredShutdowns->has($folderId)) { self::$registeredShutdowns->put($folderId, true); - register_shutdown_function(fn() => $this->factory->removeUpload($this->serverId)); + register_shutdown_function(fn () => $this->factory->removeUpload($this->serverId)); } } } -} \ No newline at end of file +} diff --git a/src/FilePond/GarbageCollector.php b/src/FilePond/GarbageCollector.php index ab71502..5159cbc 100644 --- a/src/FilePond/GarbageCollector.php +++ b/src/FilePond/GarbageCollector.php @@ -7,7 +7,6 @@ class GarbageCollector { - protected int $deleted = 0; public function __invoke(): void @@ -47,4 +46,4 @@ protected function collect(): void } } -} \ No newline at end of file +} diff --git a/src/FilePond/HasFilePondUpload.php b/src/FilePond/HasFilePondUpload.php index bafe616..f750c9e 100644 --- a/src/FilePond/HasFilePondUpload.php +++ b/src/FilePond/HasFilePondUpload.php @@ -14,7 +14,7 @@ trait HasFilePondUpload protected function setFilePondInput(string $input): void { $serverId = $this->input($input); - if (!is_array($serverId) && !is_string($serverId)) { + if (! is_array($serverId) && ! is_string($serverId)) { return; } elseif (is_array($serverId)) { $files = []; @@ -26,4 +26,4 @@ protected function setFilePondInput(string $input): void $this->merge([$input => FilePond::getUpload($serverId)]); } } -} \ No newline at end of file +} diff --git a/src/FilePond/ServerId.php b/src/FilePond/ServerId.php index 5bc12c8..b8e6b35 100644 --- a/src/FilePond/ServerId.php +++ b/src/FilePond/ServerId.php @@ -7,7 +7,6 @@ readonly class ServerId { - public string $folderId; public int $size; @@ -23,12 +22,12 @@ */ private function __construct( FilePondFactory $factory, - ?string $folderId = null, - ?int $size = null, - ?string $encrypted = null + string $folderId = null, + int $size = null, + string $encrypted = null ) { $this->factory = $factory; - if ( ! empty($folderId) && ! empty($size)) { + if (! empty($folderId) && ! empty($size)) { $this->folderId = $folderId; $this->size = $size; $data = [ @@ -36,7 +35,7 @@ private function __construct( 'size' => $this->size, ]; $this->encrypted = Crypt::encryptString(json_encode($data)); - } elseif ( ! empty($encrypted)) { + } elseif (! empty($encrypted)) { $this->encrypted = $encrypted; $data = json_decode(Crypt::decryptString($encrypted), true); if ($data === null || empty($data['folder']) || empty($data['size'])) { @@ -72,7 +71,8 @@ public static function create(FilePondFactory $factory, string $folderId, int $s public function getFilePath(): string { $files = collect($this->factory->disk()->files($this->getFolderPath())) - ->filter(fn(string $path) => ! str_ends_with($path, FilePondUploadedFile::EXTENDED_FILENAME_POSTFIX)); + ->filter(fn (string $path) => ! str_ends_with($path, FilePondUploadedFile::EXTENDED_FILENAME_POSTFIX)); + return match ($files->count()) { 0 => throw new Exception(sprintf( 'The upload "%s" file does not exist or has been already removed', @@ -87,4 +87,4 @@ public function getFolderPath(): string { return $this->basePath.DIRECTORY_SEPARATOR.$this->folderId.DIRECTORY_SEPARATOR; } -} \ No newline at end of file +} diff --git a/src/FilePond/StoreManyItem.php b/src/FilePond/StoreManyItem.php index 3787099..5391084 100644 --- a/src/FilePond/StoreManyItem.php +++ b/src/FilePond/StoreManyItem.php @@ -4,10 +4,9 @@ abstract class StoreManyItem { - protected readonly FilePondUploadedFile $filePondUploadFile; - private string|null $name = null; + private ?string $name = null; private string|array|null $options = null; @@ -16,7 +15,7 @@ abstract class StoreManyItem */ abstract public function handle(): string; - final public function name(): string|null + final public function name(): ?string { return $this->name; } @@ -31,20 +30,24 @@ final public function options(): string|array|null */ abstract public function path(): string; - final public function setFilePondUploadFile (FilePondUploadedFile $filePondUploadFile): self + final public function setFilePondUploadFile(FilePondUploadedFile $filePondUploadFile): self { $this->filePondUploadFile = $filePondUploadFile; + return $this; } final public function withName(string $name): self { $this->name = $name; + return $this; } + final public function withOptions(string|array $options): self { $this->options = $options; + return $this; } -} \ No newline at end of file +} diff --git a/src/Http/Controllers/FilePond/ChunkController.php b/src/Http/Controllers/FilePond/ChunkController.php index 49601c8..c669d20 100644 --- a/src/Http/Controllers/FilePond/ChunkController.php +++ b/src/Http/Controllers/FilePond/ChunkController.php @@ -15,17 +15,19 @@ class ChunkController extends BaseController { - protected Filesystem|FilesystemAdapter $disk; + protected FilePondFactory $factory; + protected Request $request; + protected ServerId $serverId; public function __construct(Request $request, FilePondFactory $factory) { $memoryLimit = config('laravuewind.filepond.memory_limit'); if (is_numeric($memoryLimit)) { - ini_set('memory_limit', (int)$memoryLimit); + ini_set('memory_limit', (int) $memoryLimit); } $this->request = $request; $this->factory = $factory; @@ -70,16 +72,19 @@ protected function getFilename(): string { $filename = $this->request->headers->get('upload-name'); abort_if( - empty($filename) || !is_string($filename), + empty($filename) || ! is_string($filename), 'No file name provided', 500, ['Content-Type' => 'text/plain'] ); + return $filename; } - protected function mergeChunk(int $carry, string $chunkFilePath, $resource): int { + protected function mergeChunk(int $carry, string $chunkFilePath, $resource): int + { fwrite($resource, $this->disk->get($chunkFilePath)); + return $carry + 1; } @@ -92,8 +97,8 @@ private function wasPersisted(): ?bool } $chunks = collect($this->disk->files($this->serverId->getFolderPath())); - $size = $chunks->sum(fn($chunk) => $this->disk->size($chunk)); - $wantedSize = (int)$this->request->headers->get('upload-length', 0); + $size = $chunks->sum(fn ($chunk) => $this->disk->size($chunk)); + $wantedSize = (int) $this->request->headers->get('upload-length', 0); if ($size < $wantedSize) { return null; @@ -102,7 +107,7 @@ private function wasPersisted(): ?bool $file = fopen($this->disk->path($tmpFilenamePath), 'w'); abort_if($file === false, 'Could not open file', 500, ['Content-Type' => 'text/plain']); $chunks = $chunks - ->mapWithKeys(fn($chunk) => [ + ->mapWithKeys(fn ($chunk) => [ str($chunk)->afterLast(DIRECTORY_SEPARATOR)->replace(['patch.', '.tmp'], '')->toInteger() => $chunk, ]) ->sortKeys(SORT_NUMERIC); @@ -114,21 +119,22 @@ private function wasPersisted(): ?bool abort_if(fclose($file) === false, 'Could not close file', 500, ['Content-Type' => 'text/plain']); abort_if( - !$this->disk->move($tmpFilenamePath, $filenamePath), + ! $this->disk->move($tmpFilenamePath, $filenamePath), 'Could not move file', 500, ['Content-Type' => 'text/plain'] ); if ($processedChunks === $chunks->count() && $this->disk->size($filenamePath) === $wantedSize) { - $chunks->each(fn($chunk) => $this->disk->delete($chunk)); + $chunks->each(fn ($chunk) => $this->disk->delete($chunk)); $this->factory->garbageCollect(); + return true; } + return false; } - public static function initChunk(Request $request, FilePondFactory $factory): Response { $folderId = $factory->createFolderId(); @@ -146,4 +152,4 @@ public static function initChunk(Request $request, FilePondFactory $factory): Re ['Content-Type' => 'text/plain'] ); } -} \ No newline at end of file +} diff --git a/src/Http/Controllers/FilePond/DeleteController.php b/src/Http/Controllers/FilePond/DeleteController.php index fa569a5..0407070 100644 --- a/src/Http/Controllers/FilePond/DeleteController.php +++ b/src/Http/Controllers/FilePond/DeleteController.php @@ -16,7 +16,7 @@ public function __invoke(Request $request, FilePondFactory $factory): Response { $memoryLimit = config('laravuewind.filepond.memory_limit'); if (is_numeric($memoryLimit)) { - ini_set('memory_limit', (int)$memoryLimit); + ini_set('memory_limit', (int) $memoryLimit); } $serverId = $factory->getServerId($request->getContent()); if ($factory->disk()->deleteDirectory($serverId->getFolderPath())) { diff --git a/src/Http/Controllers/FilePond/UploadController.php b/src/Http/Controllers/FilePond/UploadController.php index 47877a2..0fa378e 100644 --- a/src/Http/Controllers/FilePond/UploadController.php +++ b/src/Http/Controllers/FilePond/UploadController.php @@ -9,7 +9,6 @@ class UploadController extends BaseController { - /** * @throws \Exception */ @@ -17,7 +16,7 @@ public function __invoke(Request $request, FilePondFactory $factory): Response { $memoryLimit = config('laravuewind.filepond.memory_limit'); if (is_numeric($memoryLimit)) { - ini_set('memory_limit', (int)$memoryLimit); + ini_set('memory_limit', (int) $memoryLimit); } $input = $request->file('filepond'); @@ -37,6 +36,7 @@ public function __invoke(Request $request, FilePondFactory $factory): Response abort_if(! $savedFile, 500, 'Could not save file', ['Content-Type' => 'text/plain']); $factory->garbageCollect(); + return response( $factory->createServerId($folderId, (int) $request->headers->get('content-length'))->encrypted, 200, diff --git a/src/LaravuewindServiceProvider.php b/src/LaravuewindServiceProvider.php index 22d6afb..7aba9e6 100644 --- a/src/LaravuewindServiceProvider.php +++ b/src/LaravuewindServiceProvider.php @@ -33,6 +33,7 @@ public function boot(): self $this->app->bind(FilePond::class, function (Application $app) { return new FilePond($app->make(FilePondFactory::class)); }); + return parent::boot(); } }