Add share download endpoint

This commit is contained in:
Deluan
2023-03-10 20:46:13 -05:00
committed by Deluan Quintão
parent 50d9838652
commit a22eef39f7
7 changed files with 64 additions and 27 deletions
+16
View File
@@ -0,0 +1,16 @@
package public
import (
"net/http"
)
func (p *Router) handleDownloads(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get(":id")
if id == "" {
http.Error(w, "invalid id", http.StatusBadRequest)
return
}
err := p.archiver.ZipShare(r.Context(), id, w)
checkShareError(r.Context(), w, err, id)
}
+16 -11
View File
@@ -1,6 +1,7 @@
package public
import (
"context"
"errors"
"net/http"
@@ -27,18 +28,8 @@ func (p *Router) handleShares(w http.ResponseWriter, r *http.Request) {
// If it is not, consider it a share ID
s, err := p.share.Load(r.Context(), id)
switch {
case errors.Is(err, model.ErrNotAvailable):
log.Error(r, "Share expired", "id", id, err)
http.Error(w, "Share not available anymore", http.StatusGone)
case errors.Is(err, model.ErrNotFound):
log.Error(r, "Share not found", "id", id, err)
http.Error(w, "Share not found", http.StatusNotFound)
case err != nil:
log.Error(r, "Error retrieving share", "id", id, err)
http.Error(w, "Error retrieving share", http.StatusInternalServerError)
}
if err != nil {
checkShareError(r.Context(), w, err, id)
return
}
@@ -46,6 +37,20 @@ func (p *Router) handleShares(w http.ResponseWriter, r *http.Request) {
server.IndexWithShare(p.ds, ui.BuildAssets(), s)(w, r)
}
func checkShareError(ctx context.Context, w http.ResponseWriter, err error, id string) {
switch {
case errors.Is(err, model.ErrExpired):
log.Error(ctx, "Share expired", "id", id, err)
http.Error(w, "Share not available anymore", http.StatusGone)
case errors.Is(err, model.ErrNotFound):
log.Error(ctx, "Share not found", "id", id, err)
http.Error(w, "Share not found", http.StatusNotFound)
case err != nil:
log.Error(ctx, "Error retrieving share", "id", id, err)
http.Error(w, "Error retrieving share", http.StatusInternalServerError)
}
}
func (p *Router) mapShareInfo(r *http.Request, s model.Share) *model.Share {
s.URL = ShareURL(r, s.ID)
s.ImageURL = ImageURL(r, s.CoverArtID(), consts.UICoverArtSize)
@@ -20,13 +20,14 @@ type Router struct {
http.Handler
artwork artwork.Artwork
streamer core.MediaStreamer
archiver core.Archiver
share core.Share
assetsHandler http.Handler
ds model.DataStore
}
func New(ds model.DataStore, artwork artwork.Artwork, streamer core.MediaStreamer, share core.Share) *Router {
p := &Router{ds: ds, artwork: artwork, streamer: streamer, share: share}
func New(ds model.DataStore, artwork artwork.Artwork, streamer core.MediaStreamer, share core.Share, archiver core.Archiver) *Router {
p := &Router{ds: ds, artwork: artwork, streamer: streamer, share: share, archiver: archiver}
shareRoot := path.Join(conf.Server.BasePath, consts.URLPathPublic)
p.assetsHandler = http.StripPrefix(shareRoot, http.FileServer(http.FS(ui.BuildAssets())))
p.Handler = p.routes()
@@ -51,6 +52,7 @@ func (p *Router) routes() http.Handler {
})
if conf.Server.EnableSharing {
r.HandleFunc("/s/{id}", p.handleStream)
r.HandleFunc("/d/{id}", p.handleDownloads)
r.HandleFunc("/{id}", p.handleShares)
r.HandleFunc("/", p.handleShares)
r.Handle("/*", p.assetsHandler)