From 0e97c0f8b8ad1f1601669cedc456e72436fe67db Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Mon, 4 Sep 2023 02:56:53 -0500 Subject: [PATCH] Adds `FileAdder@setFileSize()` (#3357) * add withFileSize; reduce repeated calls to fetching size of file * adds test for withFilesize * eyeballing style change --- src/MediaCollections/FileAdder.php | 21 +++++++++++++++---- .../MediaConversions/AddMediaTest.php | 9 ++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/MediaCollections/FileAdder.php b/src/MediaCollections/FileAdder.php index 38d90f354..4d02351e9 100644 --- a/src/MediaCollections/FileAdder.php +++ b/src/MediaCollections/FileAdder.php @@ -48,6 +48,8 @@ class FileAdder protected string $diskName = ''; + protected ?int $fileSize = null; + protected string $conversionsDiskName = ''; protected ?Closure $fileNameSanitizer; @@ -160,6 +162,13 @@ public function setFileName(string $fileName): self return $this; } + public function setFileSize(int $fileSize): self + { + $this->fileSize = $fileSize; + + return $this; + } + public function withCustomProperties(array $customProperties): self { $this->customProperties = $customProperties; @@ -229,7 +238,9 @@ public function toMediaCollectionFromRemote(string $collectionName = 'default', throw FileDoesNotExist::create($this->pathToFile); } - if ($storage->size($this->pathToFile) > config('media-library.max_file_size')) { + $this->fileSize ??= $storage->size($this->pathToFile); + + if ($this->fileSize > config('media-library.max_file_size')) { throw FileIsTooBig::create($this->pathToFile, $storage->size($this->pathToFile)); } @@ -253,7 +264,7 @@ public function toMediaCollectionFromRemote(string $collectionName = 'default', $media->collection_name = $collectionName; $media->mime_type = $storage->mimeType($this->pathToFile); - $media->size = $storage->size($this->pathToFile); + $media->size = $this->fileSize; $media->custom_properties = $this->customProperties; $media->generated_conversions = []; @@ -290,7 +301,9 @@ public function toMediaCollection(string $collectionName = 'default', string $di throw FileDoesNotExist::create($this->pathToFile); } - if (filesize($this->pathToFile) > config('media-library.max_file_size')) { + $this->fileSize ??= filesize($this->pathToFile); + + if ($this->fileSize > config('media-library.max_file_size')) { throw FileIsTooBig::create($this->pathToFile); } @@ -311,7 +324,7 @@ public function toMediaCollection(string $collectionName = 'default', string $di $media->collection_name = $collectionName; $media->mime_type = File::getMimeType($this->pathToFile); - $media->size = filesize($this->pathToFile); + $media->size = $this->fileSize; if (! is_null($this->order)) { $media->order_column = $this->order; diff --git a/tests/Feature/FileAdder/MediaConversions/AddMediaTest.php b/tests/Feature/FileAdder/MediaConversions/AddMediaTest.php index f4f2fcb10..ec96dabb0 100644 --- a/tests/Feature/FileAdder/MediaConversions/AddMediaTest.php +++ b/tests/Feature/FileAdder/MediaConversions/AddMediaTest.php @@ -131,3 +131,12 @@ public function registerMediaConversions(Media $media = null): void expect($conversionManipulations['width'])->toEqual(123); }); + +it('can set filesize', function () { + $media = $this->testModelWithoutMediaConversions + ->copyMedia($this->getTestFilesDirectory('test.jpg')) + ->setFileSize(99999) + ->toMediaCollection(); + + expect($media->size)->toEqual(99999); +});