From 7b0b48cc30cd24f14ba39e77d1e3f47b2d044984 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Fri, 19 Feb 2021 09:43:29 +0100 Subject: [PATCH 1/4] Safely get asset meta values --- src/Assets/Asset.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index c11106cc4f3..209bb771cd1 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -134,6 +134,21 @@ public function meta() }); } + protected function metaValue($key) + { + $value = array_get($this->meta(), $key); + + if (! is_null($value)) { + return $value; + } + + Cache::forget($this->metaCacheKey()); + + $this->writeMeta($meta = $this->generateMeta()); + + return array_get($meta, $key); + } + public function generateMeta() { $meta = ['data' => $this->data->all()]; @@ -360,7 +375,7 @@ public function extension() */ public function lastModified() { - return Carbon::createFromTimestamp($this->meta()['last_modified']); + return Carbon::createFromTimestamp($this->metaValue('last_modified')); } /** @@ -492,7 +507,7 @@ public function move($folder, $filename = null) */ public function dimensions() { - return [$this->meta()['width'], $this->meta()['height']]; + return [$this->metaValue('width'), $this->metaValue('height')]; } /** @@ -554,7 +569,7 @@ public function ratio() */ public function size() { - return $this->meta()['size']; + return $this->metaValue('size'); } /** From ddaba26c68066eff14606adcfd79616ce6dace77 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Mon, 22 Feb 2021 09:42:48 +0100 Subject: [PATCH 2/4] Add a test for the new metaValue method --- tests/Assets/AssetTest.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/Assets/AssetTest.php b/tests/Assets/AssetTest.php index b31f260d787..c2d247ce9e0 100644 --- a/tests/Assets/AssetTest.php +++ b/tests/Assets/AssetTest.php @@ -376,6 +376,38 @@ 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_a_required_value_is_missing() + { + Storage::fake('test'); + + $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')->getAdapter()->getPathPrefix().'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'); + + $incompleteMeta = [ + 'data' => [], + ]; + + $completeMeta = [ + 'data' => [], + 'size' => 723, + 'last_modified' => $timestamp, + 'width' => 30, + 'height' => 60, + ]; + + Storage::disk('test')->put('foo/.meta/image.jpg.yaml', YAML::dump($incompleteMeta)); + + $asset->size(); + + $this->assertEquals($completeMeta, YAML::parse(Storage::disk('test')->get('foo/.meta/image.jpg.yaml'))); + } + /** @test */ public function it_hydrates_data_from_meta_file() { From 4a1502308ac39611c927a005552d6c7a5a3153f6 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 9 Mar 2021 10:09:18 -0500 Subject: [PATCH 3/4] Allow the meta method to get a key --- src/Assets/Asset.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 209bb771cd1..b533f38694b 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -113,8 +113,12 @@ public function exists() return $this->disk()->exists($path); } - public function meta() + public function meta($key = null) { + if (func_num_args() === 1) { + return $this->metaValue($key); + } + if (! config('statamic.assets.cache_meta')) { return $this->generateMeta(); } @@ -134,7 +138,7 @@ public function meta() }); } - protected function metaValue($key) + private function metaValue($key) { $value = array_get($this->meta(), $key); @@ -375,7 +379,7 @@ public function extension() */ public function lastModified() { - return Carbon::createFromTimestamp($this->metaValue('last_modified')); + return Carbon::createFromTimestamp($this->meta('last_modified')); } /** @@ -507,7 +511,7 @@ public function move($folder, $filename = null) */ public function dimensions() { - return [$this->metaValue('width'), $this->metaValue('height')]; + return [$this->meta('width'), $this->meta('height')]; } /** @@ -569,7 +573,7 @@ public function ratio() */ public function size() { - return $this->metaValue('size'); + return $this->meta('size'); } /** From f788c0d76f24a434fcc5c9def6958dbee81a21dd Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 9 Mar 2021 10:09:46 -0500 Subject: [PATCH 4/4] facade --- src/Assets/Asset.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index b533f38694b..c8d9cf5abb7 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -140,7 +140,7 @@ public function meta($key = null) private function metaValue($key) { - $value = array_get($this->meta(), $key); + $value = Arr::get($this->meta(), $key); if (! is_null($value)) { return $value; @@ -150,7 +150,7 @@ private function metaValue($key) $this->writeMeta($meta = $this->generateMeta()); - return array_get($meta, $key); + return Arr::get($meta, $key); } public function generateMeta()