Closed theelous3 closed 2 years ago
Actually I think the better solution here is to just ignore all of that entirely and not assign things of any type at all, removing attrs like file, and let the user do whatever they want with the given info.
The MultipartParser
API treats all parts as file-like binary data because on protocol level there is no real difference between file uploads or form fields. Both are binary data with headers and both can be larger than what you would want to store in a string. The only real difference is that browsers add a filename
field to the Content-Disposition
header if the data came from a file.
So, I think this issue is based on a misunderstanding. MultipartPart.file
is only false during the header parsing phase, and after that, always has a binary file-like value. It is not a flag indicating whether or not the part was a HTTP form file upload. The application should check for MultipartPart.filename
to detect file uploads.
The high-level parse_form_data
function does exactly that: It splits all parts between text fields and file uploads based on MultipartPart.filename
(and MultipartPart.is_buffered()
for text fields that are too large to fit into memory).
There's nothing to indicate that the above is a file. It should be treated as regular form data. If I'm reading correctly, only the presence of the filename param indicates a file.
It's not clear if the reverse of this is the case, where if no filename is given but the content type is app/octet it's inferred that the content is a file. I think it doesn't make sense to presume any and all app/octet is a file, so filename seems the only good indicator.
Side note: the default value for
MultipartPart.file
isFalse
, but it becomes aBytesIO
obj. It should instead be used as a flag and setTrue
, andMultipartPart.value
should be the only way to fetch the contents of any and all parts.