Set Media.download_date for imported files

This commit is contained in:
tcely 2025-03-01 02:54:56 -05:00 committed by GitHub
parent 20e879767c
commit 3411bac78a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,5 @@
import os import os
from datetime import timedelta
from pathlib import Path from pathlib import Path
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from common.logger import log from common.logger import log
@ -54,5 +55,16 @@ class Command(BaseCommand):
item.media_file.name = str(Path(filepath).relative_to(item.media_file.storage.location)) item.media_file.name = str(Path(filepath).relative_to(item.media_file.storage.location))
item.downloaded = True item.downloaded = True
item.downloaded_filesize = Path(filepath).stat().st_size item.downloaded_filesize = Path(filepath).stat().st_size
# set a reasonable download date
date = item.posix_epoch + timedelta(seconds=Path(filepath).stat().st_mtime)
if item.published and item.published > date:
date = item.published
if item.has_metadata:
metadata_date = item.posix_epoch + timedelta(seconds=item.loaded_metadata.get('epoch', 0))
if metadata_date and metadata_date > date:
date = metadata_date
if item.download_date and item.download_date > date:
date = item.download_date
item.download_date = date
item.save() item.save()
log.info('Done') log.info('Done')