Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Setting to allow files to be uploaded with their original filename #1264

Open
coredumperror opened this issue Jul 20, 2023 · 2 comments

Comments

@coredumperror
Copy link

I recently banged my head against the wall for a while to figure out why files being uploaded from my site were being renamed with underscores and non-alphanumeric characters (like apostrophes) were being deleted. Turns out this is Django's fault, because their Storage.get_valid_name() method calls django.utils.text.get_valid_filename() to convert the filenames of every file uploaded.

This is all well and good if you're using ancient filesystems and long-dead browsers, but it's pointless in the modern age, and is more confusing than anything else.

However, this is a long-established default behavior, so simple changing it unilaterally is a bad idea. Thus, I propose a new boolean setting for django-storages, perhaps named STORAGES_MANGLE_FILENAMES, which defaults to True. Users can then set it to False to turn off automatic filename mangling.

This would be implemented as an override to the Storage.get_valid_name() method in each subclass of Storage defined by django-storages (probably just a few lines in BaseStorage). Something like this:

def get_valid_name(self, name):
    if settings.STORAGES_MANGLE_FILENAMES:
        return super().get_valid_name(name)
    else:
        return name
@sakthips
Copy link

@coredumperror , did you got anything to sort this out. I'm facing the same issue and i'm breaking my head around this. Let me know, if you found anything.

@coredumperror
Copy link
Author

I couldn't recall for sure if I'd found a solution to this, but looking through my shop's code for these method names led me to a part of my code that calls django.utils.text.get_valid_filename() intentionally, to mangle the filename of an uploading file in the same way Django does, so our form code can do a proper comparison between the incoming file and all existing files in the S3 bucket.

So I think I just worked around this issue, rather than finding an actual fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants