Opened 4 years ago

Closed 4 years ago

Last modified 4 weeks 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 Changed 4 years ago by Aymeric Augustin

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 Changed 4 years ago by Claude Paroz

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

comment:3 in reply to:  2 Changed 4 years ago by Vlastimil Zíma

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 Changed 4 years ago by Claude Paroz

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 Changed 4 years ago by Marcin Biernat

Owner: changed from nobody to Marcin Biernat
Status: newassigned

comment:6 Changed 4 years ago by Marcin Biernat

Has patch: set

comment:7 Changed 4 years ago by Grzegorz Szczepańczyk

Triage Stage: AcceptedReady for checkin

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

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 Changed 4 weeks ago by Tim Graham <timograham@…>

In 3f16e4df:

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

comment:10 Changed 4 weeks ago by Tim Graham <timograham@…>

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