feat: Implement CustomPathGenerator for dynamic media path generation in the Media Library.
This commit is contained in:
parent
3e00965a8a
commit
4cd80eae10
40
app/MediaLibrary/CustomPathGenerator.php
Normal file
40
app/MediaLibrary/CustomPathGenerator.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\MediaLibrary;
|
||||||
|
|
||||||
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
|
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
|
||||||
|
|
||||||
|
class CustomPathGenerator implements PathGenerator
|
||||||
|
{
|
||||||
|
public function getPath(Media $media): string
|
||||||
|
{
|
||||||
|
$feature = $media->getCustomProperty('feature', 'misc');
|
||||||
|
$date = $media->getCustomProperty('date', now()->toDateString());
|
||||||
|
$docType = $media->getCustomProperty('doc_type');
|
||||||
|
|
||||||
|
$feature = str($feature)->slug();
|
||||||
|
$date = str($date);
|
||||||
|
$docType = $docType ? str($docType)->slug() : null;
|
||||||
|
|
||||||
|
$path = "{$feature}/";
|
||||||
|
|
||||||
|
if ($docType) {
|
||||||
|
$path .= "{$docType}/";
|
||||||
|
}
|
||||||
|
|
||||||
|
$path .= "{$date}/";
|
||||||
|
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPathForConversions(Media $media): string
|
||||||
|
{
|
||||||
|
return $this->getPath($media).'conversions/';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPathForResponsiveImages(Media $media): string
|
||||||
|
{
|
||||||
|
return $this->getPath($media).'responsive/';
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user