chore: sync release updates
This commit is contained in:
@@ -3,54 +3,20 @@
|
||||
namespace app\support;
|
||||
|
||||
use support\Request;
|
||||
use function pathinfo;
|
||||
use function parse_url;
|
||||
use function str_starts_with;
|
||||
use function strtolower;
|
||||
|
||||
class AppraisalEvidenceService
|
||||
{
|
||||
private const IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'bmp', 'heic'];
|
||||
private const VIDEO_EXTENSIONS = ['mp4', 'mov', 'm4v', 'webm', 'avi', 'mpeg', 'mpg'];
|
||||
private const PDF_EXTENSIONS = ['pdf'];
|
||||
|
||||
public function upload(Request $request, string $inputName = 'file'): array
|
||||
public function upload(Request $request, string $inputName = 'file', string $scene = 'appraisal_evidence'): array
|
||||
{
|
||||
$file = $request->file($inputName);
|
||||
if (!$file || !$file->isValid()) {
|
||||
throw new \RuntimeException('上传文件无效');
|
||||
}
|
||||
|
||||
$extension = strtolower($file->getUploadExtension() ?: '');
|
||||
$fileType = $this->detectFileType($extension);
|
||||
if ($fileType === 'file') {
|
||||
throw new \RuntimeException('仅支持上传图片、视频或 PDF 文件');
|
||||
}
|
||||
|
||||
$filename = sprintf('evidence_%s.%s', uniqid(), $extension ?: 'dat');
|
||||
$relativeDir = 'uploads/appraisal-evidence/' . date('Ymd');
|
||||
$relativePath = $relativeDir . '/' . $filename;
|
||||
$this->storage()->putUploadedFile($file, $relativePath);
|
||||
|
||||
$fileUrl = $this->storage()->publicUrl($request, $relativePath);
|
||||
|
||||
return [
|
||||
'file_id' => md5($relativePath),
|
||||
'file_url' => $fileUrl,
|
||||
'thumbnail_url' => $fileType === 'image' ? $fileUrl : '',
|
||||
'name' => $file->getUploadName(),
|
||||
'file_type' => $fileType,
|
||||
'mime_type' => $this->mimeType($fileType, $extension),
|
||||
];
|
||||
return $this->fileUploadService()->upload($request, $scene, $inputName);
|
||||
}
|
||||
|
||||
public function delete(string $fileUrl): void
|
||||
{
|
||||
$relativePath = $this->storage()->storagePath($fileUrl);
|
||||
if (!str_starts_with($relativePath, 'uploads/appraisal-evidence/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->storage()->delete($relativePath);
|
||||
$this->fileUploadService()->delete($fileUrl);
|
||||
}
|
||||
|
||||
public function normalize(mixed $attachments, ?Request $request = null, bool $forStorage = false): array
|
||||
@@ -112,30 +78,21 @@ class AppraisalEvidenceService
|
||||
|
||||
public function detectFileType(string $extension): string
|
||||
{
|
||||
if (in_array($extension, self::IMAGE_EXTENSIONS, true)) {
|
||||
return 'image';
|
||||
}
|
||||
if (in_array($extension, self::VIDEO_EXTENSIONS, true)) {
|
||||
return 'video';
|
||||
}
|
||||
if (in_array($extension, self::PDF_EXTENSIONS, true)) {
|
||||
return 'pdf';
|
||||
}
|
||||
return 'file';
|
||||
return $this->fileUploadService()->detectFileType($extension);
|
||||
}
|
||||
|
||||
private function mimeType(string $fileType, string $extension): string
|
||||
{
|
||||
return match ($fileType) {
|
||||
'image' => 'image/' . ($extension === 'jpg' ? 'jpeg' : ($extension ?: 'jpeg')),
|
||||
'video' => 'video/' . ($extension ?: 'mp4'),
|
||||
'pdf' => 'application/pdf',
|
||||
default => 'application/octet-stream',
|
||||
};
|
||||
return $this->fileUploadService()->mimeType($fileType, $extension);
|
||||
}
|
||||
|
||||
private function storage(): FileStorageService
|
||||
{
|
||||
return new FileStorageService();
|
||||
}
|
||||
|
||||
private function fileUploadService(): FileUploadService
|
||||
{
|
||||
return new FileUploadService();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user