Verified Commit 3d6f0b8b authored by Agate's avatar Agate 💬

Fix #183: ensure in place imported files get a proper mimetype

parent 99ff8169
import logging
import urllib.parse
from django.urls import reverse
......@@ -21,6 +22,8 @@ AP_CONTEXT = [
{},
]
logger = logging.getLogger(__name__)
class ActorSerializer(serializers.Serializer):
id = serializers.URLField()
......@@ -620,6 +623,8 @@ class CollectionPageSerializer(serializers.Serializer):
for i in raw_items:
if i.is_valid():
valid_items.append(i)
else:
logger.debug('Invalid item %s: %s', i.data, i.errors)
return valid_items
......
import os
from django.core.files.base import ContentFile
from dynamic_preferences.registries import global_preferences_registry
......@@ -13,6 +15,7 @@ from funkwhale_api.providers.audiofile.tasks import import_track_data_from_path
from django.conf import settings
from . import models
from . import lyrics as lyrics_utils
from . import utils as music_utils
@celery.app.task(name='acoustid.set_on_track_file')
......@@ -129,6 +132,10 @@ def _do_import(import_job, replace=False, use_acoustid=True):
elif not import_job.audio_file and not import_job.source.startswith('file://'):
# not an implace import, and we have a source, so let's download it
track_file.download_file()
elif not import_job.audio_file and import_job.source.startswith('file://'):
# 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)
track_file.save()
import_job.status = 'finished'
import_job.track_file = track_file
......
......@@ -63,8 +63,21 @@ def compute_status(jobs):
return 'finished'
AUDIO_EXTENSIONS_AND_MIMETYPE = [
('ogg', 'audio/ogg'),
('mp3', 'audio/mpeg'),
]
EXTENSION_TO_MIMETYPE = {ext: mt for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}
MIMETYPE_TO_EXTENSION = {mt: ext for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}
def get_ext_from_type(mimetype):
mapping = {
'audio/ogg': 'ogg',
'audio/mpeg': 'mp3',
}
return MIMETYPE_TO_EXTENSION.get(mimetype)
def get_type_from_ext(extension):
if extension.startswith('.'):
# we remove leading dot
extension = extension[1:]
return EXTENSION_TO_MIMETYPE.get(extension)
......@@ -243,3 +243,4 @@ def test__do_import_in_place_mbid(factories, tmpfile):
assert bool(tf.audio_file) is False
assert tf.source == 'file:///test.ogg'
assert tf.mimetype == 'audio/ogg'
Ensure in place imported files get a proper mimetype (#183)
Markdown is supported
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