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 -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)