40 lines
1.0 KiB
PHP
40 lines
1.0 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Support;
|
||
|
||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
|
||
|
||
/**
|
||
* 附件路径生成器:优先用老 sablog 的原始 filepath(date_200802/xxx.jpg),
|
||
* 没有的话用 spatie 默认的 {id}/{file_name} 结构
|
||
*/
|
||
class LegacyAttachmentPathGenerator implements PathGenerator
|
||
{
|
||
public function getPath(Media $media): string
|
||
{
|
||
$legacy = $media->getCustomProperty('legacy_filepath');
|
||
if ($legacy) {
|
||
$legacy = ltrim($legacy, '/');
|
||
$dir = dirname($legacy);
|
||
if ($dir !== '.') {
|
||
return $dir . '/';
|
||
}
|
||
}
|
||
|
||
return $media->id . '/';
|
||
}
|
||
|
||
public function getPathForConversions(Media $media): string
|
||
{
|
||
return $this->getPath($media) . 'conversions/';
|
||
}
|
||
|
||
public function getPathForResponsiveImages(Media $media): string
|
||
{
|
||
return $this->getPath($media) . 'responsive-images/';
|
||
}
|
||
}
|