Opened 7 years ago

Closed 7 years ago

#28402 closed Cleanup/optimization (wontfix)

Provide 'temporary_file_path' for InMemoryUploadedFile

Reported by: Thomas Güttler Owned by: nobody
Component: File uploads/storage Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was hit by this exception:

AttributeError: 'InMemoryUploadedFile' object has no attribute 'temporary_file_path'

The bad thing: It was in production, this was not detected by our CI .

Next thing I did: I implemented a method which provided my something similar to temporary_file_path.

Why not provide a property temporary_file_path for InMemoryUploadedFile.

I think this way you have the advantage of both: No file on disk if you don't want to and a file on disk if you want it.

What do you think?

Change History (4)

comment:1 by Theofanis Despoudis, 7 years ago

Looking at the source code, indeed we don't offer an API for temporary_file_path. However, do we need one? As It does not make sense to provide a path for an in memory file handler, for this abstraction. Plus you can get the file name by accessing the name property of the InMemoryUploadedFile instance.

comment:2 by Tim Graham, 7 years ago

Component: UncategorizedFile uploads/storage
Type: UncategorizedCleanup/optimization

Where does the exception come from -- your own code or from Django? I think adding the method would be backwards incompatible. Consider the code in FileSystemStorage._save() - - third party storages may have similar logic that would break with the proposed change.

comment:3 by Thomas Güttler, 7 years ago

Hi Graham, thank you for looking at this.

The code which uses temporary_file_path is in my code. This is no bug in django. This is a feature request.

Yes, naming the property "temporary_file_path" could break code like this.

A new name would be needed or a clear deprecation path.

Do you understand my use case: Get a file_name (as string) which contains the temporary upload in every case with one method.

I guess there are several hundred implementation which all do the same: put the data from MemoryUploadedFile into a temporary file.


comment:4 by Tim Graham, 7 years ago

Resolution: wontfix
Status: newclosed

Depending on your application's requirements (if every uploaded file needs to be written to disk), perhaps you'd want to define FILE_UPLOAD_HANDLERS = ['django.core.files.uploadhandler.TemporaryFileUploadHandler'] which removes MemoryFileUploadHandler from the default list. Alternatively, you can modify the upload handlers on the fly.

You didn't describe your use case, but I think these solutions suffice for most use cases. An API that effectively transforms an in-memory uploaded file into a temporary file seems more complicated.

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