Opened 12 years ago

Closed 11 years ago

Last modified 8 years ago

#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 Aymeric Augustin, 11 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 by Claude Paroz, 11 years ago

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>))?

in reply to:  2 comment:3 by Vlastimil Zíma, 11 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 Claude Paroz, 11 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 Marcin Biernat, 11 years ago

Owner: changed from nobody to Marcin Biernat
Status: newassigned

comment:7 by Grzegorz Szczepańczyk, 11 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Honza Král <Honza.Kral@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 129d2e8f85b1e80f10ec967b72ba0af9b239019f:

Merge pull request #775 from HiddenData/ticket-18899

Fixed #18899 -- FileSystemStorage.save should support any file-like objects

comment:9 by Tim Graham <timograham@…>, 8 years ago

In 3f16e4df:

Fixed #27145 -- Updated Storage.save() docs for refs #18899.

comment:10 by Tim Graham <timograham@…>, 8 years ago

In f79dce16:

[1.10.x] Fixed #27145 -- Updated Storage.save() docs for refs #18899.

Backport of 3f16e4df494dc9143d110b886c8e8608f9d2b584 from master

Note: See TracTickets for help on using tickets.
Back to Top