Skip to content

Commit

Permalink
support prefixed path
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Dec 24, 2023
1 parent bdeb43c commit 79f35b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions config/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@
* The default collection name
*/
'default_collection_name' => 'default',

/**
* Prefix the generate path of files
* set to null if you don't want any prefix
* To fully customize the generated default path, extends the Media model ans override generateBasePath method
*/
'generated_path_prefix' => null,
];
13 changes: 11 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,20 @@ public function getPath(?string $conversion = null): ?string
*/
public function generateBasePath(?string $conversion = null): string
{
$prefix = config('media.generated_path_prefix', '');

$root = Str::of($prefix)
->when($prefix, fn ($string) => $string->finish('/'))
->append($this->uuid)->finish('/');

if ($conversion) {
return "{$this->uuid}/generated_conversions/".str_replace('.', '/', $this->getConversionKey($conversion)).'/';
return $root
->append('generated_conversions/')
->append(str_replace('.', '/', $this->getConversionKey($conversion)))
->finish('/');
}

return "{$this->uuid}/";
return $root;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@
Storage::disk('media')->assertExists($media->path);
});

it('store a file within a prefixed path', function () {
config()->set('media.generated_path_prefix', 'media');

/** @var Media $media */
$media = MediaFactory::new()->make();

Storage::fake('media');

$file = $this->getTestFile('images/svg.svg');

$media->storeFile(
file: $file,
disk: 'media',
name: 'foo'
);

expect($media->name)->toBe('foo');
expect($media->file_name)->toBe('foo.svg');
expect($media->path)->toBe("media/{$media->uuid}/foo.svg");

Storage::disk('media')->assertExists($media->path);
});

it('store a conversion image of a media', function () {
/** @var Media $media */
$media = MediaFactory::new()->make();
Expand Down

0 comments on commit 79f35b0

Please sign in to comment.