mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-04 13:59:15 +02:00
See #195: fix_track_files command can now recompute size, bitrate and duration
This commit is contained in:
parent
af82111ade
commit
4b21128e46
2 changed files with 105 additions and 0 deletions
|
@ -2,6 +2,7 @@ import cacheops
|
|||
import os
|
||||
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
|
@ -24,6 +25,8 @@ class Command(BaseCommand):
|
|||
if options['dry_run']:
|
||||
self.stdout.write('Dry-run on, will not commit anything')
|
||||
self.fix_mimetypes(**options)
|
||||
self.fix_file_data(**options)
|
||||
self.fix_file_size(**options)
|
||||
cacheops.invalidate_model(models.TrackFile)
|
||||
|
||||
@transaction.atomic
|
||||
|
@ -43,3 +46,60 @@ class Command(BaseCommand):
|
|||
if not dry_run:
|
||||
self.stdout.write('[mimetypes] commiting...')
|
||||
qs.update(mimetype=mimetype)
|
||||
|
||||
def fix_file_data(self, dry_run, **kwargs):
|
||||
self.stdout.write('Fixing missing bitrate or length...')
|
||||
matching = models.TrackFile.objects.filter(
|
||||
Q(bitrate__isnull=True) | Q(duration__isnull=True))
|
||||
total = matching.count()
|
||||
self.stdout.write(
|
||||
'[bitrate/length] {} entries found with missing values'.format(
|
||||
total))
|
||||
if dry_run:
|
||||
return
|
||||
for i, tf in enumerate(matching.only('audio_file')):
|
||||
self.stdout.write(
|
||||
'[bitrate/length] {}/{} fixing file #{}'.format(
|
||||
i+1, total, tf.pk
|
||||
))
|
||||
|
||||
try:
|
||||
audio_file = tf.get_audio_file()
|
||||
if audio_file:
|
||||
with audio_file as f:
|
||||
data = utils.get_audio_file_data(audio_file)
|
||||
tf.bitrate = data['bitrate']
|
||||
tf.duration = data['length']
|
||||
tf.save(update_fields=['duration', 'bitrate'])
|
||||
else:
|
||||
self.stderr.write('[bitrate/length] no file found')
|
||||
except Exception as e:
|
||||
self.stderr.write(
|
||||
'[bitrate/length] error with file #{}: {}'.format(
|
||||
tf.pk, str(e)
|
||||
)
|
||||
)
|
||||
|
||||
def fix_file_size(self, dry_run, **kwargs):
|
||||
self.stdout.write('Fixing missing size...')
|
||||
matching = models.TrackFile.objects.filter(size__isnull=True)
|
||||
total = matching.count()
|
||||
self.stdout.write(
|
||||
'[size] {} entries found with missing values'.format(total))
|
||||
if dry_run:
|
||||
return
|
||||
for i, tf in enumerate(matching.only('size')):
|
||||
self.stdout.write(
|
||||
'[size] {}/{} fixing file #{}'.format(
|
||||
i+1, total, tf.pk
|
||||
))
|
||||
|
||||
try:
|
||||
tf.size = tf.get_file_size()
|
||||
tf.save(update_fields=['size'])
|
||||
except Exception as e:
|
||||
self.stderr.write(
|
||||
'[size] error with file #{}: {}'.format(
|
||||
tf.pk, str(e)
|
||||
)
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue