Verified Commit a2807875 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

See #195: no update track information when fetching track from federation

parent 80783f9a
......@@ -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)
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment