#18899 closed Cleanup/optimization (fixed)
FileSystemStorage.save should support any file-like objects
| Reported by: | Vlastimil Zíma | Owned by: | Marcin Biernat |
|---|---|---|---|
| Component: | File uploads/storage | Version: | 1.4 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I propose generic support of FileSystemStorage.save(), so content argument can be any object with read([size]) method.
I see no reason why FileSystemStorage does not support regular file-like objects. IT is common practise to support any file-like objects in python libraries, where file-like object is an argument.
Blocker of this is content.chunks() call which itself is nothing else then generator over file.read(chunk_size). Storage itself could easily call content.read(chunk_size) directly which grants support to various file-like objects.
Calls like this are just weird:
storage.save(target, File(open(filename)))
The File objects is here only because it has chunks() method required by FileSystemStorage.save() call.
I have seen #8204 but I do not require anything so specific. It is just painful, when you want to use storage for anything else than file from query or form.
Change History (10)
comment:1 by , 13 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|---|
| Type: | Uncategorized → Cleanup/optimization |
follow-up: 3 comment:2 by , 13 years ago
comment:3 by , 13 years ago
Replying to claudep:
In #15644, we added support for file-like objects to the File wrapper. Do you think it is still too much to call
storage.save(target, File(<file-like-object>))?
Yes. It is seems fairly excessive - you make File object from file object - only to provide chunks() method. Also this is not a solution for other file-like objects, e.g. StringIO.
comment:4 by , 13 years ago
Wrapping StringIO in a File object should work. Report it as a separate ticket if you can show that it does not.
Maybe the simplest fix would be to wrap content in a File object if it has no chunks method. This would preserve compatibility in the case someone passes a custom content instance (which might implement chunks in a non-standard way).
comment:5 by , 13 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:7 by , 13 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:8 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
In #15644, we added support for file-like objects to the File wrapper. Do you think it is still too much to call
storage.save(target, File(<file-like-object>))?