抓取车辆

This commit is contained in:
2026-03-11 15:01:54 +08:00
parent bfbed7fd2d
commit 260e76449b
5 changed files with 249 additions and 12 deletions
+45 -1
View File
@@ -46,7 +46,8 @@ class UploadService
$extensionFromPath = pathinfo($basename, PATHINFO_EXTENSION);
$extension = $extensionFromName ?: $extensionFromPath;
$mime = mime_content_type($filePath) ?: 'application/octet-stream';
// 获取 MIME 类型,如果函数不可用则使用扩展名判断
$mime = $this->getMimeType($filePath, $extension);
$size = filesize($filePath) ?: 0;
$sceneUrl = $scene ? ($scene . '/') : '';
@@ -116,6 +117,49 @@ class UploadService
}
}
/**
* 获取文件的 MIME 类型
* 优先使用 mime_content_type(),不可用时根据扩展名判断
*
* @param string $filePath 文件路径
* @param string $extension 文件扩展名
* @return string
*/
private function getMimeType(string $filePath, string $extension = ''): string
{
// 优先使用系统函数
if (function_exists('mime_content_type')) {
$mime = mime_content_type($filePath);
if ($mime !== false && $mime !== '') {
return $mime;
}
}
// 降级:根据扩展名判断
$extension = strtolower(ltrim($extension, '.'));
$mimeTypes = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
'bmp' => 'image/bmp',
'svg' => 'image/svg+xml',
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xls' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'mp4' => 'video/mp4',
'mp3' => 'audio/mpeg',
'txt' => 'text/plain',
];
return $mimeTypes[$extension] ?? 'application/octet-stream';
}
/**
* 删除 OSS 上的文件
*