Created
November 5, 2024 09:56
Custom path generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\MediaLibrary; | |
use Illuminate\Support\Str; | |
use Spatie\MediaLibrary\MediaCollections\Models\Media; | |
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator; | |
class CustomPathGenerator implements PathGenerator | |
{ | |
public function getPathForConversions(Media $media): string | |
{ | |
return $this->getPath($media) . 'conversions/'; | |
} | |
public function getPath(Media $media): string | |
{ | |
return $this->getModelName($media->model_type) . '/' . $media->created_at->format('Y') . $media->created_at->format('m') . '/' . $media->id . '/'; | |
} | |
public function getPathForResponsiveImages(Media $media): string | |
{ | |
return $this->getPath($media) . 'responsive-images/'; | |
} | |
function getModelName($namespace): string | |
{ | |
return Str::afterLast($namespace, '\\'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment