ZIP_Galerie rework (end of memory leak ? )

This commit is contained in:
loulous27 2026-04-06 09:51:47 +02:00
parent 54c9548a52
commit 1b465aa903

View file

@ -171,6 +171,7 @@ class GalleryDetailView(LoginRequiredMixin, DetailView):
class GalleryDownload(LoginRequiredMixin, DetailView): class GalleryDownload(LoginRequiredMixin, DetailView):
### IN FUTURE, PUT IT as Django Task
model = Gallery model = Gallery
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -179,19 +180,28 @@ class GalleryDownload(LoginRequiredMixin, DetailView):
""" """
# Create zip file with pictures # Create zip file with pictures
gallery = self.get_object() gallery = self.get_object()
byte_data = BytesIO()
zip_file = zipfile.ZipFile(byte_data, "w") gallery_year = Path("media/photos/"+str(+gallery.date_start.year))
gallery_zip = gallery_year / (gallery.slug +".zip")
with open(gallery_zip,"wb") as zip_bytes :
zip_file = zipfile.ZipFile(zip_bytes, "w")
for photo in gallery.photos.filter(is_public=True): for photo in gallery.photos.filter(is_public=True):
filename = os.path.basename(os.path.normpath(photo.image.path)) filename = os.path.basename(os.path.normpath(photo.image.path))
zip_file.write(photo.image.path, filename) zip_file.write(photo.image.path, filename)
zip_file.close() zip_file.close()
# Return the path to it
return redirect("/"+str(gallery_zip).replace("\\","/")) #windows fix
# Return zip file # Return zip file
response = HttpResponse(
byte_data.getvalue(), content_type="application/x-zip-compressed"
) #response = HttpResponse(
response["Content-Disposition"] = f"attachment; filename={gallery.slug}.zip" # byte_data.getvalue(), content_type="application/x-zip-compressed"
return response #)
#response["Content-Disposition"] = f"attachment; filename={gallery.slug}.zip"
#return response
class GalleryUpload(PermissionRequiredMixin, FormView): class GalleryUpload(PermissionRequiredMixin, FormView):