feat(media): use legacy filepath structure for attachments via custom path generator
Build and Deploy / verify (push) Failing after 11m47s
Build and Deploy / deploy (push) Skipped

This commit is contained in:
2026-10-12 08:58:19 +00:00
parent 3aed6771b0
commit dec02f583a
2 changed files with 40 additions and 3 deletions
@@ -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/';
}
}
+1 -3
View File
@@ -155,9 +155,7 @@ return [
* Here you can specify which path generator should be used for the given class.
*/
'custom_path_generators' => [
// Model::class => PathGenerator::class
// or
// 'model_morph_alias' => PathGenerator::class
\App\Models\Post::class => \App\Support\LegacyAttachmentPathGenerator::class,
],
/*