37 lines
1006 B
PHP
37 lines
1006 B
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
|
|
|
|
class MediaPathGenerator implements PathGenerator
|
|
{
|
|
public function getPath(Media $media): string
|
|
{
|
|
$date = $media->created_at
|
|
? $media->created_at->format('Y-m-d')
|
|
: now()->format('Y-m-d');
|
|
|
|
$modelName = strtolower(class_basename($media->model_type));
|
|
|
|
$collectionName = $media->collection_name;
|
|
|
|
if ($collectionName === 'default' || $collectionName === $modelName) {
|
|
return "{$modelName}/{$date}/{$media->model_id}/";
|
|
}
|
|
|
|
return "{$modelName}/{$collectionName}/{$date}/{$media->model_id}/";
|
|
}
|
|
|
|
public function getPathForConversions(Media $media): string
|
|
{
|
|
return $this->getPath($media).'conversions/';
|
|
}
|
|
|
|
public function getPathForResponsiveImages(Media $media): string
|
|
{
|
|
return $this->getPath($media).'responsive/';
|
|
}
|
|
}
|