Skip to content

Commit

Permalink
Try s3 only if using S3Boto3Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Sep 26, 2024
1 parent 4360178 commit 8447d57
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions electionleaflets/apps/leaflets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,33 @@ def done(self, form_list, **kwargs):
"images-image"
]
uploaded_images = json.loads(images_text)
bucket = self.storage.file_storage.bucket

storage_backend = getattr(settings, 'DEFAULT_FILE_STORAGE')
if storage_backend == 'django.core.files.storage.FileSystemStorage':
# use local file storage set in local.py
bucket = getattr(settings, 'DEFAULT_FILE_STORAGE')
else:
bucket = self.storage.file_storage.bucket

for file_path in uploaded_images:
file_name = file_path.split("/")[-1]
file_name = file_name.replace(" ", "-")
copy_source = {
"Bucket": bucket.name,
"Key": file_path,
}
if storage_backend == 'django.core.files.storage.FileSystemStorage':
copy_source = file_path
else:
copy_source = {
"Bucket": bucket.name,
"Key": file_path,
}

new_file_name = f"leaflets/{leaflet.pk}/{file_name}"
moved_file = bucket.Object(new_file_name)
moved_file.copy(copy_source)

if storage_backend != 'django.core.files.storage.FileSystemStorage':
moved_file = bucket.Object(new_file_name)
moved_file.copy(copy_source)
new_file_name_raw = f"raw_{new_file_name}"
moved_file_raw = bucket.Object(new_file_name_raw)
moved_file_raw.copy(copy_source)
new_file_name_raw = f"raw_{new_file_name}"
moved_file_raw = bucket.Object(new_file_name_raw)
moved_file_raw.copy(copy_source)

image = LeafletImage(leaflet=leaflet)
image.image.name = new_file_name
Expand Down

0 comments on commit 8447d57

Please sign in to comment.