package utils import ( "path/filepath" "strings" ) // GetExtension 获取文件扩展名(不含点号, 全小写) // 例如: "photo.JPG" -> "jpg" func GetExtension(filename string) string { ext := filepath.Ext(filename) if ext == "" { return "" } return strings.ToLower(ext[1:]) // 去掉开头的点号 } // GetMimeType 根据文件扩展名获取MIME类型 // 简单映射, 覆盖常见文件类型 func GetMimeType(extension string) string { mimeMap := map[string]string{ // 图片 "jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "gif": "image/gif", "bmp": "image/bmp", "webp": "image/webp", "svg": "image/svg+xml", "ico": "image/x-icon", // 文档 "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", "ppt": "application/vnd.ms-powerpoint", "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", // 文本 "txt": "text/plain", "html": "text/html", "htm": "text/html", "css": "text/css", "js": "application/javascript", "json": "application/json", "xml": "application/xml", "csv": "text/csv", "md": "text/markdown", // 压缩包 "zip": "application/zip", "rar": "application/x-rar-compressed", "7z": "application/x-7z-compressed", "tar": "application/x-tar", "gz": "application/gzip", // 音视频 "mp3": "audio/mpeg", "wav": "audio/wav", "mp4": "video/mp4", "avi": "video/x-msvideo", "mov": "video/quicktime", "wmv": "video/x-ms-wmv", "flv": "video/x-flv", "mkv": "video/x-matroska", } if mime, ok := mimeMap[extension]; ok { return mime } return "application/octet-stream" } // IsImageFile 判断是否为图片文件 func IsImageFile(extension string) bool { imageExts := []string{"jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "ico"} for _, ext := range imageExts { if extension == ext { return true } } return false } // IsVideoFile 判断是否为视频文件 func IsVideoFile(extension string) bool { videoExts := []string{"mp4", "avi", "mov", "wmv", "flv", "mkv"} for _, ext := range videoExts { if extension == ext { return true } } return false } // IsDocumentFile 判断是否为文档文件 func IsDocumentFile(extension string) bool { docExts := []string{"pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "md", "csv"} for _, ext := range docExts { if extension == ext { return true } } return false }