openclassify/Modules/Site/App/Support/ScopedMediaPathGenerator.php

35 lines
868 B
PHP

<?php
namespace Modules\Site\App\Support;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
class ScopedMediaPathGenerator implements PathGenerator
{
public function getPath(Media $media): string
{
return $this->basePath($media).'/';
}
public function getPathForConversions(Media $media): string
{
return $this->basePath($media).'/conversions/';
}
public function getPathForResponsiveImages(Media $media): string
{
return $this->basePath($media).'/responsive-images/';
}
private function basePath(Media $media): string
{
$scope = trim((string) $media->getCustomProperty('path_scope', ''));
$key = (string) $media->getKey();
return $scope !== ''
? $scope.'/'.$key
: $key;
}
}