From 0e278bc8c42551f27d858c2f7e371c8e28e8fb9b Mon Sep 17 00:00:00 2001 From: Laurent DEFERT Date: Wed, 28 Dec 2022 10:34:34 +0100 Subject: [PATCH] fix relative media_file path FileField should store a relative path, to make their "url" attribute work --- .../migrations/0013_fix_elative_media_file.py | 25 +++++++++++++++++++ tubesync/sync/models.py | 8 ++++-- tubesync/sync/tasks.py | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tubesync/sync/migrations/0013_fix_elative_media_file.py diff --git a/tubesync/sync/migrations/0013_fix_elative_media_file.py b/tubesync/sync/migrations/0013_fix_elative_media_file.py new file mode 100644 index 0000000..c9eee22 --- /dev/null +++ b/tubesync/sync/migrations/0013_fix_elative_media_file.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.12 on 2022-04-06 06:19 + +from django.conf import settings +from django.db import migrations, models + + +def fix_media_file(apps, schema_editor): + Media = apps.get_model('sync', 'Media') + for media in Media.objects.filter(downloaded=True): + download_dir = str(settings.DOWNLOAD_ROOT) + + if media.media_file.name.startswith(download_dir): + media.media_file.name = media.media_file.name[len(download_dir) + 1:] + media.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('sync', '0012_alter_media_downloaded_format'), + ] + + operations = [ + migrations.RunPython(fix_media_file) + ] diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 15437f6..4e42086 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -392,10 +392,14 @@ class Source(models.Model): @property def directory_path(self): download_dir = Path(media_file_storage.location) + return download_dir / self.type_directory_path + + @property + def type_directory_path(self): if self.source_resolution == self.SOURCE_RESOLUTION_AUDIO: - return download_dir / settings.DOWNLOAD_AUDIO_DIR / self.directory + return Path(settings.DOWNLOAD_AUDIO_DIR) / self.directory else: - return download_dir / settings.DOWNLOAD_VIDEO_DIR / self.directory + return Path(settings.DOWNLOAD_VIDEO_DIR) / self.directory def make_directory(self): return os.makedirs(self.directory_path, exist_ok=True) diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 34f1381..a597d11 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -341,7 +341,7 @@ def download_media(media_id): log.info(f'Successfully downloaded media: {media} (UUID: {media.pk}) to: ' f'"{filepath}"') # Link the media file to the object and update info about the download - media.media_file.name = str(filepath) + media.media_file.name = str(media.source.type_directory_path / media.filename) media.downloaded = True media.download_date = timezone.now() media.downloaded_filesize = os.path.getsize(filepath)