From 1b465aa903137bd04787249caa9e4f7b23a2d497 Mon Sep 17 00:00:00 2001 From: loulous27 Date: Mon, 6 Apr 2026 09:51:47 +0200 Subject: [PATCH] ZIP_Galerie rework (end of memory leak ? ) --- photologue/views.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/photologue/views.py b/photologue/views.py index d78afb8..a3a6c27 100644 --- a/photologue/views.py +++ b/photologue/views.py @@ -171,6 +171,7 @@ class GalleryDetailView(LoginRequiredMixin, DetailView): class GalleryDownload(LoginRequiredMixin, DetailView): + ### IN FUTURE, PUT IT as Django Task model = Gallery def get(self, request, *args, **kwargs): @@ -179,19 +180,28 @@ class GalleryDownload(LoginRequiredMixin, DetailView): """ # Create zip file with pictures gallery = self.get_object() - byte_data = BytesIO() - zip_file = zipfile.ZipFile(byte_data, "w") - for photo in gallery.photos.filter(is_public=True): - filename = os.path.basename(os.path.normpath(photo.image.path)) - zip_file.write(photo.image.path, filename) - zip_file.close() + 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): + filename = os.path.basename(os.path.normpath(photo.image.path)) + zip_file.write(photo.image.path, filename) + zip_file.close() + + # Return the path to it + + return redirect("/"+str(gallery_zip).replace("\\","/")) #windows fix # Return zip file - response = HttpResponse( - byte_data.getvalue(), content_type="application/x-zip-compressed" - ) - response["Content-Disposition"] = f"attachment; filename={gallery.slug}.zip" - return response + + + #response = HttpResponse( + # byte_data.getvalue(), content_type="application/x-zip-compressed" + #) + #response["Content-Disposition"] = f"attachment; filename={gallery.slug}.zip" + #return response class GalleryUpload(PermissionRequiredMixin, FormView):