From 0fb2b74e467176b8825638b22dcdc01adadcbdbd Mon Sep 17 00:00:00 2001 From: Emiel Molenaar Date: Tue, 6 Sep 2022 12:54:41 +0200 Subject: [PATCH 1/3] Adding getPathRelativeToRoot as a public method in the Media model for convenience --- src/MediaCollections/Models/Media.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/MediaCollections/Models/Media.php b/src/MediaCollections/Models/Media.php index ac354e6e7..3596b3138 100644 --- a/src/MediaCollections/Models/Media.php +++ b/src/MediaCollections/Models/Media.php @@ -96,6 +96,10 @@ public function getPath(string $conversionName = ''): string return $urlGenerator->getPath(); } + public function getPathRelativeToRoot(string $conversionName = ''): string + { + return $this->getUrlGenerator($conversionName)->getPathRelativeToRoot(); + } public function getUrlGenerator(string $conversionName): UrlGenerator { @@ -434,9 +438,7 @@ public static function findWithTemporaryUploadInCurrentSession(array $uuids) public function mailAttachment(string $conversion = ''): Attachment { - $path = $this->getUrlGenerator($conversion)->getPathRelativeToRoot(); - - $attachment = Attachment::fromStorageDisk($this->disk, $path)->as($this->file_name); + $attachment = Attachment::fromStorageDisk($this->disk, $this->getPathRelativeToRoot($conversion))->as($this->file_name); if ($this->mime_type) { $attachment->withMime($this->mime); From 206e10b06694ac4de4830b8947d6bc86b85f3cef Mon Sep 17 00:00:00 2001 From: Emiel Molenaar Date: Sat, 10 Sep 2022 09:14:57 +0200 Subject: [PATCH 2/3] Adds test for getPathRelativeToRoot --- .../Media/GetPathRelativeToRootTest.php | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/Feature/Media/GetPathRelativeToRootTest.php diff --git a/tests/Feature/Media/GetPathRelativeToRootTest.php b/tests/Feature/Media/GetPathRelativeToRootTest.php new file mode 100644 index 000000000..3a34d5a47 --- /dev/null +++ b/tests/Feature/Media/GetPathRelativeToRootTest.php @@ -0,0 +1,59 @@ +testModel->addMedia($this->getTestJpg())->toMediaCollection(); + + $expected = $this->makePathOsSafe("{$media->id}/test.jpg"); + + $actual = $this->makePathOsSafe($media->getPathRelativeToRoot()); + + expect($actual)->toEqual($expected); +}); + +it('can get a path of a derived image relative to the filesystem\'s root', function () { + $media = $this->testModelWithConversion->addMedia($this->getTestJpg())->toMediaCollection(); + + $conversionName = 'thumb'; + + $expected = $this->makePathOsSafe("{$media->id}/conversions/test-{$conversionName}.jpg"); + + $actual = $this->makePathOsSafe($media->getPathRelativeToRoot($conversionName)); + + expect($actual)->toEqual($expected); +}); + +it('returns an exception when getting a relative path for an unknown conversion', function () { + $media = $this->testModel->addMedia($this->getTestJpg())->toMediaCollection(); + + $this->expectException(InvalidConversion::class); + + $media->getPathRelativeToRoot('unknownConversionName'); +}); + +it('can get a path of an original item with prefix relative to the filesystem\'s root', function () { + config(['media-library.prefix' => 'prefix']); + + $media = $this->testModel->addMedia($this->getTestJpg())->toMediaCollection(); + + $expected = $this->makePathOsSafe("prefix/{$media->id}/test.jpg"); + + $actual = $this->makePathOsSafe($media->getPathRelativeToRoot()); + + expect($actual)->toEqual($expected); +}); + +it('can get a path of a derived image with prefix relative to the filesystem\'s root', function () { + config(['media-library.prefix' => 'prefix']); + + $media = $this->testModelWithConversion->addMedia($this->getTestJpg())->toMediaCollection(); + + $conversionName = 'thumb'; + + $expected = $this->makePathOsSafe("prefix/{$media->id}/conversions/test-{$conversionName}.jpg"); + + $actual = $this->makePathOsSafe($media->getPathRelativeToRoot($conversionName)); + + expect($actual)->toEqual($expected); +}); \ No newline at end of file From 9ac3b5183befcbfbf2bef2a9730a91dee19aea73 Mon Sep 17 00:00:00 2001 From: emielmolenaar Date: Sat, 10 Sep 2022 07:17:05 +0000 Subject: [PATCH 3/3] Fix styling --- tests/Feature/Media/GetPathRelativeToRootTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Media/GetPathRelativeToRootTest.php b/tests/Feature/Media/GetPathRelativeToRootTest.php index 3a34d5a47..4fe9ff5b2 100644 --- a/tests/Feature/Media/GetPathRelativeToRootTest.php +++ b/tests/Feature/Media/GetPathRelativeToRootTest.php @@ -56,4 +56,4 @@ $actual = $this->makePathOsSafe($media->getPathRelativeToRoot($conversionName)); expect($actual)->toEqual($expected); -}); \ No newline at end of file +});