Files
laralog/app/Support/LegacyAttachmentPathGenerator.php
T
gouki dec02f583a
Build and Deploy / verify (push) Failing after 11m47s
Build and Deploy / deploy (push) Skipped
feat(media): use legacy filepath structure for attachments via custom path generator
2026-10-12 08:58:19 +00:00

40 lines
1.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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/';
}
}