Refactor file download handlers to utilize getMimeType function for content type determination, enhancing support for various file formats.
This commit is contained in:
@@ -1827,40 +1827,12 @@ func downloadOrgFileHandler(w http.ResponseWriter, r *http.Request, db *database
|
||||
|
||||
// Set appropriate headers for inline viewing
|
||||
fileName := path.Base(filePath)
|
||||
// Determine content type based on file extension
|
||||
contentType := "application/octet-stream"
|
||||
switch strings.ToLower(path.Ext(fileName)) {
|
||||
case ".pdf":
|
||||
contentType = "application/pdf"
|
||||
case ".png":
|
||||
contentType = "image/png"
|
||||
case ".jpg", ".jpeg":
|
||||
contentType = "image/jpeg"
|
||||
case ".mp4":
|
||||
contentType = "video/mp4"
|
||||
case ".webm":
|
||||
contentType = "video/webm"
|
||||
case ".ogg":
|
||||
contentType = "video/ogg"
|
||||
case ".avi":
|
||||
contentType = "video/avi"
|
||||
case ".mov":
|
||||
contentType = "video/quicktime"
|
||||
case ".mkv":
|
||||
contentType = "video/x-matroska"
|
||||
case ".flv":
|
||||
contentType = "video/x-flv"
|
||||
case ".wmv":
|
||||
contentType = "video/x-ms-wmv"
|
||||
case ".m4v":
|
||||
contentType = "video/x-m4v"
|
||||
}
|
||||
// Determine content type based on file extension - use our getMimeType function
|
||||
// which supports all common video/audio/image formats
|
||||
contentType := getMimeType(fileName)
|
||||
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", fileName))
|
||||
if ct := resp.Header.Get("Content-Type"); ct != "" {
|
||||
w.Header().Set("Content-Type", ct)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
if cr := resp.Header.Get("Content-Range"); cr != "" {
|
||||
w.Header().Set("Content-Range", cr)
|
||||
@@ -2002,40 +1974,12 @@ func downloadUserFileHandler(w http.ResponseWriter, r *http.Request, db *databas
|
||||
|
||||
// Set appropriate headers for inline viewing
|
||||
fileName := path.Base(filePath)
|
||||
// Determine content type based on file extension
|
||||
contentType := "application/octet-stream"
|
||||
switch strings.ToLower(path.Ext(fileName)) {
|
||||
case ".pdf":
|
||||
contentType = "application/pdf"
|
||||
case ".png":
|
||||
contentType = "image/png"
|
||||
case ".jpg", ".jpeg":
|
||||
contentType = "image/jpeg"
|
||||
case ".mp4":
|
||||
contentType = "video/mp4"
|
||||
case ".webm":
|
||||
contentType = "video/webm"
|
||||
case ".ogg":
|
||||
contentType = "video/ogg"
|
||||
case ".avi":
|
||||
contentType = "video/avi"
|
||||
case ".mov":
|
||||
contentType = "video/quicktime"
|
||||
case ".mkv":
|
||||
contentType = "video/x-matroska"
|
||||
case ".flv":
|
||||
contentType = "video/x-flv"
|
||||
case ".wmv":
|
||||
contentType = "video/x-ms-wmv"
|
||||
case ".m4v":
|
||||
contentType = "video/x-m4v"
|
||||
}
|
||||
// Determine content type based on file extension - use our getMimeType function
|
||||
// which supports all common video/audio/image formats
|
||||
contentType := getMimeType(fileName)
|
||||
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", fileName))
|
||||
if ct := resp.Header.Get("Content-Type"); ct != "" {
|
||||
w.Header().Set("Content-Type", ct)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
if cr := resp.Header.Get("Content-Range"); cr != "" {
|
||||
w.Header().Set("Content-Range", cr)
|
||||
@@ -2116,8 +2060,22 @@ func downloadUserFolderAsZip(w http.ResponseWriter, r *http.Request, db *databas
|
||||
func getMimeType(filename string) string {
|
||||
lower := strings.ToLower(filename)
|
||||
switch {
|
||||
// Documents
|
||||
case strings.HasSuffix(lower, ".pdf"):
|
||||
return "application/pdf"
|
||||
case strings.HasSuffix(lower, ".doc"), strings.HasSuffix(lower, ".docx"):
|
||||
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
case strings.HasSuffix(lower, ".xls"), strings.HasSuffix(lower, ".xlsx"):
|
||||
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
case strings.HasSuffix(lower, ".ppt"), strings.HasSuffix(lower, ".pptx"):
|
||||
return "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
||||
case strings.HasSuffix(lower, ".odt"):
|
||||
return "application/vnd.oasis.opendocument.text"
|
||||
case strings.HasSuffix(lower, ".ods"):
|
||||
return "application/vnd.oasis.opendocument.spreadsheet"
|
||||
case strings.HasSuffix(lower, ".odp"):
|
||||
return "application/vnd.oasis.opendocument.presentation"
|
||||
// Images
|
||||
case strings.HasSuffix(lower, ".png"):
|
||||
return "image/png"
|
||||
case strings.HasSuffix(lower, ".jpg"), strings.HasSuffix(lower, ".jpeg"):
|
||||
@@ -2128,20 +2086,72 @@ func getMimeType(filename string) string {
|
||||
return "image/webp"
|
||||
case strings.HasSuffix(lower, ".svg"):
|
||||
return "image/svg+xml"
|
||||
case strings.HasSuffix(lower, ".bmp"):
|
||||
return "image/bmp"
|
||||
case strings.HasSuffix(lower, ".ico"):
|
||||
return "image/x-icon"
|
||||
// Video formats
|
||||
case strings.HasSuffix(lower, ".mp4"), strings.HasSuffix(lower, ".m4v"):
|
||||
return "video/mp4"
|
||||
case strings.HasSuffix(lower, ".webm"):
|
||||
return "video/webm"
|
||||
case strings.HasSuffix(lower, ".ogv"):
|
||||
return "video/ogg"
|
||||
case strings.HasSuffix(lower, ".mov"):
|
||||
return "video/quicktime"
|
||||
case strings.HasSuffix(lower, ".avi"):
|
||||
return "video/x-msvideo"
|
||||
case strings.HasSuffix(lower, ".mkv"):
|
||||
return "video/x-matroska"
|
||||
case strings.HasSuffix(lower, ".wmv"):
|
||||
return "video/x-ms-wmv"
|
||||
case strings.HasSuffix(lower, ".flv"):
|
||||
return "video/x-flv"
|
||||
case strings.HasSuffix(lower, ".3gp"):
|
||||
return "video/3gpp"
|
||||
case strings.HasSuffix(lower, ".ts"):
|
||||
return "video/mp2t"
|
||||
case strings.HasSuffix(lower, ".mpg"), strings.HasSuffix(lower, ".mpeg"):
|
||||
return "video/mpeg"
|
||||
// Audio formats
|
||||
case strings.HasSuffix(lower, ".mp3"):
|
||||
return "audio/mpeg"
|
||||
case strings.HasSuffix(lower, ".wav"):
|
||||
return "audio/wav"
|
||||
case strings.HasSuffix(lower, ".ogg"), strings.HasSuffix(lower, ".oga"):
|
||||
return "audio/ogg"
|
||||
case strings.HasSuffix(lower, ".m4a"), strings.HasSuffix(lower, ".aac"):
|
||||
return "audio/aac"
|
||||
case strings.HasSuffix(lower, ".flac"):
|
||||
return "audio/flac"
|
||||
case strings.HasSuffix(lower, ".wma"):
|
||||
return "audio/x-ms-wma"
|
||||
// Text/code
|
||||
case strings.HasSuffix(lower, ".txt"):
|
||||
return "text/plain"
|
||||
case strings.HasSuffix(lower, ".html"):
|
||||
case strings.HasSuffix(lower, ".html"), strings.HasSuffix(lower, ".htm"):
|
||||
return "text/html"
|
||||
case strings.HasSuffix(lower, ".css"):
|
||||
return "text/css"
|
||||
case strings.HasSuffix(lower, ".js"):
|
||||
return "application/javascript"
|
||||
case strings.HasSuffix(lower, ".json"):
|
||||
return "application/json"
|
||||
case strings.HasSuffix(lower, ".xml"):
|
||||
return "application/xml"
|
||||
case strings.HasSuffix(lower, ".csv"):
|
||||
return "text/csv"
|
||||
case strings.HasSuffix(lower, ".doc"), strings.HasSuffix(lower, ".docx"):
|
||||
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
case strings.HasSuffix(lower, ".xls"), strings.HasSuffix(lower, ".xlsx"):
|
||||
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
case strings.HasSuffix(lower, ".ppt"), strings.HasSuffix(lower, ".pptx"):
|
||||
return "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
||||
// Archives
|
||||
case strings.HasSuffix(lower, ".zip"):
|
||||
return "application/zip"
|
||||
case strings.HasSuffix(lower, ".rar"):
|
||||
return "application/vnd.rar"
|
||||
case strings.HasSuffix(lower, ".7z"):
|
||||
return "application/x-7z-compressed"
|
||||
case strings.HasSuffix(lower, ".tar"):
|
||||
return "application/x-tar"
|
||||
case strings.HasSuffix(lower, ".gz"):
|
||||
return "application/gzip"
|
||||
default:
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user