Skip to content

Commit

Permalink
Adds FileAdder@setFileSize() (#3357)
Browse files Browse the repository at this point in the history
* add withFileSize; reduce repeated calls to fetching size of file

* adds test for withFilesize

* eyeballing style change
  • Loading branch information
cosmastech authored Sep 4, 2023
1 parent d696b05 commit 0e97c0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/MediaCollections/FileAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class FileAdder

protected string $diskName = '';

protected ?int $fileSize = null;

protected string $conversionsDiskName = '';

protected ?Closure $fileNameSanitizer;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand All @@ -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 = [];
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/FileAdder/MediaConversions/AddMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 0e97c0f

Please sign in to comment.