From ca3ec2abcc1533fd324f47e1549a7ba2e2c4be3c Mon Sep 17 00:00:00 2001 From: edalzell Date: Sun, 9 Jan 2022 20:49:28 -0800 Subject: [PATCH 01/25] remove `else` --- src/Assets/Dimensions.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Assets/Dimensions.php b/src/Assets/Dimensions.php index 0cb410a61c8..9ec288448b9 100644 --- a/src/Assets/Dimensions.php +++ b/src/Assets/Dimensions.php @@ -37,7 +37,9 @@ public function get() { if ($this->asset->isImage()) { return $this->getImageDimensions(); - } elseif ($this->asset->isSvg()) { + } + + if ($this->asset->isSvg()) { return $this->getSvgDimensions(); } From fe23c5ca83d7904e43c5fe25af9b716c66f00369 Mon Sep 17 00:00:00 2001 From: edalzell Date: Sun, 9 Jan 2022 20:49:51 -0800 Subject: [PATCH 02/25] add getID3 for audio & video files --- composer.json | 1 + src/Assets/Asset.php | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 43b04d3f847..c7ceaf7a2df 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "league/csv": "^9.0", "league/glide": "^1.1", "michelf/php-smartypants": "^1.8", + "owen-oj/laravel-getid3": "^1.3", "pixelfear/composer-dist-plugin": "^0.1.4", "rebing/graphql-laravel": "^6.5", "spatie/blink": "^1.1.2", diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index e85f89d000a..94c8c0c1c46 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -5,6 +5,7 @@ use Facades\Statamic\Assets\Dimensions; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; +use Owenoj\LaravelGetId3\GetId3; use Statamic\Contracts\Assets\Asset as AssetContract; use Statamic\Contracts\Assets\AssetContainer as AssetContainerContract; use Statamic\Contracts\Data\Augmentable; @@ -163,13 +164,26 @@ public function generateMeta() if ($this->exists()) { $dimensions = Dimensions::asset($this)->get(); - $meta = array_merge($meta, [ + $otherMeta = [ 'size' => $this->disk()->size($this->path()), 'last_modified' => $this->disk()->lastModified($this->path()), 'width' => $dimensions[0], 'height' => $dimensions[1], 'mime_type' => $this->disk()->mimeType($this->path()), - ]); + ]; + + $audioVideo = []; + + if ($this->isVideo() || $this->isAudio()) { + $track = GetId3::fromDiskAndPath( + $this->container()->diskHandle(), + $this->basename() + ); + + $audioVideo = Arr::only($track->extractInfo(), ['audio', 'video']); + } + + $meta = array_merge($meta, $otherMeta, $audioVideo); } return $meta; From 1c2ac0d5e19f478916be9c6cbeb2c934d528fa80 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 20:27:31 -0800 Subject: [PATCH 03/25] use Dimensions --- src/Assets/Asset.php | 24 +++++------------- src/Assets/Dimensions.php | 53 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 94c8c0c1c46..c4ee2ee3c2b 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -2,10 +2,8 @@ namespace Statamic\Assets; -use Facades\Statamic\Assets\Dimensions; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; -use Owenoj\LaravelGetId3\GetId3; use Statamic\Contracts\Assets\Asset as AssetContract; use Statamic\Contracts\Assets\AssetContainer as AssetContainerContract; use Statamic\Contracts\Data\Augmentable; @@ -162,28 +160,18 @@ public function generateMeta() $meta = ['data' => $this->data->all()]; if ($this->exists()) { - $dimensions = Dimensions::asset($this)->get(); + [$width, $height, $length] = app(Dimensions::class)->asset($this)->get(); $otherMeta = [ - 'size' => $this->disk()->size($this->path()), + 'height' => $height, 'last_modified' => $this->disk()->lastModified($this->path()), - 'width' => $dimensions[0], - 'height' => $dimensions[1], + 'length' => $length, 'mime_type' => $this->disk()->mimeType($this->path()), + 'size' => $this->disk()->size($this->path()), + 'width' => $width, ]; - $audioVideo = []; - - if ($this->isVideo() || $this->isAudio()) { - $track = GetId3::fromDiskAndPath( - $this->container()->diskHandle(), - $this->basename() - ); - - $audioVideo = Arr::only($track->extractInfo(), ['audio', 'video']); - } - - $meta = array_merge($meta, $otherMeta, $audioVideo); + $meta = array_merge($meta, $otherMeta); } return $meta; diff --git a/src/Assets/Dimensions.php b/src/Assets/Dimensions.php index 9ec288448b9..71b6d32fb79 100644 --- a/src/Assets/Dimensions.php +++ b/src/Assets/Dimensions.php @@ -4,7 +4,9 @@ use Illuminate\Support\Facades\Storage; use League\Flysystem\MountManager; +use Owenoj\LaravelGetId3\GetId3; use Statamic\Imaging\ImageGenerator; +use Statamic\Support\Arr; class Dimensions { @@ -35,6 +37,10 @@ public function asset(Asset $asset) */ public function get() { + if ($this->asset->isAudio()) { + return $this->getAudioDimensions(); + } + if ($this->asset->isImage()) { return $this->getImageDimensions(); } @@ -43,7 +49,11 @@ public function get() return $this->getSvgDimensions(); } - return [null, null]; + if ($this->asset->isVideo()) { + return $this->getVideoDimensions(); + } + + return [null, null, null]; } /** @@ -66,6 +76,23 @@ public function height() return array_get($this->get(), 1); } + /** + * Get the dimensions of a sound. + * + * @return array + */ + private function getAudioDimensions() + { + $id3 = GetId3::fromDiskAndPath( + $this->asset->container()->diskHandle(), + $this->asset->basename() + )->extractInfo(); + + $length = Arr::get($id3, 'playtime_seconds', 0); + + return [null, null, $length]; + } + /** * Get the dimensions of an image. * @@ -90,13 +117,14 @@ private function getImageDimensions() try { $size = getimagesize($cache->getAdapter()->getPathPrefix().$cachePath); + $size[2] = 0; } catch (\Exception $e) { - $size = [0, 0]; + $size = [0, 0, 0]; } finally { $cache->delete($cachePath); } - return $size ? array_splice($size, 0, 2) : [0, 0]; + return $size ? array_splice($size, 0, 3) : [0, 0, 0]; } /** @@ -138,6 +166,25 @@ private function getSvgDimensions() return [300, 150]; } + /** + * Get the dimensions of a sound. + * + * @return array + */ + private function getVideoDimensions() + { + $id3 = GetId3::fromDiskAndPath( + $this->asset->container()->diskHandle(), + $this->asset->basename() + )->extractInfo(); + + $width = Arr::get($id3, 'video.resolution_x'); + $height = Arr::get($id3, 'video.resolution_y'); + $length = Arr::get($id3, 'playtime_seconds'); + + return [$width, $height, $length]; + } + private function getCacheFlysystem() { $disk = 'dimensions-cache'; From 5a8db0502114d3808216736d09ef43d7ee1176d4 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 20:44:46 -0800 Subject: [PATCH 04/25] rename and pass tests --- src/Assets/Asset.php | 2 +- src/Assets/{Dimensions.php => Attributes.php} | 26 ++++++++-------- tests/Assets/AssetTest.php | 5 +++- ...{DimensionsTest.php => AttributesTest.php} | 30 ++++++++++--------- 4 files changed, 34 insertions(+), 29 deletions(-) rename src/Assets/{Dimensions.php => Attributes.php} (88%) rename tests/Assets/{DimensionsTest.php => AttributesTest.php} (71%) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index c4ee2ee3c2b..4724a7d4895 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -160,7 +160,7 @@ public function generateMeta() $meta = ['data' => $this->data->all()]; if ($this->exists()) { - [$width, $height, $length] = app(Dimensions::class)->asset($this)->get(); + [$width, $height, $length] = app(Attributes::class)->asset($this)->get(); $otherMeta = [ 'height' => $height, diff --git a/src/Assets/Dimensions.php b/src/Assets/Attributes.php similarity index 88% rename from src/Assets/Dimensions.php rename to src/Assets/Attributes.php index 71b6d32fb79..4377a0542ed 100644 --- a/src/Assets/Dimensions.php +++ b/src/Assets/Attributes.php @@ -8,7 +8,7 @@ use Statamic\Imaging\ImageGenerator; use Statamic\Support\Arr; -class Dimensions +class Attributes { /** * @var Asset @@ -31,7 +31,7 @@ public function asset(Asset $asset) } /** - * Get the dimensions of an asset. + * Get the attributes of an asset. * * @return array */ @@ -77,7 +77,7 @@ public function height() } /** - * Get the dimensions of a sound. + * Get the attributes of a sound. * * @return array */ @@ -94,14 +94,14 @@ private function getAudioDimensions() } /** - * Get the dimensions of an image. + * Get the attributes of an image. * * @return array */ private function getImageDimensions() { // Since assets may be located on external platforms like Amazon S3, we can't simply - // grab the dimensions. So we'll copy it locally and read the dimensions from there. + // grab the attributes. So we'll copy it locally and read the attributes from there. $manager = new MountManager([ 'source' => $this->asset->disk()->filesystem()->getDriver(), 'cache' => $cache = $this->getCacheFlysystem(), @@ -128,14 +128,14 @@ private function getImageDimensions() } /** - * Get the dimensions of an SVG. + * Get the attributes of an SVG. * * @return array */ private function getSvgDimensions() { // Since assets may be located on external platforms like Amazon S3, we can't simply - // grab the dimensions. So we'll copy it locally and read the dimensions from there. + // grab the attributes. So we'll copy it locally and read the attributes from there. $manager = new MountManager([ 'source' => $this->asset->disk()->filesystem()->getDriver(), 'cache' => $cache = $this->getCacheFlysystem(), @@ -156,18 +156,18 @@ private function getSvgDimensions() if ($svg['width'] && $svg['height'] && is_numeric((string) $svg['width']) && is_numeric((string) $svg['height'])) { - return [(float) $svg['width'], (float) $svg['height']]; + return [(float) $svg['width'], (float) $svg['height'], 0]; } elseif ($svg['viewBox']) { $viewBox = preg_split('/[\s,]+/', $svg['viewBox'] ?: ''); - return [$viewBox[2], $viewBox[3]]; + return [$viewBox[2], $viewBox[3], 0]; } - return [300, 150]; + return [300, 150, 0]; } /** - * Get the dimensions of a sound. + * Get the attributes of a sound. * * @return array */ @@ -187,11 +187,11 @@ private function getVideoDimensions() private function getCacheFlysystem() { - $disk = 'dimensions-cache'; + $disk = 'attributes-cache'; config(["filesystems.disks.{$disk}" => [ 'driver' => 'local', - 'root' => storage_path('statamic/dimensions-cache'), + 'root' => storage_path('statamic/attributes-cache'), ]]); return Storage::disk($disk)->getDriver(); diff --git a/tests/Assets/AssetTest.php b/tests/Assets/AssetTest.php index cf0dff61752..b67673dee7b 100644 --- a/tests/Assets/AssetTest.php +++ b/tests/Assets/AssetTest.php @@ -42,7 +42,7 @@ public function setUp(): void ->disk('test'); Storage::fake('test'); - Storage::fake('dimensions-cache'); + Storage::fake('attributes-cache'); } /** @test */ @@ -360,6 +360,7 @@ public function it_generates_meta_on_demand_if_it_doesnt_exist() 'width' => 30, 'height' => 60, 'mime_type' => 'image/jpeg', + 'length' => 0, ]; $metaWithData = [ @@ -369,6 +370,7 @@ public function it_generates_meta_on_demand_if_it_doesnt_exist() 'width' => 30, 'height' => 60, 'mime_type' => 'image/jpeg', + 'length' => 0, ]; // The meta that's saved to file will also be cached, but will not include in-memory data... @@ -413,6 +415,7 @@ public function it_generates_meta_on_demand_if_a_required_value_is_missing() 'width' => 30, 'height' => 60, 'mime_type' => 'image/jpeg', + 'length' => 0, ]; Storage::disk('test')->put('foo/.meta/image.jpg.yaml', YAML::dump($incompleteMeta)); diff --git a/tests/Assets/DimensionsTest.php b/tests/Assets/AttributesTest.php similarity index 71% rename from tests/Assets/DimensionsTest.php rename to tests/Assets/AttributesTest.php index 75ad24f0b44..3c82137824b 100644 --- a/tests/Assets/DimensionsTest.php +++ b/tests/Assets/AttributesTest.php @@ -6,12 +6,12 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; use Statamic\Assets\Asset; -use Statamic\Assets\Dimensions; +use Statamic\Assets\Attributes; use Statamic\Facades\AssetContainer; use Statamic\Imaging\ImageGenerator; use Tests\TestCase; -class DimensionsTest extends TestCase +class AttributesTest extends TestCase { public function setUp(): void { @@ -24,19 +24,21 @@ public function setUp(): void Storage::fake('test'); - $this->dimensions = new Dimensions(app(ImageGenerator::class)); + $this->attributes = new Attributes(app(ImageGenerator::class)); } /** @test */ public function a_non_image_asset_has_no_dimensions() { $asset = $this->mock(Asset::class); + $asset->shouldReceive('isAudio')->andReturnFalse(); $asset->shouldReceive('isImage')->andReturnFalse(); $asset->shouldReceive('isSvg')->andReturnFalse(); + $asset->shouldReceive('isVideo')->andReturnFalse(); - $dimensions = $this->dimensions->asset($asset); + $dimensions = $this->attributes->asset($asset); - $this->assertEquals([null, null], $dimensions->get()); + $this->assertEquals([null, null, null], $dimensions->get()); $this->assertEquals(null, $dimensions->width()); $this->assertEquals(null, $dimensions->height()); } @@ -59,9 +61,9 @@ public function it_gets_the_dimensions() $imagesize = getimagesize($realpath); $this->assertEquals([30, 60], array_splice($imagesize, 0, 2)); - $dimensions = $this->dimensions->asset($asset); + $dimensions = $this->attributes->asset($asset); - $this->assertEquals([30, 60], $dimensions->get()); + $this->assertEquals([30, 60, 0], $dimensions->get()); $this->assertEquals(30, $dimensions->width()); $this->assertEquals(60, $dimensions->height()); } @@ -71,7 +73,7 @@ public function it_gets_the_dimensions_of_an_svg() { $asset = $this->svgAsset(''); - $this->assertEquals([30, 60], $this->dimensions->asset($asset)->get()); + $this->assertEquals([30.0, 60.0, 0], $this->attributes->asset($asset)->get()); } /** @test */ @@ -79,7 +81,7 @@ public function it_uses_the_viewbox_if_the_svg_dimensions_havent_been_provided() { $asset = $this->svgAsset(''); - $this->assertEquals([300, 600], $this->dimensions->asset($asset)->get()); + $this->assertEquals([300, 600, 0], $this->attributes->asset($asset)->get()); } /** @test */ @@ -87,7 +89,7 @@ public function it_uses_the_viewbox_if_the_svg_dimensions_are_percents() { $asset = $this->svgAsset(''); - $this->assertEquals([300, 600], $this->dimensions->asset($asset)->get()); + $this->assertEquals([300, 600, 0], $this->attributes->asset($asset)->get()); } /** @test */ @@ -95,15 +97,15 @@ public function it_uses_the_viewbox_if_the_svg_dimensions_are_ems() { $asset = $this->svgAsset(''); - $this->assertEquals([300, 600], $this->dimensions->asset($asset)->get()); + $this->assertEquals([300, 600, 0], $this->attributes->asset($asset)->get()); } /** @test */ public function it_uses_default_dimensions_if_the_svg_has_no_viewbox_and_is_missing_either_or_both_dimensions() { - $this->assertEquals([300, 150], $this->dimensions->asset($this->svgAsset(''))->get()); - $this->assertEquals([300, 150], $this->dimensions->asset($this->svgAsset(''))->get()); - $this->assertEquals([300, 150], $this->dimensions->asset($this->svgAsset(''))->get()); + $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); + $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); + $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); } private function svgAsset($svg) From 3259d022f4447f9334e583c0fc08ae7a9c615054 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 20:50:36 -0800 Subject: [PATCH 05/25] fix another test --- tests/Assets/AssetRepositoryTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Assets/AssetRepositoryTest.php b/tests/Assets/AssetRepositoryTest.php index f0c92543a39..d7d32566373 100644 --- a/tests/Assets/AssetRepositoryTest.php +++ b/tests/Assets/AssetRepositoryTest.php @@ -34,11 +34,12 @@ public function it_saves_the_meta_file_to_disk() $disk->assertExists($path = 'foo/.meta/image.jpg.yaml'); $contents = <<assertEquals($contents, $disk->get($path)); From fc15535937b73d461833c97c012319d58ef47cb2 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 21:31:35 -0800 Subject: [PATCH 06/25] rename --- src/Assets/Attributes.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index 4377a0542ed..a16444b25ef 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -38,19 +38,19 @@ public function asset(Asset $asset) public function get() { if ($this->asset->isAudio()) { - return $this->getAudioDimensions(); + return $this->getAudioAttributes(); } if ($this->asset->isImage()) { - return $this->getImageDimensions(); + return $this->getImageAttributes(); } if ($this->asset->isSvg()) { - return $this->getSvgDimensions(); + return $this->getSvgAttributes(); } if ($this->asset->isVideo()) { - return $this->getVideoDimensions(); + return $this->getVideoAttributes(); } return [null, null, null]; @@ -81,7 +81,7 @@ public function height() * * @return array */ - private function getAudioDimensions() + private function getAudioAttributes() { $id3 = GetId3::fromDiskAndPath( $this->asset->container()->diskHandle(), @@ -98,7 +98,7 @@ private function getAudioDimensions() * * @return array */ - private function getImageDimensions() + private function getImageAttributes() { // Since assets may be located on external platforms like Amazon S3, we can't simply // grab the attributes. So we'll copy it locally and read the attributes from there. @@ -132,7 +132,7 @@ private function getImageDimensions() * * @return array */ - private function getSvgDimensions() + private function getSvgAttributes() { // Since assets may be located on external platforms like Amazon S3, we can't simply // grab the attributes. So we'll copy it locally and read the attributes from there. From a2da9d1aab31feefe88e3acf728b5a82c59886f7 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 21:31:54 -0800 Subject: [PATCH 07/25] composer plugins --- composer.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c7ceaf7a2df..f715956d223 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,11 @@ "config": { "optimize-autoloader": true, "preferred-install": "dist", - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "composer/package-versions-deprecated": true, + "pixelfear/composer-dist-plugin": true + } }, "extra": { "download-dist": { From 538910687b838f7ad98d1dcea14373447373787d Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 21:32:10 -0800 Subject: [PATCH 08/25] load id3 provider --- tests/TestCase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 831fc50e41b..6912362c2d5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -54,6 +54,7 @@ protected function getPackageProviders($app) \Rebing\GraphQL\GraphQLServiceProvider::class, \Wilderborn\Partyline\ServiceProvider::class, \Archetype\ServiceProvider::class, + \Owenoj\LaravelGetId3\GetId3ServiceProvider::class, ]; } From adec6866f532282e88c954ff4b696ba463b1bf41 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 21:34:20 -0800 Subject: [PATCH 09/25] rename --- tests/Assets/AttributesTest.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/Assets/AttributesTest.php b/tests/Assets/AttributesTest.php index 3c82137824b..577a5ed4dcb 100644 --- a/tests/Assets/AttributesTest.php +++ b/tests/Assets/AttributesTest.php @@ -8,7 +8,6 @@ use Statamic\Assets\Asset; use Statamic\Assets\Attributes; use Statamic\Facades\AssetContainer; -use Statamic\Imaging\ImageGenerator; use Tests\TestCase; class AttributesTest extends TestCase @@ -24,11 +23,11 @@ public function setUp(): void Storage::fake('test'); - $this->attributes = new Attributes(app(ImageGenerator::class)); + $this->attributes = app(Attributes::class); } /** @test */ - public function a_non_image_asset_has_no_dimensions() + public function a_non_image_asset_has_no_attributes() { $asset = $this->mock(Asset::class); $asset->shouldReceive('isAudio')->andReturnFalse(); @@ -36,15 +35,15 @@ public function a_non_image_asset_has_no_dimensions() $asset->shouldReceive('isSvg')->andReturnFalse(); $asset->shouldReceive('isVideo')->andReturnFalse(); - $dimensions = $this->attributes->asset($asset); + $attributes = $this->attributes->asset($asset); - $this->assertEquals([null, null, null], $dimensions->get()); - $this->assertEquals(null, $dimensions->width()); - $this->assertEquals(null, $dimensions->height()); + $this->assertEquals([null, null, null], $attributes->get()); + $this->assertEquals(null, $attributes->width()); + $this->assertEquals(null, $attributes->height()); } /** @test */ - public function it_gets_the_dimensions() + public function it_gets_the_attributes() { Carbon::setTestNow(now()); @@ -61,15 +60,15 @@ public function it_gets_the_dimensions() $imagesize = getimagesize($realpath); $this->assertEquals([30, 60], array_splice($imagesize, 0, 2)); - $dimensions = $this->attributes->asset($asset); + $attributes = $this->attributes->asset($asset); - $this->assertEquals([30, 60, 0], $dimensions->get()); - $this->assertEquals(30, $dimensions->width()); - $this->assertEquals(60, $dimensions->height()); + $this->assertEquals([30, 60, 0], $attributes->get()); + $this->assertEquals(30, $attributes->width()); + $this->assertEquals(60, $attributes->height()); } /** @test */ - public function it_gets_the_dimensions_of_an_svg() + public function it_gets_the_attributes_of_an_svg() { $asset = $this->svgAsset(''); @@ -101,7 +100,7 @@ public function it_uses_the_viewbox_if_the_svg_dimensions_are_ems() } /** @test */ - public function it_uses_default_dimensions_if_the_svg_has_no_viewbox_and_is_missing_either_or_both_dimensions() + public function it_uses_default_attributes_if_the_svg_has_no_viewbox_and_is_missing_either_or_both_dimensions() { $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); From 5a1a7bc7b73c771d1c3fb9f0b70cd3db3e0adaf5 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 12 Jan 2022 21:34:34 -0800 Subject: [PATCH 10/25] add (broken) test --- tests/Assets/AttributesTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Assets/AttributesTest.php b/tests/Assets/AttributesTest.php index 577a5ed4dcb..e2bbfacd369 100644 --- a/tests/Assets/AttributesTest.php +++ b/tests/Assets/AttributesTest.php @@ -67,6 +67,31 @@ public function it_gets_the_attributes() $this->assertEquals(60, $attributes->height()); } + /** @test */ + public function it_gets_the_attributes_of_audio_file() + { + $this->markTestSkipped(); + Carbon::setTestNow(now()); + + $asset = (new Asset) + ->container(AssetContainer::make('test-container')->disk('test')) + ->path('path/to/asset.mp3'); + + $file = UploadedFile::fake()->createWithContent('asset.mp3', 'Hello World'); + + Storage::disk('test')->putFileAs('path/to', $file, 'asset.mp3'); + + // Test about the actual file, for good measure. + $realpath = Storage::disk('test')->getAdapter()->getPathPrefix().'path/to/asset.mp3'; + $this->assertFileExists($realpath); + + $attributes = $this->attributes->asset($asset); + + $this->assertEquals([0, 0, 11], $attributes->get()); + $this->assertEquals(0, $attributes->width()); + $this->assertEquals(0, $attributes->height()); + } + /** @test */ public function it_gets_the_attributes_of_an_svg() { From 3745db6a6d170fc2773918c950822f57c4a9fb8a Mon Sep 17 00:00:00 2001 From: edalzell Date: Sat, 15 Jan 2022 18:22:39 -0800 Subject: [PATCH 11/25] keyed array --- src/Assets/Asset.php | 8 ++++---- src/Assets/Attributes.php | 36 ++++++++++++++++----------------- tests/Assets/AttributesTest.php | 25 ++++++++++++----------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index b265cfcea2f..f63a09ad2ec 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -158,15 +158,15 @@ public function generateMeta() $meta = ['data' => $this->data->all()]; if ($this->exists()) { - [$width, $height, $length] = app(Attributes::class)->asset($this)->get(); + $attributes = app(Attributes::class)->asset($this)->get(); $otherMeta = [ - 'height' => $height, + 'height' => Arr::get($attributes, 'height'), 'last_modified' => $this->disk()->lastModified($this->path()), - 'length' => $length, + 'length' => Arr::get($attributes, 'length'), 'mime_type' => $this->disk()->mimeType($this->path()), 'size' => $this->disk()->size($this->path()), - 'width' => $width, + 'width' => Arr::get($attributes, 'width'), ]; $meta = array_merge($meta, $otherMeta); diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index a16444b25ef..fd504dc8828 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -53,7 +53,7 @@ public function get() return $this->getVideoAttributes(); } - return [null, null, null]; + return []; } /** @@ -63,7 +63,7 @@ public function get() */ public function width() { - return array_get($this->get(), 0); + return array_get($this->get(), 'width'); } /** @@ -73,7 +73,7 @@ public function width() */ public function height() { - return array_get($this->get(), 1); + return array_get($this->get(), 'height'); } /** @@ -90,7 +90,7 @@ private function getAudioAttributes() $length = Arr::get($id3, 'playtime_seconds', 0); - return [null, null, $length]; + return ['length' => $length]; } /** @@ -116,15 +116,15 @@ private function getImageAttributes() $manager->copy("source://{$this->asset->path()}", $destination); try { - $size = getimagesize($cache->getAdapter()->getPathPrefix().$cachePath); - $size[2] = 0; + [$width, $height] = getimagesize($cache->getAdapter()->getPathPrefix().$cachePath); + $size = compact('width', 'height'); } catch (\Exception $e) { - $size = [0, 0, 0]; + $size = []; } finally { $cache->delete($cachePath); } - return $size ? array_splice($size, 0, 3) : [0, 0, 0]; + return $size; } /** @@ -156,14 +156,14 @@ private function getSvgAttributes() if ($svg['width'] && $svg['height'] && is_numeric((string) $svg['width']) && is_numeric((string) $svg['height'])) { - return [(float) $svg['width'], (float) $svg['height'], 0]; + return ['width' => (float) $svg['width'], 'height' => (float) $svg['height']]; } elseif ($svg['viewBox']) { - $viewBox = preg_split('/[\s,]+/', $svg['viewBox'] ?: ''); + [,,$width, $height] = preg_split('/[\s,]+/', $svg['viewBox'] ?: ''); - return [$viewBox[2], $viewBox[3], 0]; + return compact('width', 'height'); } - return [300, 150, 0]; + return ['width' => 300, 'height' => 150]; } /** @@ -171,18 +171,18 @@ private function getSvgAttributes() * * @return array */ - private function getVideoDimensions() + private function getVideoAttributes() { $id3 = GetId3::fromDiskAndPath( $this->asset->container()->diskHandle(), $this->asset->basename() )->extractInfo(); - $width = Arr::get($id3, 'video.resolution_x'); - $height = Arr::get($id3, 'video.resolution_y'); - $length = Arr::get($id3, 'playtime_seconds'); - - return [$width, $height, $length]; + return [ + 'width' => Arr::get($id3, 'video.resolution_x'), + 'height' => Arr::get($id3, 'video.resolution_y'), + 'length' => Arr::get($id3, 'playtime_seconds'), + ]; } private function getCacheFlysystem() diff --git a/tests/Assets/AttributesTest.php b/tests/Assets/AttributesTest.php index e2bbfacd369..758df77795c 100644 --- a/tests/Assets/AttributesTest.php +++ b/tests/Assets/AttributesTest.php @@ -37,7 +37,7 @@ public function a_non_image_asset_has_no_attributes() $attributes = $this->attributes->asset($asset); - $this->assertEquals([null, null, null], $attributes->get()); + $this->assertEquals([], $attributes->get()); $this->assertEquals(null, $attributes->width()); $this->assertEquals(null, $attributes->height()); } @@ -57,12 +57,13 @@ public function it_gets_the_attributes() // Test about the actual file, for good measure. $realpath = Storage::disk('test')->getAdapter()->getPathPrefix().'path/to/asset.jpg'; $this->assertFileExists($realpath); - $imagesize = getimagesize($realpath); - $this->assertEquals([30, 60], array_splice($imagesize, 0, 2)); + [$width, $height] = getimagesize($realpath); + $this->assertEquals(30, $width); + $this->assertEquals(60, $height); $attributes = $this->attributes->asset($asset); - $this->assertEquals([30, 60, 0], $attributes->get()); + $this->assertEquals(['width' => 30, 'height' => 60], $attributes->get()); $this->assertEquals(30, $attributes->width()); $this->assertEquals(60, $attributes->height()); } @@ -87,7 +88,7 @@ public function it_gets_the_attributes_of_audio_file() $attributes = $this->attributes->asset($asset); - $this->assertEquals([0, 0, 11], $attributes->get()); + $this->assertEquals(['length' => 11], $attributes->get()); $this->assertEquals(0, $attributes->width()); $this->assertEquals(0, $attributes->height()); } @@ -97,7 +98,7 @@ public function it_gets_the_attributes_of_an_svg() { $asset = $this->svgAsset(''); - $this->assertEquals([30.0, 60.0, 0], $this->attributes->asset($asset)->get()); + $this->assertEquals(['width' => 30.0, 'height' => 60.0], $this->attributes->asset($asset)->get()); } /** @test */ @@ -105,7 +106,7 @@ public function it_uses_the_viewbox_if_the_svg_dimensions_havent_been_provided() { $asset = $this->svgAsset(''); - $this->assertEquals([300, 600, 0], $this->attributes->asset($asset)->get()); + $this->assertEquals(['width' => 300, 'height' => 600], $this->attributes->asset($asset)->get()); } /** @test */ @@ -113,7 +114,7 @@ public function it_uses_the_viewbox_if_the_svg_dimensions_are_percents() { $asset = $this->svgAsset(''); - $this->assertEquals([300, 600, 0], $this->attributes->asset($asset)->get()); + $this->assertEquals(['width' => 300, 'height' => 600], $this->attributes->asset($asset)->get()); } /** @test */ @@ -121,15 +122,15 @@ public function it_uses_the_viewbox_if_the_svg_dimensions_are_ems() { $asset = $this->svgAsset(''); - $this->assertEquals([300, 600, 0], $this->attributes->asset($asset)->get()); + $this->assertEquals(['width' => 300, 'height' => 600], $this->attributes->asset($asset)->get()); } /** @test */ public function it_uses_default_attributes_if_the_svg_has_no_viewbox_and_is_missing_either_or_both_dimensions() { - $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); - $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); - $this->assertEquals([300, 150, 0], $this->attributes->asset($this->svgAsset(''))->get()); + $this->assertEquals(['width' => 300, 'height' => 150], $this->attributes->asset($this->svgAsset(''))->get()); + $this->assertEquals(['width' => 300, 'height' => 150], $this->attributes->asset($this->svgAsset(''))->get()); + $this->assertEquals(['width' => 300, 'height' => 150], $this->attributes->asset($this->svgAsset(''))->get()); } private function svgAsset($svg) From 31dc43e2482b272ae097c09c29abcabd7007fa5e Mon Sep 17 00:00:00 2001 From: edalzell Date: Sat, 15 Jan 2022 18:25:37 -0800 Subject: [PATCH 12/25] missed a test --- tests/Assets/AssetRepositoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Assets/AssetRepositoryTest.php b/tests/Assets/AssetRepositoryTest.php index d7d32566373..ea19f78af6b 100644 --- a/tests/Assets/AssetRepositoryTest.php +++ b/tests/Assets/AssetRepositoryTest.php @@ -36,7 +36,7 @@ public function it_saves_the_meta_file_to_disk() data: { } height: 60 last_modified: $timestamp -length: 0 +length: null mime_type: image/jpeg size: 723 width: 30 From 879484b2cdeec024e78472148ddd7a804d95996a Mon Sep 17 00:00:00 2001 From: edalzell Date: Sun, 16 Jan 2022 11:22:24 -0800 Subject: [PATCH 13/25] video --- src/Assets/Attributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index fd504dc8828..1b7e1d31355 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -167,7 +167,7 @@ private function getSvgAttributes() } /** - * Get the attributes of a sound. + * Get the attributes of a video. * * @return array */ From b222593345112a9502e01cfbb99614ad7cdc1a16 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 19 Jan 2022 20:51:43 -0800 Subject: [PATCH 14/25] duration --- src/Assets/Asset.php | 2 +- src/Assets/Attributes.php | 4 ++-- tests/Assets/AssetRepositoryTest.php | 2 +- tests/Assets/AttributesTest.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index f63a09ad2ec..831c007d32c 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -163,7 +163,7 @@ public function generateMeta() $otherMeta = [ 'height' => Arr::get($attributes, 'height'), 'last_modified' => $this->disk()->lastModified($this->path()), - 'length' => Arr::get($attributes, 'length'), + 'duration' => Arr::get($attributes, 'duration'), 'mime_type' => $this->disk()->mimeType($this->path()), 'size' => $this->disk()->size($this->path()), 'width' => Arr::get($attributes, 'width'), diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index 1b7e1d31355..1c8f0c3205d 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -90,7 +90,7 @@ private function getAudioAttributes() $length = Arr::get($id3, 'playtime_seconds', 0); - return ['length' => $length]; + return ['duration' => $length]; } /** @@ -181,7 +181,7 @@ private function getVideoAttributes() return [ 'width' => Arr::get($id3, 'video.resolution_x'), 'height' => Arr::get($id3, 'video.resolution_y'), - 'length' => Arr::get($id3, 'playtime_seconds'), + 'duration' => Arr::get($id3, 'playtime_seconds'), ]; } diff --git a/tests/Assets/AssetRepositoryTest.php b/tests/Assets/AssetRepositoryTest.php index ea19f78af6b..dd79a6656fc 100644 --- a/tests/Assets/AssetRepositoryTest.php +++ b/tests/Assets/AssetRepositoryTest.php @@ -36,7 +36,7 @@ public function it_saves_the_meta_file_to_disk() data: { } height: 60 last_modified: $timestamp -length: null +duration: null mime_type: image/jpeg size: 723 width: 30 diff --git a/tests/Assets/AttributesTest.php b/tests/Assets/AttributesTest.php index 758df77795c..88b873fc63f 100644 --- a/tests/Assets/AttributesTest.php +++ b/tests/Assets/AttributesTest.php @@ -88,7 +88,7 @@ public function it_gets_the_attributes_of_audio_file() $attributes = $this->attributes->asset($asset); - $this->assertEquals(['length' => 11], $attributes->get()); + $this->assertEquals(['duration' => 11], $attributes->get()); $this->assertEquals(0, $attributes->width()); $this->assertEquals(0, $attributes->height()); } From 08d3b46d09fae331b13d63d44a36c3450959503b Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 19 Jan 2022 20:58:56 -0800 Subject: [PATCH 15/25] missed a test --- tests/Assets/AssetTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Assets/AssetTest.php b/tests/Assets/AssetTest.php index 1fcf688dd07..0e33a821d2a 100644 --- a/tests/Assets/AssetTest.php +++ b/tests/Assets/AssetTest.php @@ -377,7 +377,7 @@ public function it_generates_meta_on_demand_if_it_doesnt_exist() 'width' => 30, 'height' => 60, 'mime_type' => 'image/jpeg', - 'length' => 0, + 'duration' => null, ]; $metaWithData = [ @@ -387,7 +387,7 @@ public function it_generates_meta_on_demand_if_it_doesnt_exist() 'width' => 30, 'height' => 60, 'mime_type' => 'image/jpeg', - 'length' => 0, + 'duration' => null, ]; // The meta that's saved to file will also be cached, but will not include in-memory data... @@ -432,7 +432,7 @@ public function it_generates_meta_on_demand_if_a_required_value_is_missing() 'width' => 30, 'height' => 60, 'mime_type' => 'image/jpeg', - 'length' => 0, + 'duration' => null, ]; Storage::disk('test')->put('foo/.meta/image.jpg.yaml', YAML::dump($incompleteMeta)); From 66160adb5abc9e04f88ea1f1a1df24e76b37d975 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 25 Jan 2022 15:57:16 -0500 Subject: [PATCH 16/25] Deprecate the Dimensions class instead of deleting --- src/Assets/Dimensions.php | 26 +++++++ tests/Assets/DimensionsTest.php | 121 ++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/Assets/Dimensions.php create mode 100644 tests/Assets/DimensionsTest.php diff --git a/src/Assets/Dimensions.php b/src/Assets/Dimensions.php new file mode 100644 index 00000000000..6ca2875d97e --- /dev/null +++ b/src/Assets/Dimensions.php @@ -0,0 +1,26 @@ +get(), 0); + } + + public function height() + { + return array_get($this->get(), 1); + } +} diff --git a/tests/Assets/DimensionsTest.php b/tests/Assets/DimensionsTest.php new file mode 100644 index 00000000000..6d1adfe68af --- /dev/null +++ b/tests/Assets/DimensionsTest.php @@ -0,0 +1,121 @@ + [ + 'driver' => 'local', + 'root' => __DIR__.'/doesnt-matter-itll-get-faked-anyway', + ]]); + + Storage::fake('test'); + + $this->dimensions = new Dimensions(app(ImageGenerator::class)); + } + + /** @test */ + public function a_non_image_asset_has_no_dimensions() + { + $asset = $this->mock(Asset::class); + $asset->shouldReceive('isImage')->andReturnFalse(); + $asset->shouldReceive('isSvg')->andReturnFalse(); + $asset->shouldReceive('isAudio')->andReturnFalse(); + $asset->shouldReceive('isVideo')->andReturnFalse(); + + $dimensions = $this->dimensions->asset($asset); + + $this->assertEquals([null, null], $dimensions->get()); + $this->assertEquals(null, $dimensions->width()); + $this->assertEquals(null, $dimensions->height()); + } + + /** @test */ + public function it_gets_the_dimensions() + { + Carbon::setTestNow(now()); + + $asset = (new Asset) + ->container(AssetContainer::make('test-container')->disk('test')) + ->path('path/to/asset.jpg'); + + $file = UploadedFile::fake()->image('asset.jpg', 30, 60); + Storage::disk('test')->putFileAs('path/to', $file, 'asset.jpg'); + + // Test about the actual file, for good measure. + $realpath = Storage::disk('test')->getAdapter()->getPathPrefix().'path/to/asset.jpg'; + $this->assertFileExists($realpath); + $imagesize = getimagesize($realpath); + $this->assertEquals([30, 60], array_splice($imagesize, 0, 2)); + + $dimensions = $this->dimensions->asset($asset); + + $this->assertEquals([30, 60], $dimensions->get()); + $this->assertEquals(30, $dimensions->width()); + $this->assertEquals(60, $dimensions->height()); + } + + /** @test */ + public function it_gets_the_dimensions_of_an_svg() + { + $asset = $this->svgAsset(''); + + $this->assertEquals([30, 60], $this->dimensions->asset($asset)->get()); + } + + /** @test */ + public function it_uses_the_viewbox_if_the_svg_dimensions_havent_been_provided() + { + $asset = $this->svgAsset(''); + + $this->assertEquals([300, 600], $this->dimensions->asset($asset)->get()); + } + + /** @test */ + public function it_uses_the_viewbox_if_the_svg_dimensions_are_percents() + { + $asset = $this->svgAsset(''); + + $this->assertEquals([300, 600], $this->dimensions->asset($asset)->get()); + } + + /** @test */ + public function it_uses_the_viewbox_if_the_svg_dimensions_are_ems() + { + $asset = $this->svgAsset(''); + + $this->assertEquals([300, 600], $this->dimensions->asset($asset)->get()); + } + + /** @test */ + public function it_uses_default_dimensions_if_the_svg_has_no_viewbox_and_is_missing_either_or_both_dimensions() + { + $this->assertEquals([300, 150], $this->dimensions->asset($this->svgAsset(''))->get()); + $this->assertEquals([300, 150], $this->dimensions->asset($this->svgAsset(''))->get()); + $this->assertEquals([300, 150], $this->dimensions->asset($this->svgAsset(''))->get()); + } + + private function svgAsset($svg) + { + $asset = (new Asset) + ->container(AssetContainer::make('test-container')->disk('test')) + ->path('path/to/asset.svg'); + + Storage::disk('test')->put('path/to/asset.svg', $svg); + + return $asset; + } + } From 1f6135d42b1739ffbb51c38e3ce1fad2d2609c0b Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 25 Jan 2022 16:44:37 -0500 Subject: [PATCH 17/25] Move package logic into a mockable class --- src/Assets/Attributes.php | 12 +++------- src/Assets/ExtractInfo.php | 17 ++++++++++++++ tests/Assets/AttributesTest.php | 40 ++++++++++++++++++++++----------- 3 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 src/Assets/ExtractInfo.php diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index 1c8f0c3205d..eb445f998fd 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -2,9 +2,9 @@ namespace Statamic\Assets; +use Facades\Statamic\Assets\ExtractInfo; use Illuminate\Support\Facades\Storage; use League\Flysystem\MountManager; -use Owenoj\LaravelGetId3\GetId3; use Statamic\Imaging\ImageGenerator; use Statamic\Support\Arr; @@ -83,10 +83,7 @@ public function height() */ private function getAudioAttributes() { - $id3 = GetId3::fromDiskAndPath( - $this->asset->container()->diskHandle(), - $this->asset->basename() - )->extractInfo(); + $id3 = ExtractInfo::fromAsset($this->asset); $length = Arr::get($id3, 'playtime_seconds', 0); @@ -173,10 +170,7 @@ private function getSvgAttributes() */ private function getVideoAttributes() { - $id3 = GetId3::fromDiskAndPath( - $this->asset->container()->diskHandle(), - $this->asset->basename() - )->extractInfo(); + $id3 = ExtractInfo::fromAsset($this->asset); return [ 'width' => Arr::get($id3, 'video.resolution_x'), diff --git a/src/Assets/ExtractInfo.php b/src/Assets/ExtractInfo.php new file mode 100644 index 00000000000..c41d76ef3b9 --- /dev/null +++ b/src/Assets/ExtractInfo.php @@ -0,0 +1,17 @@ +container()->diskHandle(), + $asset->path() + )->extractInfo(); + } +} diff --git a/tests/Assets/AttributesTest.php b/tests/Assets/AttributesTest.php index 88b873fc63f..3839b41b9c5 100644 --- a/tests/Assets/AttributesTest.php +++ b/tests/Assets/AttributesTest.php @@ -2,6 +2,7 @@ namespace Tests\Assets; +use Facades\Statamic\Assets\ExtractInfo; use Illuminate\Http\UploadedFile; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; @@ -71,26 +72,39 @@ public function it_gets_the_attributes() /** @test */ public function it_gets_the_attributes_of_audio_file() { - $this->markTestSkipped(); - Carbon::setTestNow(now()); - $asset = (new Asset) - ->container(AssetContainer::make('test-container')->disk('test')) - ->path('path/to/asset.mp3'); + ->container(AssetContainer::make('test-container')->disk('test')) + ->path('path/to/asset.mp3'); - $file = UploadedFile::fake()->createWithContent('asset.mp3', 'Hello World'); + ExtractInfo::shouldReceive('fromAsset')->with($asset)->andReturn(['playtime_seconds' => 13]); - Storage::disk('test')->putFileAs('path/to', $file, 'asset.mp3'); + $attributes = $this->attributes->asset($asset); - // Test about the actual file, for good measure. - $realpath = Storage::disk('test')->getAdapter()->getPathPrefix().'path/to/asset.mp3'; - $this->assertFileExists($realpath); + $this->assertEquals(['duration' => 13], $attributes->get()); + $this->assertNull($attributes->width()); + $this->assertNull($attributes->height()); + } + + /** @test */ + public function it_gets_the_attributes_of_video_file() + { + $asset = (new Asset) + ->container(AssetContainer::make('test-container')->disk('test')) + ->path('path/to/asset.mp4'); + + ExtractInfo::shouldReceive('fromAsset')->with($asset)->andReturn([ + 'playtime_seconds' => 13, + 'video' => [ + 'resolution_x' => 1920, + 'resolution_y' => 1080, + ], + ]); $attributes = $this->attributes->asset($asset); - $this->assertEquals(['duration' => 11], $attributes->get()); - $this->assertEquals(0, $attributes->width()); - $this->assertEquals(0, $attributes->height()); + $this->assertEquals(['duration' => 13, 'width' => 1920, 'height' => 1080], $attributes->get()); + $this->assertEquals(1920, $attributes->width()); + $this->assertEquals(1080, $attributes->height()); } /** @test */ From 3f460143f3032740ea55afee550573b5c58cdac1 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 25 Jan 2022 17:06:16 -0500 Subject: [PATCH 18/25] Avoid the middleman laravel package --- composer.json | 2 +- src/Assets/ExtractInfo.php | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index f715956d223..6b59456bb7b 100644 --- a/composer.json +++ b/composer.json @@ -14,13 +14,13 @@ "composer/composer": "^1.10.22 || ^2.0.13", "facade/ignition-contracts": "^1.0", "guzzlehttp/guzzle": "^6.3 || ^7.0", + "james-heinrich/getid3": "^1.9", "laravel/framework": "^6.20.14 || ^7.30.4 || ^8.24.0", "laravel/helpers": "^1.1", "league/commonmark": "^1.5", "league/csv": "^9.0", "league/glide": "^1.1", "michelf/php-smartypants": "^1.8", - "owen-oj/laravel-getid3": "^1.3", "pixelfear/composer-dist-plugin": "^0.1.4", "rebing/graphql-laravel": "^6.5", "spatie/blink": "^1.1.2", diff --git a/src/Assets/ExtractInfo.php b/src/Assets/ExtractInfo.php index c41d76ef3b9..478a2c7d749 100644 --- a/src/Assets/ExtractInfo.php +++ b/src/Assets/ExtractInfo.php @@ -2,16 +2,15 @@ namespace Statamic\Assets; -use Owenoj\LaravelGetId3\GetId3; use Statamic\Contracts\Assets\Asset; class ExtractInfo { public function fromAsset(Asset $asset): array { - return GetId3::fromDiskAndPath( - $asset->container()->diskHandle(), - $asset->path() - )->extractInfo(); + $disk = $asset->disk()->filesystem(); + $path = $asset->path(); + + return (new \getID3)->analyze($path, $disk->getSize($path), '', $disk->readStream($path)); } } From a1c02bd93a3b35bbe5fc1e3471f5c2f9a0832d68 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 25 Jan 2022 17:11:58 -0500 Subject: [PATCH 19/25] Remove provider --- tests/TestCase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 6912362c2d5..831fc50e41b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -54,7 +54,6 @@ protected function getPackageProviders($app) \Rebing\GraphQL\GraphQLServiceProvider::class, \Wilderborn\Partyline\ServiceProvider::class, \Archetype\ServiceProvider::class, - \Owenoj\LaravelGetId3\GetId3ServiceProvider::class, ]; } From 1075408b27605d29d709660b313b3b5853e0adc5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 26 Jan 2022 12:33:25 -0500 Subject: [PATCH 20/25] Videos can have dimensions --- src/Assets/Asset.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 831c007d32c..e71713b23b8 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -550,7 +550,7 @@ public function move($folder, $filename = null) */ public function dimensions() { - if (! $this->isImage() && ! $this->isSvg()) { + if (! $this->hasDimensions()) { return [null, null]; } @@ -602,7 +602,7 @@ public function orientation() */ public function ratio() { - if (! $this->isImage() && ! $this->isSvg()) { + if (! $this->hasDimensions()) { return null; } @@ -787,4 +787,9 @@ public function shallowAugmentedArrayKeys() { return ['id', 'url', 'permalink', 'api_url']; } + + private function hasDimensions() + { + return $this->isImage() || $this->isSvg() || $this->isVideo(); + } } From 6c5394ff9e4511e247fedbbc9060d8e07b8270d9 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 26 Jan 2022 14:26:32 -0500 Subject: [PATCH 21/25] remove temp var --- src/Assets/Asset.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index e71713b23b8..5412fce510d 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -160,16 +160,14 @@ public function generateMeta() if ($this->exists()) { $attributes = app(Attributes::class)->asset($this)->get(); - $otherMeta = [ + $meta = array_merge($meta, [ 'height' => Arr::get($attributes, 'height'), 'last_modified' => $this->disk()->lastModified($this->path()), 'duration' => Arr::get($attributes, 'duration'), 'mime_type' => $this->disk()->mimeType($this->path()), 'size' => $this->disk()->size($this->path()), 'width' => Arr::get($attributes, 'width'), - ]; - - $meta = array_merge($meta, $otherMeta); + ]); } return $meta; From 79e36f3b695a2ec61fd6eeaa00d765e0fd1ce9f3 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 26 Jan 2022 14:27:30 -0500 Subject: [PATCH 22/25] reorder --- src/Assets/Asset.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 5412fce510d..52c86cc3021 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -161,12 +161,12 @@ public function generateMeta() $attributes = app(Attributes::class)->asset($this)->get(); $meta = array_merge($meta, [ - 'height' => Arr::get($attributes, 'height'), - 'last_modified' => $this->disk()->lastModified($this->path()), - 'duration' => Arr::get($attributes, 'duration'), - 'mime_type' => $this->disk()->mimeType($this->path()), 'size' => $this->disk()->size($this->path()), + 'last_modified' => $this->disk()->lastModified($this->path()), 'width' => Arr::get($attributes, 'width'), + 'height' => Arr::get($attributes, 'height'), + 'mime_type' => $this->disk()->mimeType($this->path()), + 'duration' => Arr::get($attributes, 'duration'), ]); } From 0db299b8a90cda35bcda8f3215e9e740e41ef645 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 26 Jan 2022 14:31:09 -0500 Subject: [PATCH 23/25] use real time facade like it was --- src/Assets/Asset.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 52c86cc3021..820a4af4b3b 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -2,6 +2,7 @@ namespace Statamic\Assets; +use Facades\Statamic\Assets\Attributes; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; use Statamic\Contracts\Assets\Asset as AssetContract; @@ -158,7 +159,7 @@ public function generateMeta() $meta = ['data' => $this->data->all()]; if ($this->exists()) { - $attributes = app(Attributes::class)->asset($this)->get(); + $attributes = Attributes::asset($this)->get(); $meta = array_merge($meta, [ 'size' => $this->disk()->size($this->path()), From 2e682326e1e3d34d7752b664fee89c96b3f78da1 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 26 Jan 2022 14:31:19 -0500 Subject: [PATCH 24/25] reorder in test too --- tests/Assets/AssetRepositoryTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Assets/AssetRepositoryTest.php b/tests/Assets/AssetRepositoryTest.php index dd79a6656fc..7daa92cea0e 100644 --- a/tests/Assets/AssetRepositoryTest.php +++ b/tests/Assets/AssetRepositoryTest.php @@ -34,12 +34,12 @@ public function it_saves_the_meta_file_to_disk() $disk->assertExists($path = 'foo/.meta/image.jpg.yaml'); $contents = <<assertEquals($contents, $disk->get($path)); From f8afa9ba7f557c1e4b2c02fac4fec60908368e64 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 26 Jan 2022 14:46:48 -0500 Subject: [PATCH 25/25] don't really need these --- src/Assets/Attributes.php | 20 -------------------- tests/Assets/AttributesTest.php | 8 -------- 2 files changed, 28 deletions(-) diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index eb445f998fd..2e3af7a8f2c 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -56,26 +56,6 @@ public function get() return []; } - /** - * Get the width of the asset. - * - * @return int - */ - public function width() - { - return array_get($this->get(), 'width'); - } - - /** - * Get the height of the asset. - * - * @return int - */ - public function height() - { - return array_get($this->get(), 'height'); - } - /** * Get the attributes of a sound. * diff --git a/tests/Assets/AttributesTest.php b/tests/Assets/AttributesTest.php index 3839b41b9c5..8666c3ca431 100644 --- a/tests/Assets/AttributesTest.php +++ b/tests/Assets/AttributesTest.php @@ -39,8 +39,6 @@ public function a_non_image_asset_has_no_attributes() $attributes = $this->attributes->asset($asset); $this->assertEquals([], $attributes->get()); - $this->assertEquals(null, $attributes->width()); - $this->assertEquals(null, $attributes->height()); } /** @test */ @@ -65,8 +63,6 @@ public function it_gets_the_attributes() $attributes = $this->attributes->asset($asset); $this->assertEquals(['width' => 30, 'height' => 60], $attributes->get()); - $this->assertEquals(30, $attributes->width()); - $this->assertEquals(60, $attributes->height()); } /** @test */ @@ -81,8 +77,6 @@ public function it_gets_the_attributes_of_audio_file() $attributes = $this->attributes->asset($asset); $this->assertEquals(['duration' => 13], $attributes->get()); - $this->assertNull($attributes->width()); - $this->assertNull($attributes->height()); } /** @test */ @@ -103,8 +97,6 @@ public function it_gets_the_attributes_of_video_file() $attributes = $this->attributes->asset($asset); $this->assertEquals(['duration' => 13, 'width' => 1920, 'height' => 1080], $attributes->get()); - $this->assertEquals(1920, $attributes->width()); - $this->assertEquals(1080, $attributes->height()); } /** @test */