mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 04:09:56 +02:00
Fix #258: Implemented getCovertArt in Subsonic API to serve album covers
This commit is contained in:
parent
218a92547e
commit
8d50743b3b
6 changed files with 77 additions and 4 deletions
|
@ -242,8 +242,8 @@ def get_file_path(audio_file):
|
|||
'You need to specify MUSIC_DIRECTORY_SERVE_PATH and '
|
||||
'MUSIC_DIRECTORY_PATH to serve in-place imported files'
|
||||
)
|
||||
path = audio_file.replace(prefix, serve_path, 1).encode('utf-8')
|
||||
return path
|
||||
path = audio_file.replace(prefix, serve_path, 1)
|
||||
return path.encode('utf-8')
|
||||
|
||||
|
||||
def handle_serve(track_file):
|
||||
|
|
|
@ -57,8 +57,10 @@ class GetArtistSerializer(serializers.Serializer):
|
|||
'name': album.title,
|
||||
'artist': artist.name,
|
||||
'created': album.creation_date,
|
||||
'songCount': len(album.tracks.all())
|
||||
'songCount': len(album.tracks.all()),
|
||||
}
|
||||
if album.cover:
|
||||
album_data['coverArt'] = 'al-{}'.format(album.id)
|
||||
if album.release_date:
|
||||
album_data['year'] = album.release_date.year
|
||||
payload['album'].append(album_data)
|
||||
|
@ -81,6 +83,8 @@ def get_track_data(album, track, tf):
|
|||
'artistId': album.artist.pk,
|
||||
'type': 'music',
|
||||
}
|
||||
if track.album.cover:
|
||||
data['coverArt'] = 'al-{}'.format(track.album.id)
|
||||
if tf.bitrate:
|
||||
data['bitrate'] = int(tf.bitrate/1000)
|
||||
if tf.size:
|
||||
|
@ -98,6 +102,9 @@ def get_album2_data(album):
|
|||
'artist': album.artist.name,
|
||||
'created': album.creation_date,
|
||||
}
|
||||
if album.cover:
|
||||
payload['coverArt'] = 'al-{}'.format(album.id)
|
||||
|
||||
try:
|
||||
payload['songCount'] = album._tracks_count
|
||||
except AttributeError:
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
from rest_framework import exceptions
|
||||
|
@ -459,7 +460,7 @@ class SubsonicViewSet(viewsets.GenericViewSet):
|
|||
'code': 10,
|
||||
'message': 'Playlist ID or name must be specified.'
|
||||
}
|
||||
}, data)
|
||||
})
|
||||
|
||||
playlist = request.user.playlists.create(
|
||||
name=name
|
||||
|
@ -503,3 +504,51 @@ class SubsonicViewSet(viewsets.GenericViewSet):
|
|||
}
|
||||
}
|
||||
return response.Response(data)
|
||||
|
||||
@list_route(
|
||||
methods=['get', 'post'],
|
||||
url_name='get_cover_art',
|
||||
url_path='getCoverArt')
|
||||
def get_cover_art(self, request, *args, **kwargs):
|
||||
data = request.GET or request.POST
|
||||
id = data.get('id', '')
|
||||
if not id:
|
||||
return response.Response({
|
||||
'error': {
|
||||
'code': 10,
|
||||
'message': 'cover art ID must be specified.'
|
||||
}
|
||||
})
|
||||
|
||||
if id.startswith('al-'):
|
||||
try:
|
||||
album_id = int(id.replace('al-', ''))
|
||||
album = music_models.Album.objects.exclude(
|
||||
cover__isnull=True
|
||||
).exclude(cover='').get(pk=album_id)
|
||||
except (TypeError, ValueError, music_models.Album.DoesNotExist):
|
||||
return response.Response({
|
||||
'error': {
|
||||
'code': 70,
|
||||
'message': 'cover art not found.'
|
||||
}
|
||||
})
|
||||
cover = album.cover
|
||||
else:
|
||||
return response.Response({
|
||||
'error': {
|
||||
'code': 70,
|
||||
'message': 'cover art not found.'
|
||||
}
|
||||
})
|
||||
|
||||
mapping = {
|
||||
'nginx': 'X-Accel-Redirect',
|
||||
'apache2': 'X-Sendfile',
|
||||
}
|
||||
path = music_views.get_file_path(cover)
|
||||
file_header = mapping[settings.REVERSE_PROXY_TYPE]
|
||||
# let the proxy set the content-type
|
||||
r = response.Response({}, content_type='')
|
||||
r[file_header] = path
|
||||
return r
|
Loading…
Add table
Add a link
Reference in a new issue