Fix #976: fix cover attachment migration under S3

This commit is contained in:
Eliot Berriot 2019-11-27 16:47:24 +01:00
parent 73e7211398
commit ad7274ff90
No known key found for this signature in database
GPG Key ID: 6B501DFD73514E14
1 changed files with 18 additions and 11 deletions

View File

@ -13,17 +13,24 @@ def create_attachments(apps, schema_editor):
if path.lower().endswith('.png'): if path.lower().endswith('.png'):
return "image/png" return "image/png"
return "image/jpeg" return "image/jpeg"
qs = Album.objects.filter(attachment_cover=None).exclude(cover="").exclude(cover=None)
for album in Album.objects.filter(attachment_cover=None).exclude(cover="").exclude(cover=None): total = qs.count()
try: print('Creating attachments for {} album covers, this may take a while…'.format(total))
album_attachment_mapping[album] = Attachment( from django.core.files.storage import FileSystemStorage
file=album.cover, for i, album in enumerate(qs):
size=album.cover.size, if isinstance(album.cover.storage._wrapped, FileSystemStorage):
mimetype=get_mimetype(album.cover.path), try:
) size = album.cover.size
except FileNotFoundError: except FileNotFoundError:
print('Skipping missing cover file {}'.format(album.cover.path)) # can occur when file isn't found on disk or S3
print(" Warning: cover file wasn't found in storage: {}".format(e.__class__))
size = None
album_attachment_mapping[album] = Attachment(
file=album.cover,
size=None,
mimetype=get_mimetype(album.cover.name),
)
print('Commiting changes…')
Attachment.objects.bulk_create(album_attachment_mapping.values(), batch_size=2000) Attachment.objects.bulk_create(album_attachment_mapping.values(), batch_size=2000)
# map each attachment to the corresponding album # map each attachment to the corresponding album
# and bulk save # and bulk save