ZIP_Galerie rework (end of memory leak ? )
This commit is contained in:
parent
54c9548a52
commit
1b465aa903
1 changed files with 21 additions and 11 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue