Skip to content

Commit

Permalink
fix(subsonic): send valid content-type with http.ServeStream
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Dec 27, 2022
1 parent 63f7b05 commit 8dc58c7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 50 deletions.
21 changes: 7 additions & 14 deletions db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ package db

import (
"path"
"path/filepath"
"strconv"
"strings"
"time"

// TODO: remove this dep

"go.senan.xyz/gonic/mime"

// TODO: remove this dep
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
)

Expand Down Expand Up @@ -123,19 +124,15 @@ func (t *Track) ArtistSID() *specid.ID {
}

func (t *Track) Ext() string {
longExt := path.Ext(t.Filename)
if len(longExt) < 1 {
return ""
}
return longExt[1:]
return filepath.Ext(t.Filename)
}

func (t *Track) AudioFilename() string {
return t.Filename
}

func (t *Track) MIME() string {
return mime.FromExtension(t.Ext())
return mime.TypeByExtension(filepath.Ext(t.Filename))
}

func (t *Track) AbsPath() string {
Expand Down Expand Up @@ -419,15 +416,11 @@ func (pe *PodcastEpisode) AudioFilename() string {
}

func (pe *PodcastEpisode) Ext() string {
longExt := path.Ext(pe.Filename)
if len(longExt) < 1 {
return ""
}
return longExt[1:]
return filepath.Ext(pe.Filename)
}

func (pe *PodcastEpisode) MIME() string {
return mime.FromExtension(pe.Ext())
return mime.TypeByExtension(filepath.Ext(pe.Filename))
}

type Bookmark struct {
Expand Down
52 changes: 33 additions & 19 deletions mime/mime.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
//nolint:gochecknoglobals
package mime

func FromExtension(ext string) string {
switch ext {
case "mp3":
return "audio/mpeg"
case "flac":
return "audio/x-flac"
case "aac":
return "audio/x-aac"
case "m4a":
return "audio/m4a"
case "m4b":
return "audio/m4b"
case "ogg":
return "audio/ogg"
case "opus":
return "audio/ogg"
case "wma":
return "audio/x-ms-wma"
default:
import (
"log"
stdmime "mime"
)

var supportedAudioTypes = map[string]string{
".mp3": "audio/mpeg",
".flac": "audio/x-flac",
".aac": "audio/x-aac",
".m4a": "audio/m4a",
".m4b": "audio/m4b",
".ogg": "audio/ogg",
".opus": "audio/ogg",
".wma": "audio/x-ms-wma",
}

//nolint:gochecknoinits
func init() {
for ext, mime := range supportedAudioTypes {
if err := stdmime.AddExtensionType(ext, mime); err != nil {
log.Fatalf("adding audio type mime for ext %q: %v", ext, err)
}
}
}

var TypeByExtension = stdmime.TypeByExtension
var ParseMediaType = stdmime.ParseMediaType
var FormatMediaType = stdmime.FormatMediaType

func TypeByAudioExtension(ext string) string {
if _, ok := supportedAudioTypes[ext]; !ok {
return ""
}
return stdmime.TypeByExtension(ext)
}
8 changes: 2 additions & 6 deletions podcasts/podcasts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"log"
"mime"
"net/http"
"net/url"
"os"
Expand All @@ -19,7 +18,7 @@ import (
"github.com/mmcdole/gofeed"

"go.senan.xyz/gonic/db"
gmime "go.senan.xyz/gonic/mime"
"go.senan.xyz/gonic/mime"
"go.senan.xyz/gonic/multierr"
"go.senan.xyz/gonic/scanner/tags"
)
Expand Down Expand Up @@ -236,10 +235,7 @@ func (p *Podcasts) AddEpisode(podcastID int, item *gofeed.Item) (*db.PodcastEpis
}

func isAudio(mediaType, url string) bool {
if mediaType != "" && strings.HasPrefix(mediaType, "audio") {
return true
}
return gmime.FromExtension(filepath.Ext(url)[1:]) != ""
return mime.TypeByAudioExtension(path.Ext(url)) != ""
}

func itemToEpisode(podcastID, size, duration int, audio string,
Expand Down
9 changes: 1 addition & 8 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (s *Scanner) scanDir(tx *db.DB, c *Context, musicDir string, absPath string
cover = item.Name()
continue
}
if mime := mime.FromExtension(ext(item.Name())); mime != "" {
if mime := mime.TypeByAudioExtension(filepath.Ext(item.Name())); mime != "" {
tracks = append(tracks, item.Name())
continue
}
Expand Down Expand Up @@ -578,13 +578,6 @@ func (s *Scanner) cleanGenres(c *Context) error {
return nil
}

func ext(name string) string {
if ext := filepath.Ext(name); len(ext) > 0 {
return ext[1:]
}
return ""
}

func isCover(name string) bool {
switch path := strings.ToLower(name); path {
case
Expand Down
2 changes: 1 addition & 1 deletion server/ctrlsubsonic/spec/construct_by_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewTCTrackByFolder(t *db.Track, parent *db.Album) *TrackChild {
trCh := &TrackChild{
ID: t.SID(),
ContentType: t.MIME(),
Suffix: t.Ext(),
Suffix: formatExt(t.Ext()),
Size: t.Size,
Artist: t.TagTrackArtist,
Title: t.TagTitle,
Expand Down
2 changes: 1 addition & 1 deletion server/ctrlsubsonic/spec/construct_by_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewTrackByTags(t *db.Track, album *db.Album) *TrackChild {
ret := &TrackChild{
ID: t.SID(),
ContentType: t.MIME(),
Suffix: t.Ext(),
Suffix: formatExt(t.Ext()),
ParentID: t.AlbumSID(),
CreatedAt: t.CreatedAt,
Size: t.Size,
Expand Down
2 changes: 1 addition & 1 deletion server/ctrlsubsonic/spec/construct_podcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewPodcastEpisode(e *db.PodcastEpisode) *PodcastEpisode {
Genre: "Podcast",
Duration: e.Length,
Year: e.PublishDate.Year(),
Suffix: e.Ext(),
Suffix: formatExt(e.Ext()),
BitRate: e.Bitrate,
IsDir: false,
Path: e.Path,
Expand Down
5 changes: 5 additions & 0 deletions server/ctrlsubsonic/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spec

import (
"fmt"
"strings"
"time"

"go.senan.xyz/gonic"
Expand Down Expand Up @@ -422,3 +423,7 @@ func formatRating(rating float64) string {
}
return fmt.Sprintf("%.2f", rating)
}

func formatExt(ext string) string {
return strings.TrimPrefix(ext, ".")
}

0 comments on commit 8dc58c7

Please sign in to comment.