From e514f8c24c459033ea7a4638acbf62af666a556d Mon Sep 17 00:00:00 2001 From: Johannes Maron Date: Sat, 6 Aug 2022 12:19:55 +0200 Subject: [PATCH] Fix #26 -- Always open files from storage for processing --- pictures/tasks.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pictures/tasks.py b/pictures/tasks.py index 8d9fba2..70e2fb3 100644 --- a/pictures/tasks.py +++ b/pictures/tasks.py @@ -12,12 +12,14 @@ def _process_picture(field_file: PictureFieldFile) -> None: - field_file.open() # the file needs to be open - with Image.open(field_file.file) as img: - for ratio, sources in field_file.aspect_ratios.items(): - for file_type, srcset in sources.items(): - for width, picture in srcset.items(): - picture.save(img) + # field_file.file may already be closed and can't be reopened. + # Therefore, we always open it from storage. + with field_file.storage.open(field_file.name) as fs: + with Image.open(fs) as img: + for ratio, sources in field_file.aspect_ratios.items(): + for file_type, srcset in sources.items(): + for width, picture in srcset.items(): + picture.save(img) process_picture = _process_picture