feat(media): use legacy filepath structure for attachments via custom path generator
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?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/';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user