Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/streaming"
|
|
)
|
|
|
|
type streamHandlers struct{ svc streamService }
|
|
|
|
// stream serves a track's original file. http.ServeContent does the protocol
|
|
// work: Range (206, 416), HEAD, Content-Length, conditional requests, and it
|
|
// stops copying when the client goes away.
|
|
func (h *streamHandlers) stream(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
st, err := h.svc.Open(c.Request.Context(), id)
|
|
switch {
|
|
case errors.Is(err, streaming.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, "track_not_found", "no playable file for this track")
|
|
return
|
|
case errors.Is(err, streaming.ErrFileMissing):
|
|
writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available")
|
|
return
|
|
case errors.Is(err, streaming.ErrUnsafePath):
|
|
// A stored path that leaves the library is a data problem, not the
|
|
// client's: log it, but say no more than "not available".
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available")
|
|
return
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
defer st.Close()
|
|
|
|
hdr := c.Writer.Header()
|
|
hdr.Set("Content-Type", st.ContentType)
|
|
hdr.Set("X-Content-Type-Options", "nosniff")
|
|
// The URL may carry the access token: never leak it through Referer.
|
|
hdr.Set("Referrer-Policy", "no-referrer")
|
|
hdr.Set("Cache-Control", "private, no-cache")
|
|
|
|
http.ServeContent(c.Writer, c.Request, st.Name, st.ModTime, st.File)
|
|
}
|