diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-17 15:25:52 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-17 15:25:52 +0300 |
| commit | 914bd7cd6aa14e839332a98d91c30b19865b0cf2 (patch) | |
| tree | 02b11537237a204048589e14ed24938b805e42f7 /internal/api/handlers_file.go | |
| parent | 3b24f0e1be832584d6550e8cc3e5d24329f66f90 (diff) | |
Restructure repo: move Go server into player-server/
Diffstat (limited to 'internal/api/handlers_file.go')
| -rw-r--r-- | internal/api/handlers_file.go | 109 |
1 files changed, 0 insertions, 109 deletions
diff --git a/internal/api/handlers_file.go b/internal/api/handlers_file.go deleted file mode 100644 index ecf55e0..0000000 --- a/internal/api/handlers_file.go +++ /dev/null @@ -1,109 +0,0 @@ -package api - -import ( - "context" - "errors" - "net/http" - - "codeberg.org/snonux/player/internal/service" -) - -// ------------------------------------------------------------------ -// File serving handlers -// ------------------------------------------------------------------ - -func (s *Server) fileHandler(fn func(context.Context, int64, int64) (*service.FileResult, error)) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - id := pathID(r, "id") - if id == 0 { - http.Error(w, "invalid media id", http.StatusBadRequest) - return - } - res, err := fn(r.Context(), id, userIDFromContext(r)) - if err != nil { - if errors.Is(err, service.ErrNotFound) { - http.Error(w, "not found", http.StatusNotFound) - return - } - if errors.Is(err, service.ErrForbidden) { - http.Error(w, "forbidden", http.StatusForbidden) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - if res == nil { - http.Error(w, "not found", http.StatusNotFound) - return - } - s.serveFileResult(w, r, res, false) - } -} - -func (s *Server) handleStream(w http.ResponseWriter, r *http.Request) { - if !requireService(w, s.browseSvc) { - return - } - s.fileHandler(s.browseSvc.StreamMedia)(w, r) -} - -func (s *Server) handleDownload(w http.ResponseWriter, r *http.Request) { - if !requireService(w, s.browseSvc) { - return - } - id := pathID(r, "id") - if id == 0 { - badRequest(w, "invalid media id") - return - } - res, err := s.browseSvc.DownloadMedia(r.Context(), id, userIDFromContext(r)) - if err != nil { - if errors.Is(err, service.ErrNotFound) { - notFound(w) - return - } - if errors.Is(err, service.ErrForbidden) { - forbidden(w, "forbidden") - return - } - handleError(w, err) - return - } - if res == nil { - notFound(w) - return - } - s.serveFileResult(w, r, res, true) -} - -func (s *Server) handleThumbnail(w http.ResponseWriter, r *http.Request) { - if !requireService(w, s.browseSvc) { - return - } - w.Header().Set("Cache-Control", "no-cache") - s.fileHandler(s.browseSvc.GetThumbnail)(w, r) -} - -func (s *Server) handleRegenThumbnail(w http.ResponseWriter, r *http.Request) { - if !requireService(w, s.writeSvc) { - return - } - id := pathID(r, "id") - if id == 0 { - badRequest(w, "invalid media id") - return - } - if err := s.writeSvc.RegenerateThumbnail(r.Context(), id, userIDFromContext(r)); err != nil { - if errors.Is(err, service.ErrNotFound) { - notFound(w) - return - } - if errors.Is(err, service.ErrForbidden) { - forbidden(w, "forbidden") - return - } - handleError(w, err) - return - } - writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) -} |
