package httpapi import ( "context" "errors" "net/http" "github.com/gin-gonic/gin" "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/internal/waveform" ) type waveformHandlers struct{ svc waveformService } // peaks gives the shape of a track: how loud it is along its length, in a few levels from 0 to 255, for the seek bar. // The first time it is asked for, the server starts working it out and says so (202); the page asks again. func (h *waveformHandlers) peaks(c *gin.Context) { id, ok := pathID(c) if !ok { return } peaks, err := h.svc.Peaks(c.Request.Context(), id) switch { case err == nil: levels := make([]int, len(peaks)) for i, v := range peaks { levels[i] = int(v) } c.JSON(http.StatusOK, gin.H{"count": len(levels), "peaks": levels}) case errors.Is(err, waveform.ErrNotReady): c.Header("Retry-After", "3") c.JSON(http.StatusAccepted, gin.H{"status": "working"}) case errors.Is(err, streaming.ErrNotFound): writeError(c, http.StatusNotFound, "track_not_found", "no playable file for this track") case errors.Is(err, streaming.ErrFileMissing), errors.Is(err, streaming.ErrUnsafePath): writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available") case errors.Is(err, waveform.ErrUnsupported): writeError(c, http.StatusNotFound, "waveform_unavailable", "the waveform of this file cannot be worked out") case errors.Is(err, context.Canceled): c.AbortWithStatus(statusClientClosed) // the page was left; nobody is waiting default: _ = c.Error(err) writeError(c, http.StatusInternalServerError, "internal", "internal error") } }