Skip to content

Commit

Permalink
better transformFile
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 13, 2024
1 parent 20cbee9 commit d1dcd47
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
18 changes: 9 additions & 9 deletions src/Concerns/InteractWithFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,14 @@ 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
* @param Closure(HttpFile $copy, \Spatie\TemporaryDirectory\TemporaryDirectory $temporaryDirectory): 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 ||
Expand All @@ -199,10 +196,10 @@ public function transformFile(Closure $transform): static
return $this;
}

$storage = Storage::build([
'driver' => 'local',
'root' => $temporaryDirectory->path(),
]);
/** Used to delete the old file at the end */
$clone = clone $this;

$storage = TemporaryDirectory::storage($temporaryDirectory);

$copy = $this->copyFileTo(
disk: $storage,
Expand All @@ -213,7 +210,10 @@ public function transformFile(Closure $transform): static
return;
}

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

$result = $this->putFile(
disk: $this->disk,
Expand Down
6 changes: 1 addition & 5 deletions src/Definitions/MediaConversionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Elegantly\Media\TemporaryDirectory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Facades\Storage;
use Spatie\TemporaryDirectory\TemporaryDirectory as SpatieTemporaryDirectory;

class MediaConversionDefinition
Expand Down Expand Up @@ -73,10 +72,7 @@ public function execute(Media $media, ?MediaConversion $parent): ?MediaConversio
{
return TemporaryDirectory::callback(function ($temporaryDirectory) use ($media, $parent) {

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

$source = $parent ?? $media;

Expand Down
10 changes: 0 additions & 10 deletions src/Helpers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use Elegantly\Media\Enums\MediaType;
use FFMpeg\Coordinate\Dimension;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Http\File as HttpFile;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File as SupportFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use Spatie\TemporaryDirectory\TemporaryDirectory;

class File
{
Expand Down Expand Up @@ -104,12 +102,4 @@ public static function extractFilename(string|HttpFile $file, ?string $name = nu

return "{$name}.{$extension}";
}

public static function makeTemporaryDisk(TemporaryDirectory $directory): Filesystem
{
return Storage::build([
'driver' => 'local',
'root' => $directory->path(),
]);
}
}
10 changes: 10 additions & 0 deletions src/TemporaryDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Elegantly\Media;

use Closure;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;
use Spatie\TemporaryDirectory\TemporaryDirectory as SpatieTemporaryDirectory;

class TemporaryDirectory extends SpatieTemporaryDirectory
Expand Down Expand Up @@ -36,4 +38,12 @@ public static function callback(
return $value;

}

public static function storage(SpatieTemporaryDirectory $temporaryDirectory): Filesystem
{
return Storage::build([
'driver' => 'local',
'root' => $temporaryDirectory->path(),
]);
}
}

0 comments on commit d1dcd47

Please sign in to comment.