Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Filesystem/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Filesystem;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use League\Flysystem\UnableToReadFile;
use Statamic\Support\FileCollection;
use Statamic\Support\Str;

Expand All @@ -12,7 +13,7 @@ public function get($path, $fallback = null)
{
try {
return $this->filesystem->get($this->normalizePath($path)) ?: $fallback;
} catch (FileNotFoundException $e) {
} catch (FileNotFoundException|UnableToReadFile $e) {
return $fallback;
}
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Assets/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,50 @@ public function it_generates_meta_on_demand_if_it_doesnt_exist()
$this->assertEquals($metaWithData, Cache::get($asset->metaCacheKey()));
}

#[Test]
public function it_generates_meta_on_demand_if_it_doesnt_exist_on_a_disk_that_throws_exceptions()
{
Storage::fake('test', ['throw' => true]);

$file = UploadedFile::fake()->image('image.jpg', 30, 60); // creates a 723 byte image
Storage::disk('test')->putFileAs('foo', $file, 'image.jpg');
$realFilePath = Storage::disk('test')->path('foo/image.jpg');
touch($realFilePath, $timestamp = Carbon::parse('2021-02-22 09:41:42')->timestamp);

$container = Facades\AssetContainer::make('test')->disk('test');
$asset = (new Asset)->container($container)->path('foo/image.jpg');

$meta = [
'data' => [],
'size' => 723,
'last_modified' => $timestamp,
'width' => 30,
'height' => 60,
'mime_type' => 'image/jpeg',
'duration' => null,
];

$this->assertEquals($meta, $asset->meta());
$this->assertEquals($meta, YAML::parse(Storage::disk('test')->get('foo/.meta/image.jpg.yaml')));
}

#[Test]
public function it_uploads_to_a_disk_that_throws_exceptions_while_the_last_modified_index_is_in_use()
{
Storage::fake('test', ['throw' => true]);

$container = Facades\AssetContainer::make('test')->disk('test')->save();

Facades\Stache::store('assets::test')->cacheIndexUsage('last_modified');

$asset = $container->makeAsset('image.jpg');

$asset->upload(UploadedFile::fake()->image('image.jpg', 30, 60));

$this->assertTrue(Storage::disk('test')->exists('.meta/image.jpg.yaml'));
$this->assertNotNull($asset->lastModified());
}

#[Test]
public function it_generates_meta_on_demand_if_a_required_value_is_missing()
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Filesystem/FlysystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ protected function makeAdapter()
return new FlysystemAdapter($this->filesystem);
}

#[Test]
public function gets_fallback_if_file_doesnt_exist_on_a_disk_that_throws_exceptions()
{
$adapter = new FlysystemAdapter(Storage::build([
'driver' => 'local',
'root' => $this->tempDir,
'throw' => true,
]));

$this->assertEquals('Hello World', $adapter->get('filename.txt', 'Hello World'));
}

#[Test]
public function it_normalizes_relative_paths()
{
Expand Down
Loading