Code

#18899 closed Cleanup/optimization (fixed)

FileSystemStorage.save should support any file-like objects

Reported by: vzima Owned by: biern
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.

Attachments (0)

Change History (8)

comment:1 Changed 18 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Cleanup/optimization

comment:2 follow-up: Changed 17 months ago by 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>))?

comment:3 in reply to: ↑ 2 Changed 17 months ago by vzima

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 17 months ago by claudep

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 14 months ago by biern

  • Owner changed from nobody to biern
  • Status changed from new to assigned

comment:7 Changed 14 months ago by gszczepanczyk

  • Triage Stage changed from Accepted to Ready for checkin

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

  • Resolution set to fixed
  • Status changed from assigned to closed

In 129d2e8f85b1e80f10ec967b72ba0af9b239019f:

Merge pull request #775 from HiddenData/ticket-18899

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.