-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
djanog-pictures not compatible wiht django-storages #26
Comments
@vchrisb interesting. I have been using the async methods in a life system only. I believe we should still try to open the files, but handle the exception. I use a custom storage form Do you want to do the honors of adding a try bock around this? |
From my understanding it is not possible to open the files again when they are in-memory representations. |
What about: def _process_picture(field_file: PictureFieldFile) -> None:
with field_file.storage.open(field_file.name) as file:
with Image.open(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) |
If they are open or in memory, we can just read from them? Loading it into memory twice might not to be so memory efficient. |
So I did dig into it a bit more. Apparently S3boto is calling |
Another try. 😁 # TemporaryUploadedFile can't be reopened
if field_file.closed and getattr(field_file, "_file", None) is not None and isinstance(field_file._file, TemporaryUploadedFile):
field_file._file = None
with field_file.open() as file:
with Image.open(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) If |
That looks like a lot of ifs. Maybe we just try to open the file, catch the exception and open it from storage if need be. Maybe that's more EAFP? |
I further looked into it: boto3 is calling (unwanted) With
Long story short, creating a custom
Besides that, I think |
OK, you seem to have put a lot of research into this. I don't know if a custom storage is feasible for most. I would probably default to open the file from storage (always) and of course close it at the end as you suggested. |
I think for With that said, I can add a section to the README for |
Hm… yes, but it's still going to raise many questions aka tickets. Besides, we'd leave all files open, even if they are not post processesed, this seems wrong. I believe storages is doing the right thing. Opening a file from storage seems more plausible. It needs to be done in all async cases anyway. So it would be a more consistent behavior across all implementations. |
If someone is using django-storages with ManifestFilesMixin, they will also get a similar exception and need to look at the docs. Anyway, I'm happy with adapting the storage class and you might want to merge the addition to the Readme. ;) |
Hi @vchrisb, I understand how that works for you. But you also seem like a very talented software engineer. That aside, I don't really know what side effects no closing the files might have on other packages. Soooo, I would be more reluctant to have this package work regardless of the storage implementation and without interfering with other 3rd party code. I am happy to do the changes myself, but I feel like you deserve the credit here. Best Joe |
I'm just good at googling. You are the engineer who created this great package! :)
|
@vchrisb I gave some thought and I do feel your first impulse was right. This package should handle this issue out of the box and there is no reasonable disadvantage to open the file from storage. |
This issue persist, I'm using django-cookiecutter. I've used the custom storage that @vchrisb kindly provided to fix it. Thank you |
I finally did test
django-pictures
withdjango-storages
(using s3) and they are not compatible.When creating or updating an image I do get
The file cannot be reopened.
I think this is due to https://github.com/codingjoe/django-pictures/blob/main/pictures/tasks.py#L15 trying to open the (closed) file again. From my understanding
django-storages
is using in-memory for file processing, which does not allow it to be reopened.I also looked at
django-stdimage
and this implementation is opening the file from storage:https://github.com/codingjoe/django-stdimage/blob/4c7194a88fc970cb0d910f444a2de29911d4a630/stdimage/models.py#L72
❓ Maybe adopting this would fix this?
❓ Or not closing it in the first place before processing finished?
The text was updated successfully, but these errors were encountered: