From a28078753be4b2cf35786d73efc02a8780cfd0ed Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Wed, 16 May 2018 18:55:09 +0200 Subject: [PATCH] See #195: no update track information when fetching track from federation --- api/funkwhale_api/music/models.py | 17 +++++++++++++++++ api/funkwhale_api/music/tasks.py | 14 +------------- api/funkwhale_api/music/utils.py | 2 ++ api/funkwhale_api/music/views.py | 4 ++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py index 7c5fbe9a..1259cc3c 100644 --- a/api/funkwhale_api/music/models.py +++ b/api/funkwhale_api/music/models.py @@ -497,6 +497,23 @@ class TrackFile(models.Model): if self.library_track and self.library_track.audio_file: return self.library_track.audio_file.open() + def set_audio_data(self): + audio_file = self.get_audio_file() + if audio_file: + with audio_file as f: + audio_data = utils.get_audio_file_data(f) + if not audio_data: + return + self.duration = int(audio_data['length']) + self.bitrate = audio_data['bitrate'] + self.size = self.get_file_size() + else: + lt = self.library_track + if lt: + self.duration = lt.get_metadata('length') + self.size = lt.get_metadata('size') + self.bitrate = lt.get_metadata('bitrate') + def save(self, **kwargs): if not self.mimetype and self.audio_file: self.mimetype = utils.guess_mimetype(self.audio_file) diff --git a/api/funkwhale_api/music/tasks.py b/api/funkwhale_api/music/tasks.py index a6d76b96..34345e47 100644 --- a/api/funkwhale_api/music/tasks.py +++ b/api/funkwhale_api/music/tasks.py @@ -134,19 +134,7 @@ def _do_import(import_job, replace=False, use_acoustid=True): # in place import, we set mimetype from extension path, ext = os.path.splitext(import_job.source) track_file.mimetype = music_utils.get_type_from_ext(ext) - audio_file = track_file.get_audio_file() - if audio_file: - with audio_file as f: - audio_data = music_utils.get_audio_file_data(f) - track_file.duration = int(audio_data['length']) - track_file.bitrate = audio_data['bitrate'] - track_file.size = track_file.get_file_size() - else: - lt = track_file.library_track - if lt: - track_file.duration = lt.get_metadata('length') - track_file.size = lt.get_metadata('size') - track_file.bitrate = lt.get_metadata('bitrate') + track_file.set_audio_data() track_file.save() import_job.status = 'finished' import_job.track_file = track_file diff --git a/api/funkwhale_api/music/utils.py b/api/funkwhale_api/music/utils.py index 04ca208d..f11e4507 100644 --- a/api/funkwhale_api/music/utils.py +++ b/api/funkwhale_api/music/utils.py @@ -87,6 +87,8 @@ def get_type_from_ext(extension): def get_audio_file_data(f): data = mutagen.File(f) + if not data: + return d = {} d['bitrate'] = data.info.bitrate d['length'] = data.info.length diff --git a/api/funkwhale_api/music/views.py b/api/funkwhale_api/music/views.py index 226da049..f2ab72c5 100644 --- a/api/funkwhale_api/music/views.py +++ b/api/funkwhale_api/music/views.py @@ -268,6 +268,10 @@ def handle_serve(track_file): qs = LibraryTrack.objects.select_for_update() library_track = qs.get(pk=library_track.pk) library_track.download_audio() + track_file.library_track = library_track + track_file.set_audio_data() + track_file.save(update_fields=['bitrate', 'duration', 'size']) + audio_file = library_track.audio_file file_path = get_file_path(audio_file) mt = library_track.audio_mimetype -- GitLab