Skip to content

Commit c6301d2

Browse files
committed
add transform
1 parent 1cf1e6d commit c6301d2

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/Concerns/InteractWithFiles.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Elegantly\Media\Concerns;
44

55
use Carbon\CarbonInterval;
6+
use Closure;
67
use DateTimeInterface;
78
use Elegantly\Media\Enums\MediaType;
89
use Elegantly\Media\Helpers\File;
10+
use Elegantly\Media\TemporaryDirectory;
911
use Illuminate\Contracts\Filesystem\Filesystem;
1012
use Illuminate\Http\File as HttpFile;
1113
use Illuminate\Http\UploadedFile;
@@ -174,6 +176,64 @@ public function moveFileTo(
174176

175177
}
176178

179+
/**
180+
* Transform the media file inside a temporary directory while keeping the same Model
181+
* Usefull to optimize or convert the media file afterwards
182+
*
183+
* @param Closure(HttpFile $copy): HttpFile $transform
184+
* @return $this
185+
*/
186+
public function transformFile(Closure $transform): static
187+
{
188+
189+
TemporaryDirectory::callback(function ($temporaryDirectory) use ($transform) {
190+
191+
/** Used to delete the old file */
192+
$clone = clone $this;
193+
194+
if (
195+
! $this->path ||
196+
! $this->disk ||
197+
! $this->name
198+
) {
199+
return $this;
200+
}
201+
202+
$storage = Storage::build([
203+
'driver' => 'local',
204+
'root' => $temporaryDirectory->path(),
205+
]);
206+
207+
$copy = $this->copyFileTo(
208+
disk: $storage,
209+
path: $this->path
210+
);
211+
212+
if (! $copy) {
213+
return;
214+
}
215+
216+
$file = $transform(new HttpFile($storage->path($copy)));
217+
218+
$result = $this->putFile(
219+
disk: $this->disk,
220+
destination: dirname($this->path),
221+
file: $file,
222+
name: $this->name
223+
);
224+
225+
if (
226+
$result &&
227+
$clone->path !== $this->path
228+
) {
229+
$clone->deleteFile();
230+
}
231+
232+
});
233+
234+
return $this;
235+
}
236+
177237
public function humanReadableSize(
178238
int $precision = 0,
179239
?int $maxPrecision = null

tests/Feature/InteractWithFilesTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

33
use Elegantly\Media\Database\Factories\MediaFactory;
4+
use Elegantly\Media\Helpers\File;
45
use Elegantly\Media\Models\Media;
6+
use Illuminate\Http\File as HttpFile;
57
use Illuminate\Http\UploadedFile;
68
use Illuminate\Support\Facades\Storage;
9+
use Spatie\Image\Image;
710

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

3033
Storage::disk('media-copy')->assertExists($copy);
3134
});
35+
36+
it('transforms a file and delete the original one', function () {
37+
/** @var Media $media */
38+
$media = MediaFactory::new()->make();
39+
40+
Storage::fake('media');
41+
42+
$file = UploadedFile::fake()->image('foo.jpg', width: 16, height: 9);
43+
44+
$media->storeFile(
45+
file: $file,
46+
disk: 'media'
47+
);
48+
49+
$path = $media->path;
50+
51+
Storage::disk('media')->assertExists($path);
52+
53+
$media->transformFile(function ($file) {
54+
55+
$path = $file->getRealPath();
56+
$basename = dirname($path);
57+
$name = File::name($path);
58+
59+
$new = "{$basename}/{$name}.png";
60+
61+
Image::load($path)
62+
->save($new);
63+
64+
return new HttpFile($new);
65+
});
66+
67+
Storage::disk('media')->assertMissing($path);
68+
69+
Storage::disk('media')->assertExists($media->path);
70+
71+
$path = $media->path;
72+
73+
$media->transformFile(function ($file) {
74+
$path = $file->getRealPath();
75+
76+
Image::load($path)
77+
->optimize()
78+
->save($path);
79+
80+
return new HttpFile($path);
81+
});
82+
83+
Storage::disk('media')->assertExists($path);
84+
85+
});

0 commit comments

Comments
 (0)