Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getPathRelativeToRoot as a public method in the Media model #3026

Merged
merged 4 commits into from
Sep 12, 2022
Merged
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
8 changes: 5 additions & 3 deletions src/MediaCollections/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/Media/GetPathRelativeToRootTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidConversion;

it('can get a path of an original item relative to the filesystem\'s root', function () {
$media = $this->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);
});