#23759: Storage.get_available_name should preserve all file extensions, not just
the first one
-------------------------------------+-------------------------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: File | Version: 1.7
uploads/storage | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by thenewguy):
Sorry for the delay. I just found out status updates on tickets I am
watching have been going to spam. I guess that is why I never get
updates.
In this case, I do not think it is an issue if a valid part of the
filename is assumed to be an extension because `get_available_name` does
not promise to return the same filename, and it doesn't matter where the
random string is in the name to be "available".
However, the mimetypes module could be used to determine if an extension
was actually an extension. If mimetypes returns a type or an encoding,
add the extension to the extensions list, otherwise break and assume there
are no more extensions.
Also, this only happens if the submitted filename already exists.
I think it would be very nice to be able to maintain reliable file
extensions out of the box. This is important for many cases, including
when a user downloads the file.
Here is a sample mixin I've used in the past (keep in mind it does not
query mimetypes, but that is trivial)
{{{
class GetUUIDAvailableNameStorageMixin(object):
def get_available_name(self, name):
"""
Returns a hex encoded uuid filename, using the extensions provided
by the
input filename, that's suitable for use in the target storage
system.
"""
exts = []
root = ext = name
while ext:
root, ext = splitext(root)
exts.insert(0, ext)
name = "%s%s" % (uuid4().hex, "".join(exts))
return super(GetUUIDAvailableNameStorageMixin,
self).get_available_name(name)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23759#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.ec678b92e2c3c584c591d801e2c9afd9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.