40 lines
1004 B
PHP
40 lines
1004 B
PHP
<?php
|
|
|
|
namespace App\Media\PathGenerators;
|
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
|
|
|
|
class CustomPathGenerator implements PathGenerator
|
|
{
|
|
public function getPath(Media $media): string
|
|
{
|
|
$model = $media->model;
|
|
|
|
if ($model && method_exists($model, 'getMediaPath')) {
|
|
return $model->getMediaPath($media).'/';
|
|
}
|
|
|
|
return $this->defaultPath($media);
|
|
}
|
|
|
|
public function getPathForConversions(Media $media): string
|
|
{
|
|
return $this->getPath($media).'conversions/';
|
|
}
|
|
|
|
public function getPathForResponsiveImages(Media $media): string
|
|
{
|
|
return $this->getPath($media).'responsive/';
|
|
}
|
|
|
|
private function defaultPath(Media $media): string
|
|
{
|
|
$module = strtolower(class_basename($media->model_type));
|
|
$date = $media->created_at->format('Y/m/d');
|
|
$id = $media->id;
|
|
|
|
return "{$module}/{$date}/{$id}/";
|
|
}
|
|
}
|