feat(server): create M3Us from shares (#3652)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/navidrome/navidrome/consts"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
@@ -38,6 +39,26 @@ func (pub *Router) handleShares(w http.ResponseWriter, r *http.Request) {
|
||||
server.IndexWithShare(pub.ds, ui.BuildAssets(), s)(w, r)
|
||||
}
|
||||
|
||||
func (pub *Router) handleM3U(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := req.Params(r).String(":id")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// If it is not, consider it a share ID
|
||||
s, err := pub.share.Load(r.Context(), id)
|
||||
if err != nil {
|
||||
checkShareError(r.Context(), w, err, id)
|
||||
return
|
||||
}
|
||||
|
||||
s = pub.mapShareToM3U(r, *s)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "audio/x-mpegurl")
|
||||
_, _ = w.Write([]byte(s.ToM3U8()))
|
||||
}
|
||||
|
||||
func checkShareError(ctx context.Context, w http.ResponseWriter, err error, id string) {
|
||||
switch {
|
||||
case errors.Is(err, model.ErrExpired):
|
||||
@@ -63,3 +84,11 @@ func (pub *Router) mapShareInfo(r *http.Request, s model.Share) *model.Share {
|
||||
}
|
||||
return &s
|
||||
}
|
||||
|
||||
func (pub *Router) mapShareToM3U(r *http.Request, s model.Share) *model.Share {
|
||||
for i := range s.Tracks {
|
||||
id := encodeMediafileShare(s, s.Tracks[i].ID)
|
||||
s.Tracks[i].Path = publicURL(r, path.Join(consts.URLPathPublic, "s", id), nil)
|
||||
}
|
||||
return &s
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ func (pub *Router) routes() http.Handler {
|
||||
if conf.Server.EnableDownloads {
|
||||
r.HandleFunc("/d/{id}", pub.handleDownloads)
|
||||
}
|
||||
r.HandleFunc("/{id}/m3u", pub.handleM3U)
|
||||
r.HandleFunc("/{id}", pub.handleShares)
|
||||
r.HandleFunc("/", pub.handleShares)
|
||||
r.Handle("/*", pub.assetsHandler)
|
||||
|
||||
Reference in New Issue
Block a user