mirror of
https://github.com/navidrome/navidrome.git
synced 2026-04-28 03:19:38 +00:00
Some checks are pending
Pipeline: Test, Lint, Build / Get version info (push) Waiting to run
Pipeline: Test, Lint, Build / Lint Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test JS code (push) Waiting to run
Pipeline: Test, Lint, Build / Lint i18n files (push) Waiting to run
Pipeline: Test, Lint, Build / Check Docker configuration (push) Waiting to run
Pipeline: Test, Lint, Build / Build (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-1 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-2 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-3 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-4 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-5 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-6 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-7 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-8 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-9 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-10 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to GHCR (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build Windows installers (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Package/Release (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Blocked by required conditions
* refactor: rename core/transcode directory to core/stream * refactor: update all imports from core/transcode to core/stream * refactor: rename exported symbols to fit core/stream package name * refactor: simplify MediaStreamer interface to single NewStream method Remove the two-method interface (NewStream + DoStream) in favor of a single NewStream(ctx, mf, req) method. Callers are now responsible for fetching the MediaFile before calling NewStream. This removes the implicit DB lookup from the streamer, making it a pure streaming concern. * refactor: update all callers from DoStream to NewStream * chore: update wire_gen.go and stale comment for core/stream rename * refactor: update wire command to handle GO_BUILD_TAGS correctly Signed-off-by: Deluan <deluan@navidrome.org> * fix: distinguish not-found from internal errors in public stream handler * refactor: remove unused ID field from stream.Request * refactor: simplify ResolveRequestFromToken to receive *model.MediaFile Move MediaFile fetching responsibility to callers, making the method focused on token validation and request resolution. Remove ErrMediaNotFound (no longer produced). Update GetTranscodeStream handler to fetch the media file before calling ResolveRequestFromToken. * refactor: extend tokenTTL from 12 to 48 hours Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package public
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/navidrome/navidrome/core/auth"
|
|
"github.com/navidrome/navidrome/core/stream"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils/req"
|
|
)
|
|
|
|
func (pub *Router) handleStream(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
p := req.Params(r)
|
|
tokenId, _ := p.String(":id")
|
|
info, err := decodeStreamInfo(tokenId)
|
|
if err != nil {
|
|
log.Error(ctx, "Error parsing shared stream info", err)
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
mf, err := pub.ds.MediaFile(ctx).Get(info.id)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
} else {
|
|
log.Error(ctx, "Error retrieving media file for shared stream", "id", info.id, err)
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
}
|
|
return
|
|
}
|
|
|
|
stream, err := pub.streamer.NewStream(ctx, mf, stream.Request{
|
|
Format: info.format, BitRate: info.bitrate,
|
|
})
|
|
if err != nil {
|
|
log.Error(ctx, "Error starting shared stream", err)
|
|
http.Error(w, "invalid request", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Make sure the stream will be closed at the end, to avoid leakage
|
|
defer func() {
|
|
if err := stream.Close(); err != nil && log.IsGreaterOrEqualTo(log.LevelDebug) {
|
|
log.Error("Error closing shared stream", "id", info.id, "file", stream.Name(), err)
|
|
}
|
|
}()
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.Header().Set("X-Content-Duration", strconv.FormatFloat(float64(stream.Duration()), 'G', -1, 32))
|
|
|
|
if stream.Seekable() {
|
|
http.ServeContent(w, r, stream.Name(), stream.ModTime(), stream)
|
|
} else {
|
|
// If the stream doesn't provide a size (i.e. is not seekable), we can't support ranges/content-length
|
|
w.Header().Set("Accept-Ranges", "none")
|
|
w.Header().Set("Content-Type", stream.ContentType())
|
|
|
|
estimateContentLength := p.BoolOr("estimateContentLength", false)
|
|
|
|
// if Client requests the estimated content-length, send it
|
|
if estimateContentLength {
|
|
length := strconv.Itoa(stream.EstimatedContentLength())
|
|
log.Trace(ctx, "Estimated content-length", "contentLength", length)
|
|
w.Header().Set("Content-Length", length)
|
|
}
|
|
|
|
if r.Method == http.MethodHead {
|
|
go func() { _, _ = io.Copy(io.Discard, stream) }()
|
|
} else {
|
|
c, err := io.Copy(w, stream)
|
|
if log.IsGreaterOrEqualTo(log.LevelDebug) {
|
|
if err != nil {
|
|
log.Error(ctx, "Error sending shared transcoded file", "id", info.id, err)
|
|
} else {
|
|
log.Trace(ctx, "Success sending shared transcode file", "id", info.id, "size", c)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
type shareTrackInfo struct {
|
|
id string
|
|
format string
|
|
bitrate int
|
|
}
|
|
|
|
func decodeStreamInfo(tokenString string) (shareTrackInfo, error) {
|
|
token, err := auth.TokenAuth.Decode(tokenString)
|
|
if err != nil {
|
|
return shareTrackInfo{}, err
|
|
}
|
|
if token == nil {
|
|
return shareTrackInfo{}, errors.New("unauthorized")
|
|
}
|
|
c := auth.ClaimsFromToken(token)
|
|
if c.ID == "" {
|
|
return shareTrackInfo{}, errors.New("required claim \"id\" not found")
|
|
}
|
|
return shareTrackInfo{
|
|
id: c.ID,
|
|
format: c.Format,
|
|
bitrate: c.BitRate,
|
|
}, nil
|
|
}
|