navidrome/server/subsonic/sharing.go

76 lines
1.9 KiB
Go

package subsonic
import (
"net/http"
"strings"
"time"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/public"
"github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils"
)
func (api *Router) GetShares(r *http.Request) (*responses.Subsonic, error) {
repo := api.share.NewRepository(r.Context())
entity, err := repo.ReadAll()
if err != nil {
return nil, err
}
shares := entity.(model.Shares)
response := newResponse()
response.Shares = &responses.Shares{}
for _, share := range shares {
response.Shares.Share = append(response.Shares.Share, api.buildShare(r, share))
}
return response, nil
}
func (api *Router) buildShare(r *http.Request, share model.Share) responses.Share {
return responses.Share{
Entry: childrenFromMediaFiles(r.Context(), share.Tracks),
ID: share.ID,
Url: public.ShareURL(r, share.ID),
Description: share.Description,
Username: share.Username,
Created: share.CreatedAt,
Expires: &share.ExpiresAt,
LastVisited: share.LastVisitedAt,
VisitCount: share.VisitCount,
}
}
func (api *Router) CreateShare(r *http.Request) (*responses.Subsonic, error) {
ids := utils.ParamStrings(r, "id")
if len(ids) == 0 {
return nil, newError(responses.ErrorMissingParameter, "Required id parameter is missing")
}
description := utils.ParamString(r, "description")
expires := utils.ParamTime(r, "expires", time.Time{})
repo := api.share.NewRepository(r.Context())
share := &model.Share{
Description: description,
ExpiresAt: expires,
ResourceIDs: strings.Join(ids, ","),
}
id, err := repo.(rest.Persistable).Save(share)
if err != nil {
return nil, err
}
entity, err := repo.Read(id)
if err != nil {
return nil, err
}
share = entity.(*model.Share)
response := newResponse()
response.Shares = &responses.Shares{Share: []responses.Share{api.buildShare(r, *share)}}
return response, nil
}