diff --git a/src/Transformers/AssetsTransformer.php b/src/Transformers/AssetsTransformer.php index 98c4ced..26aea69 100644 --- a/src/Transformers/AssetsTransformer.php +++ b/src/Transformers/AssetsTransformer.php @@ -99,7 +99,7 @@ private function assetPath(AssetContainerContract $assetContainer, string $path) private function processAssetUsingSourcePreset($asset, string $path, string $contents): void { - Storage::disk('local')->put($tempPath = 'statamic/temp-assets/'.uniqid().'.'.Str::afterLast($path, '.'), $contents); + Storage::disk('local')->put($tempPath = 'statamic/temp-assets/'.Str::random().'.'.Str::afterLast($path, '.'), $contents); $uploadedFile = new UploadedFile( path: Storage::disk('local')->path($tempPath), diff --git a/tests/Transformers/AssetsTransformerTest.php b/tests/Transformers/AssetsTransformerTest.php index d8e5cb1..c8239fe 100644 --- a/tests/Transformers/AssetsTransformerTest.php +++ b/tests/Transformers/AssetsTransformerTest.php @@ -3,14 +3,18 @@ namespace Statamic\Importer\Tests\Transformers; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; +use Mockery; use PHPUnit\Framework\Attributes\Test; use Statamic\Facades\AssetContainer; use Statamic\Facades\Collection; +use Statamic\Facades\Glide; use Statamic\Importer\Facades\Import; use Statamic\Importer\Tests\TestCase; use Statamic\Importer\Transformers\AssetsTransformer; +use Statamic\Support\Str; use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk; class AssetsTransformerTest extends TestCase @@ -32,7 +36,7 @@ protected function setUp(): void $this->collection = tap(Collection::make('pages'))->save(); $this->blueprint = $this->collection->entryBlueprint(); - $this->blueprint->ensureField('featured_image', ['type' => 'assets', 'max_files' => 1])->save(); + $this->blueprint->ensureField('featured_image', ['type' => 'assets', 'container' => 'assets', 'max_files' => 1])->save(); $this->field = $this->blueprint->field('featured_image'); @@ -228,4 +232,42 @@ public function it_sets_alt_text_on_downloaded_asset() $asset = $this->assetContainer->asset('2024/10/image.png'); $this->assertEquals('A photo taken by someone.', $asset->get('alt')); } + + #[Test] + public function it_processes_asset_using_source_preset() + { + Http::fake([ + 'https://example.com/wp-content/uploads/2024/10/image.png' => Http::response(UploadedFile::fake()->image('image.png')->size(100)->get()), + ]); + + Str::createRandomStringsUsing(fn () => 'temp'); + + File::ensureDirectoryExists(storage_path('statamic/glide/tmp')); + File::put(storage_path('statamic/glide/tmp/temp.png/random/temp.png'), 'Transformed Image'); + + Glide::shouldReceive('server')->once()->andReturnSelf(); + Glide::shouldReceive('makeImage')->once()->with('temp.png', ['p' => 'thumbnail'])->andReturn('temp.png/random/temp.png'); + + $this->assetContainer->disk('public')->sourcePreset('thumbnail')->save(); + + $transformer = new AssetsTransformer( + import: $this->import, + blueprint: $this->blueprint, + field: $this->field, + config: [ + 'related_field' => 'url', + 'base_url' => 'https://example.com/wp-content/uploads', + 'download_when_missing' => true, + 'process_downloaded_images' => true, + ], + ); + + $output = $transformer->transform('https://example.com/wp-content/uploads/2024/10/image.png'); + + $this->assertEquals('2024/10/image.png', $output); + + Storage::disk('public')->assertExists('2024/10/image.png'); + + $this->assertFileDoesNotExist(storage_path('statamic/glide/tmp/temp.png/random/temp.png')); + } }