Skip to content

Commit

Permalink
Add option to download playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Aug 21, 2020
1 parent 073e40d commit 8fa5544
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
41 changes: 33 additions & 8 deletions core/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type Archiver interface {
ZipAlbum(ctx context.Context, id string, w io.Writer) error
ZipArtist(ctx context.Context, id string, w io.Writer) error
ZipPlaylist(ctx context.Context, id string, w io.Writer) error
}

func NewArchiver(ds model.DataStore) Archiver {
Expand All @@ -26,13 +27,15 @@ type archiver struct {
ds model.DataStore
}

type createHeader func(idx int, mf model.MediaFile) *zip.FileHeader

func (a *archiver) ZipAlbum(ctx context.Context, id string, out io.Writer) error {
mfs, err := a.ds.MediaFile(ctx).FindByAlbum(id)
if err != nil {
log.Error(ctx, "Error loading mediafiles from album", "id", id, err)
return err
}
return a.zipTracks(ctx, id, out, mfs)
return a.zipTracks(ctx, id, out, mfs, a.createHeader)
}

func (a *archiver) ZipArtist(ctx context.Context, id string, out io.Writer) error {
Expand All @@ -44,13 +47,22 @@ func (a *archiver) ZipArtist(ctx context.Context, id string, out io.Writer) erro
log.Error(ctx, "Error loading mediafiles from artist", "id", id, err)
return err
}
return a.zipTracks(ctx, id, out, mfs)
return a.zipTracks(ctx, id, out, mfs, a.createHeader)
}

func (a *archiver) ZipPlaylist(ctx context.Context, id string, out io.Writer) error {
pls, err := a.ds.Playlist(ctx).Get(id)
if err != nil {
log.Error(ctx, "Error loading mediafiles from playlist", "id", id, err)
return err
}
return a.zipTracks(ctx, id, out, pls.Tracks, a.createPlaylistHeader)
}

func (a *archiver) zipTracks(ctx context.Context, id string, out io.Writer, mfs model.MediaFiles) error {
func (a *archiver) zipTracks(ctx context.Context, id string, out io.Writer, mfs model.MediaFiles, ch createHeader) error {
z := zip.NewWriter(out)
for _, mf := range mfs {
_ = a.addFileToZip(ctx, z, mf)
for idx, mf := range mfs {
_ = a.addFileToZip(ctx, z, mf, ch(idx, mf))
}
err := z.Close()
if err != nil {
Expand All @@ -59,13 +71,26 @@ func (a *archiver) zipTracks(ctx context.Context, id string, out io.Writer, mfs
return err
}

func (a *archiver) addFileToZip(ctx context.Context, z *zip.Writer, mf model.MediaFile) error {
func (a *archiver) createHeader(idx int, mf model.MediaFile) *zip.FileHeader {
_, file := filepath.Split(mf.Path)
w, err := z.CreateHeader(&zip.FileHeader{
return &zip.FileHeader{
Name: fmt.Sprintf("%s/%s", mf.Album, file),
Modified: mf.UpdatedAt,
Method: zip.Store,
})
}
}

func (a *archiver) createPlaylistHeader(idx int, mf model.MediaFile) *zip.FileHeader {
_, file := filepath.Split(mf.Path)
return &zip.FileHeader{
Name: fmt.Sprintf("%d - %s-%s", idx, mf.AlbumArtist, file),
Modified: mf.UpdatedAt,
Method: zip.Store,
}
}

func (a *archiver) addFileToZip(ctx context.Context, z *zip.Writer, mf model.MediaFile, zh *zip.FileHeader) error {
w, err := z.CreateHeader(zh)
if err != nil {
log.Error(ctx, "Error creating zip entry", "file", mf.Path, err)
return err
Expand Down
4 changes: 4 additions & 0 deletions server/subsonic/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ func getEntityByID(ctx context.Context, ds model.DataStore, id string) (interfac
if err == nil {
return al, nil
}
pls, err := ds.Playlist(ctx).Get(id)
if err == nil {
return pls, nil
}
mf, err := ds.MediaFile(ctx).Get(id)
if err == nil {
return mf, nil
Expand Down
3 changes: 3 additions & 0 deletions server/subsonic/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*re
case *model.Artist:
setHeaders(v.Name)
err = c.archiver.ZipArtist(ctx, id, w)
case *model.Playlist:
setHeaders(v.Name)
err = c.archiver.ZipPlaylist(ctx, id, w)
default:
err = model.ErrNotFound
}
Expand Down
4 changes: 2 additions & 2 deletions ui/src/album/AlbumActions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'
import { useDispatch } from 'react-redux'
import {
Button,
sanitizeListRestProps,
Expand All @@ -7,8 +9,6 @@ import {
import PlayArrowIcon from '@material-ui/icons/PlayArrow'
import ShuffleIcon from '@material-ui/icons/Shuffle'
import CloudDownloadOutlinedIcon from '@material-ui/icons/CloudDownloadOutlined'
import React from 'react'
import { useDispatch } from 'react-redux'
import { playTracks, shuffleTracks } from '../audioplayer'
import subsonic from '../subsonic'

Expand Down
15 changes: 13 additions & 2 deletions ui/src/playlist/PlaylistActions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'
import { useDispatch } from 'react-redux'
import {
Button,
sanitizeListRestProps,
Expand All @@ -6,16 +8,17 @@ import {
} from 'react-admin'
import PlayArrowIcon from '@material-ui/icons/PlayArrow'
import ShuffleIcon from '@material-ui/icons/Shuffle'
import React from 'react'
import { useDispatch } from 'react-redux'
import CloudDownloadOutlinedIcon from '@material-ui/icons/CloudDownloadOutlined'
import { playTracks, shuffleTracks } from '../audioplayer'
import subsonic from '../subsonic'

const PlaylistActions = ({
className,
ids,
data,
exporter,
permanentFilter,
playlistId,
...rest
}) => {
const dispatch = useDispatch()
Expand All @@ -39,6 +42,14 @@ const PlaylistActions = ({
>
<ShuffleIcon />
</Button>
<Button
onClick={() => {
subsonic.download(playlistId)
}}
label={translate('resources.album.actions.download')}
>
<CloudDownloadOutlinedIcon />
</Button>
</TopToolbar>
)
}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/playlist/PlaylistShow.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const PlaylistShow = (props) => {
playlistId={props.id}
readOnly={isReadOnly(record && record.owner)}
title={<Title subTitle={record && record.name} />}
actions={<PlaylistActions />}
actions={<PlaylistActions playlistId={props.id} />}
filter={{ playlist_id: props.id }}
resource={'playlistTrack'}
exporter={false}
Expand Down

0 comments on commit 8fa5544

Please sign in to comment.