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); diff --git a/tests/Feature/Media/GetPathRelativeToRootTest.php b/tests/Feature/Media/GetPathRelativeToRootTest.php new file mode 100644 index 000000000..4fe9ff5b2 --- /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); +});