Skip to content

Commit

Permalink
add transform
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Oct 24, 2024
1 parent 1cf1e6d commit c6301d2
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Concerns/InteractWithFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Elegantly\Media\Concerns;

use Carbon\CarbonInterval;
use Closure;
use DateTimeInterface;
use Elegantly\Media\Enums\MediaType;
use Elegantly\Media\Helpers\File;
use Elegantly\Media\TemporaryDirectory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Http\File as HttpFile;
use Illuminate\Http\UploadedFile;
Expand Down Expand Up @@ -174,6 +176,64 @@ public function moveFileTo(

}

/**
* Transform the media file inside a temporary directory while keeping the same Model
* Usefull to optimize or convert the media file afterwards
*
* @param Closure(HttpFile $copy): HttpFile $transform
* @return $this
*/
public function transformFile(Closure $transform): static
{

TemporaryDirectory::callback(function ($temporaryDirectory) use ($transform) {

/** Used to delete the old file */
$clone = clone $this;

if (
! $this->path ||
! $this->disk ||
! $this->name
) {
return $this;
}

$storage = Storage::build([
'driver' => 'local',
'root' => $temporaryDirectory->path(),
]);

$copy = $this->copyFileTo(
disk: $storage,
path: $this->path
);

if (! $copy) {
return;
}

$file = $transform(new HttpFile($storage->path($copy)));

$result = $this->putFile(
disk: $this->disk,
destination: dirname($this->path),
file: $file,
name: $this->name
);

if (
$result &&
$clone->path !== $this->path
) {
$clone->deleteFile();
}

});

return $this;
}

public function humanReadableSize(
int $precision = 0,
?int $maxPrecision = null
Expand Down
54 changes: 54 additions & 0 deletions tests/Feature/InteractWithFilesTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

use Elegantly\Media\Database\Factories\MediaFactory;
use Elegantly\Media\Helpers\File;
use Elegantly\Media\Models\Media;
use Illuminate\Http\File as HttpFile;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Spatie\Image\Image;

it('copies a file to a disk and path', function () {
/** @var Media $media */
Expand All @@ -29,3 +32,54 @@

Storage::disk('media-copy')->assertExists($copy);
});

it('transforms a file and delete the original one', function () {
/** @var Media $media */
$media = MediaFactory::new()->make();

Storage::fake('media');

$file = UploadedFile::fake()->image('foo.jpg', width: 16, height: 9);

$media->storeFile(
file: $file,
disk: 'media'
);

$path = $media->path;

Storage::disk('media')->assertExists($path);

$media->transformFile(function ($file) {

$path = $file->getRealPath();
$basename = dirname($path);
$name = File::name($path);

$new = "{$basename}/{$name}.png";

Image::load($path)
->save($new);

return new HttpFile($new);
});

Storage::disk('media')->assertMissing($path);

Storage::disk('media')->assertExists($media->path);

$path = $media->path;

$media->transformFile(function ($file) {
$path = $file->getRealPath();

Image::load($path)
->optimize()
->save($path);

return new HttpFile($path);
});

Storage::disk('media')->assertExists($path);

});

0 comments on commit c6301d2

Please sign in to comment.