From 6770bae66a1822f9603948d4e4ca33bb809dd339 Mon Sep 17 00:00:00 2001 From: jiangyong Date: Mon, 16 Mar 2026 12:27:51 +0800 Subject: [PATCH] content-type --- storage/util.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/storage/util.go b/storage/util.go index de9ad2c..c749723 100644 --- a/storage/util.go +++ b/storage/util.go @@ -21,16 +21,98 @@ func contentType2Ext(contentType string) string { func ext2ContentType(ext string) string { ext = strings.ToLower(ext) - if ext == "jpg" || ext == "jpeg" { + switch ext { + // 图片格式 + case "jpg", "jpeg": return "image/jpeg" - } else if ext == "png" { + case "png": return "image/png" - } else if ext == "mp3" { + case "gif": + return "image/gif" + case "webp": + return "image/webp" + case "bmp": + return "image/bmp" + case "svg", "svgz": + return "image/svg+xml" + case "ico": + return "image/x-icon" + case "tiff", "tif": + return "image/tiff" + case "avif": + return "image/avif" + case "apng": + return "image/apng" + // 音频格式 + case "mp3": return "audio/mpeg" - } else if ext == "mp4" { + case "wav": + return "audio/wav" + case "ogg": + return "audio/ogg" + case "flac": + return "audio/flac" + case "aac": + return "audio/aac" + case "m4a": + return "audio/mp4" + // 视频格式 + case "mp4": return "video/mp4" + case "avi": + return "video/x-msvideo" + case "mov", "qt": + return "video/quicktime" + case "webm": + return "video/webm" + case "flv": + return "video/x-flv" + case "mkv": + return "video/x-matroska" + case "wmv": + return "video/x-ms-wmv" + case "m3u8": + return "application/vnd.apple.mpegurl" + case "ts": + return "video/mp2t" + // 文档格式 + case "pdf": + return "application/pdf" + case "doc": + return "application/msword" + case "docx": + return "application/vnd.openxmlformats-officedocument.wordprocessingml.document" + case "xls": + return "application/vnd.ms-excel" + case "xlsx": + return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + case "ppt": + return "application/vnd.ms-powerpoint" + case "pptx": + return "application/vnd.openxmlformats-officedocument.presentationml.presentation" + case "txt": + return "text/plain" + case "html", "htm": + return "text/html" + case "css": + return "text/css" + case "js": + return "application/javascript" + case "json": + return "application/json" + case "xml": + return "application/xml" + case "zip": + return "application/zip" + case "rar": + return "application/x-rar-compressed" + case "7z": + return "application/x-7z-compressed" + case "gz": + return "application/gzip" + default: + return "" } - return "" } func Download(url, path string) error {