diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index a432150acbb..cb3b116c919 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -754,11 +754,7 @@ public function containerHandle() */ public function rename($filename, $unique = false) { - if ($unique) { - return $this->moveUnique($this->folder(), $filename); - } - - return $this->move($this->folder(), $filename); + return $this->move($this->folder(), $filename, $unique); } /** @@ -766,11 +762,13 @@ public function rename($filename, $unique = false) * * @param string $folder The folder relative to the container. * @param string|null $filename The new filename, if renaming. + * @param bool $unique Whether to ensure the filename is unique. * @return $this */ - public function move($folder, $filename = null) + public function move($folder, $filename = null, $unique = false) { $filename = Uploader::getSafeFilename($filename ?: $this->filename()); + $filename = $unique ? $this->ensureUniqueFilename($folder, $filename) : $filename; $oldPath = $this->path(); $oldMetaPath = $this->metaPath(); $newPath = Str::removeLeft(Path::tidy($folder.'/'.$filename.'.'.pathinfo($oldPath, PATHINFO_EXTENSION)), '/'); @@ -789,22 +787,7 @@ public function move($folder, $filename = null) return $this; } - /** - * Move the asset to a different location with a unique filename. - * - * @param string $folder The folder relative to the container. - * @param string|null $filename The new filename, if renaming. - * @return $this - */ - public function moveUnique($folder, $filename = null) - { - $filename = Uploader::getSafeFilename($filename ?: $this->filename()); - $filename = $this->ensureUniqueFilename($folder, $filename); - - return $this->move($folder, $filename); - } - - public function moveQuietly($folder, $filename = null) + public function moveQuietly($folder, $filename = null, $unique = false) { $this->withEvents = false; diff --git a/src/Imaging/ImageGenerator.php b/src/Imaging/ImageGenerator.php index 99116c6fd06..cfeef6a0115 100644 --- a/src/Imaging/ImageGenerator.php +++ b/src/Imaging/ImageGenerator.php @@ -3,6 +3,7 @@ namespace Statamic\Imaging; use Facades\Statamic\Imaging\ImageValidator; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use League\Flysystem\Filesystem; use League\Flysystem\UnableToReadFile; @@ -150,11 +151,17 @@ public function generateVideoThumbnail($asset, array $params) /** * Generate a manipulated image by an asset. * - * @param \Statamic\Contracts\Assets\Asset $asset + * @param \Statamic\Contracts\Assets\Asset|null $asset * @return mixed */ public function generateByAsset($asset, array $params) { + if (! $asset) { + Log::error('Cannot generate an image for a missing asset.'); + + return ''; + } + if ($asset->isVideo() && ThumbnailExtractor::available()) { return $this->generateVideoThumbnail($asset, $params); } diff --git a/tests/Assets/AssetTest.php b/tests/Assets/AssetTest.php index 9e2733134c1..cbf34bebf61 100644 --- a/tests/Assets/AssetTest.php +++ b/tests/Assets/AssetTest.php @@ -1321,7 +1321,7 @@ public function it_doesnt_lowercase_moved_files_when_configured() } #[Test] - public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists() + public function it_can_be_moved_to_another_folder_with_a_unique_filename_when_conflict_exists() { Storage::fake('local'); $disk = Storage::disk('local'); @@ -1334,7 +1334,7 @@ public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists( $asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']); $asset->save(); - $return = $asset->moveUnique('new'); + $return = $asset->move('new', null, true); $this->assertEquals($asset, $return); $disk->assertMissing('old/asset.txt'); @@ -1343,7 +1343,7 @@ public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists( } #[Test] - public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when_no_conflict() + public function it_can_be_moved_to_another_folder_with_a_unique_filename_without_renaming_when_no_conflict() { Storage::fake('local'); $disk = Storage::disk('local'); @@ -1354,7 +1354,7 @@ public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when $asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']); $asset->save(); - $return = $asset->moveUnique('new'); + $return = $asset->move('new', null, true); $this->assertEquals($asset, $return); $disk->assertMissing('old/asset.txt'); diff --git a/tests/Imaging/ImageGeneratorTest.php b/tests/Imaging/ImageGeneratorTest.php index f95c9e2e5eb..222950ee042 100644 --- a/tests/Imaging/ImageGeneratorTest.php +++ b/tests/Imaging/ImageGeneratorTest.php @@ -98,6 +98,12 @@ public function it_generates_an_image_by_asset() Event::assertDispatchedTimes(GlideImageGenerated::class, 1); } + #[Test] + public function it_does_not_generate_an_image_for_a_missing_asset() + { + $this->assertSame('', $this->makeGenerator()->generateByAsset(null, ['w' => 100])); + } + #[Test] public function it_does_not_check_ffmpeg_availability_for_non_video_assets() { diff --git a/tests/Tags/GlideTest.php b/tests/Tags/GlideTest.php index d953dd5ed9d..6451fa895a5 100644 --- a/tests/Tags/GlideTest.php +++ b/tests/Tags/GlideTest.php @@ -3,6 +3,7 @@ namespace Tests\Tags; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Log; use Orchestra\Testbench\Attributes\DefineEnvironment; use PHPUnit\Framework\Attributes\Test; use Statamic\Facades\File; @@ -11,6 +12,25 @@ class GlideTest extends TestCase { + #[Test] + /** + * https://github.com/statamic/cms/pull/15447 + */ + public function it_logs_the_item_when_the_asset_cannot_be_resolved() + { + Log::shouldReceive('error') + ->once() + ->with(\Mockery::pattern('/Could not generate a manipulated image from asset.*nonexistent\.jpg/')); + + $result = (string) Parse::template( + '{{ glide:foo width="100" }}', + ['foo' => 'nonexistent.jpg'], + trusted: true + ); + + $this->assertSame('', $result); + } + #[Test] #[DefineEnvironment('relativeRouteUrl')] public function it_outputs_a_relative_url_by_default_when_the_glide_route_is_relative()