DetectFileType detects the MIME type from file content
(data []byte)
| 164 | |
| 165 | // DetectFileType detects the MIME type from file content |
| 166 | func DetectFileType(data []byte) string { |
| 167 | // First, try http.DetectContentType (good for text files) |
| 168 | httpType := http.DetectContentType(data) |
| 169 | |
| 170 | // If http detected text, use that |
| 171 | if httpType != "application/octet-stream" { |
| 172 | return httpType |
| 173 | } |
| 174 | |
| 175 | // For binary files, use filetype library for specific format detection |
| 176 | kind, err := filetype.Match(data) |
| 177 | if err == nil && kind != filetype.Unknown { |
| 178 | return kind.MIME.Value |
| 179 | } |
| 180 | |
| 181 | // Default to application/octet-stream for unknown types |
| 182 | return "application/octet-stream" |
| 183 | } |
| 184 | |
| 185 | func GetTmpFileHandle(baseDir string) (*os.File, error) { |
| 186 | tmpDir, err := getTempDir(baseDir) |