fix relative media_file path

FileField should store a relative path, to make their "url" attribute work
pull/297/head
Laurent DEFERT 2022-12-28 10:34:34 +01:00
parent 57921ca6b9
commit 0e278bc8c4
3 changed files with 32 additions and 3 deletions

View File

@ -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)
]

View File

@ -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)

View File

@ -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)