Add download button in the SharePlayer

This commit is contained in:
Deluan 2023-03-10 23:14:51 -05:00 committed by Deluan Quintão
parent a7d3e6e1f1
commit b520d8827a
6 changed files with 33 additions and 9 deletions

View File

@ -91,8 +91,10 @@ func (a *archiver) albumFilename(mf model.MediaFile, format string, isMultDisc b
func (a *archiver) ZipShare(ctx context.Context, id string, out io.Writer) error { func (a *archiver) ZipShare(ctx context.Context, id string, out io.Writer) error {
s, err := a.shares.Load(ctx, id) s, err := a.shares.Load(ctx, id)
if !s.Downloadable {
return model.ErrNotAuthorized
}
if err != nil { if err != nil {
log.Error(ctx, "Error loading mediafiles from share", "id", id, err)
return err return err
} }
log.Debug(ctx, "Zipping share", "name", s.ID, "format", s.Format, "bitrate", s.MaxBitRate, "numTracks", len(s.Tracks)) log.Debug(ctx, "Zipping share", "name", s.ID, "format", s.Format, "bitrate", s.MaxBitRate, "numTracks", len(s.Tracks))

View File

@ -45,6 +45,9 @@ func checkShareError(ctx context.Context, w http.ResponseWriter, err error, id s
case errors.Is(err, model.ErrNotFound): case errors.Is(err, model.ErrNotFound):
log.Error(ctx, "Share not found", "id", id, err) log.Error(ctx, "Share not found", "id", id, err)
http.Error(w, "Share not found", http.StatusNotFound) http.Error(w, "Share not found", http.StatusNotFound)
case errors.Is(err, model.ErrNotAuthorized):
log.Error(ctx, "Share is not downloadable", "id", id, err)
http.Error(w, "This share is not downloadable", http.StatusForbidden)
case err != nil: case err != nil:
log.Error(ctx, "Error retrieving share", "id", id, err) log.Error(ctx, "Error retrieving share", "id", id, err)
http.Error(w, "Error retrieving share", http.StatusInternalServerError) http.Error(w, "Error retrieving share", http.StatusInternalServerError)

View File

@ -122,7 +122,9 @@ func getIndexTemplate(r *http.Request, fs fs.FS) (*template.Template, error) {
} }
type shareData struct { type shareData struct {
ID string `json:"id"`
Description string `json:"description"` Description string `json:"description"`
Downloadable bool `json:"downloadable"`
Tracks []shareTrack `json:"tracks"` Tracks []shareTrack `json:"tracks"`
} }
@ -141,7 +143,9 @@ func addShareData(r *http.Request, data map[string]interface{}, shareInfo *model
return return
} }
sd := shareData{ sd := shareData{
ID: shareInfo.ID,
Description: shareInfo.Description, Description: shareInfo.Description,
Downloadable: shareInfo.Downloadable,
} }
sd.Tracks = slice.Map(shareInfo.Tracks, func(mf model.MediaFile) shareTrack { sd.Tracks = slice.Map(shareInfo.Tracks, func(mf model.MediaFile) shareTrack {
return shareTrack{ return shareTrack{

View File

@ -37,7 +37,7 @@ import config, { shareInfo } from './config'
import { setDispatch, startEventStream, stopEventStream } from './eventStream' import { setDispatch, startEventStream, stopEventStream } from './eventStream'
import { keyMap } from './hotkeys' import { keyMap } from './hotkeys'
import useChangeThemeColor from './useChangeThemeColor' import useChangeThemeColor from './useChangeThemeColor'
import SharePlayer from './SharePlayer' import SharePlayer from './share/SharePlayer'
const history = createHashHistory() const history = createHashHistory()

View File

@ -1,6 +1,6 @@
import ReactJkMusicPlayer from 'navidrome-music-player' import ReactJkMusicPlayer from 'navidrome-music-player'
import { shareInfo } from './config' import { shareInfo } from '../config'
import { shareCoverUrl, shareStreamUrl } from './utils' import { shareCoverUrl, shareDownloadUrl, shareStreamUrl } from '../utils'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
@ -34,12 +34,17 @@ const SharePlayer = () => {
duration: s.duration, duration: s.duration,
} }
}) })
const onBeforeAudioDownload = () => {
return Promise.resolve({
src: shareDownloadUrl(shareInfo?.id),
})
}
const options = { const options = {
audioLists: list, audioLists: list,
mode: 'full', mode: 'full',
toggleMode: false, toggleMode: false,
mobileMediaQuery: '', mobileMediaQuery: '',
showDownload: false, showDownload: shareInfo?.downloadable,
showReload: false, showReload: false,
showMediaSession: true, showMediaSession: true,
theme: 'auto', theme: 'auto',
@ -49,7 +54,13 @@ const SharePlayer = () => {
spaceBar: true, spaceBar: true,
volumeFade: { fadeIn: 200, fadeOut: 200 }, volumeFade: { fadeIn: 200, fadeOut: 200 },
} }
return <ReactJkMusicPlayer {...options} className={classes.player} /> return (
<ReactJkMusicPlayer
{...options}
className={classes.player}
onBeforeAudioDownload={onBeforeAudioDownload}
/>
)
} }
export default SharePlayer export default SharePlayer

View File

@ -19,6 +19,10 @@ export const shareStreamUrl = (id) => {
return baseUrl(config.publicBaseUrl + '/s/' + id) return baseUrl(config.publicBaseUrl + '/s/' + id)
} }
export const shareDownloadUrl = (id) => {
return baseUrl(config.publicBaseUrl + '/d/' + id)
}
export const shareCoverUrl = (id) => { export const shareCoverUrl = (id) => {
return baseUrl(config.publicBaseUrl + '/img/' + id + '?size=300') return baseUrl(config.publicBaseUrl + '/img/' + id + '?size=300')
} }