Skip to content

Commit

Permalink
Restore content type from filename extension fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Galiley authored and vishen committed Nov 29, 2020
1 parent 67dbd6e commit d574022
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/h2non/filetype"
"github.com/h2non/filetype/types"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -559,6 +558,8 @@ func (a *Application) PlayableMediaType(filename string) bool {
func (a *Application) possibleContentType(filename string) (string, error) {
//If filename is an URL returns the content-type from the HEAD response headers
//Otherwise returns the content-type thanks to filetype package
var contentType string

if strings.Contains(filename, "://") {
req, err := http.NewRequest("HEAD", filename, nil)
if err != nil {
Expand All @@ -569,17 +570,49 @@ func (a *Application) possibleContentType(filename string) (string, error) {
if err != nil {
return "", errors.Wrap(err, "do request failed")
}
return res.Header.Get("Content-Type"), nil
contentType = res.Header.Get("Content-Type")
} else {
match, err := filetype.MatchFile(filename)
if err != nil {
return "", errors.Wrap(err, "match failed")
}
if match.MIME == types.Unknown.MIME {
return "", errors.Wrap(err, "unknown content-type")
contentType = match.MIME.Value
}
if contentType == "" {
if strings.Contains(filename, "://") && strings.Contains(filename, "?") {
parts := strings.Split(filename, "?")
filename = parts[0]
}

// https://developers.google.com/cast/docs/media
switch ext := strings.ToLower(path.Ext(filename)); ext {
case ".jpg", ".jpeg":
return "image/jpeg", nil
case ".gif":
return "image/gif", nil
case ".bmp":
return "image/bmp", nil
case ".png":
return "image/png", nil
case ".webp":
return "image/webp", nil
case ".mp4", ".m4a", ".m4p":
return "video/mp4", nil
case ".webm":
return "video/webm", nil
case ".mp3":
return "audio/mp3", nil
case ".flac":
return "audio/flac", nil
case ".wav":
return "audio/wav", nil
case ".m3u8":
return "application/x-mpegURL", nil
default:
return "", fmt.Errorf("unknown file extension %q", ext)
}
return match.MIME.Value, nil
}
return contentType, nil
}

func (a *Application) knownFileType(filename string) bool {
Expand Down

0 comments on commit d574022

Please sign in to comment.