Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the Go-builtin (more extensive) file extension -> mimetype mapping #526

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions handlers/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"mime"
"net"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -57,14 +58,10 @@ func (s Server) entryGet() http.HandlerFunc {
func inferContentTypeFromFilename(f picoshare.Filename) (picoshare.ContentType, error) {
// For files that modern browser can play natively, infer the content type if
// none was specified at upload time.
switch filepath.Ext(f.String()) {
case ".mp4":
return picoshare.ContentType("video/mp4"), nil
case ".mp3":
return picoshare.ContentType("audio/mpeg"), nil
default:
return picoshare.ContentType(""), errors.New("could not infer content type from filename")
if mimetype := mime.TypeByExtension(filepath.Ext(f.String())); mimetype != "" {
return picoshare.ContentType(mimetype), nil
}
return picoshare.ContentType(""), errors.New("could not infer content type from filename")
}

func recordDownload(db store.Store, id picoshare.EntryID, remoteAddr, userAgent string) error {
Expand Down
Loading