ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Do not lowercase my filename when uploading assets.

Open geertjanknapen1 opened this issue 1 year ago • 7 comments

For a project, I need to be able to display assets in the browser (they are PDF files). Due to these PDF files being technical documents, there are already QR codes made for these files (mywebsite.test/files/MyFile.pdf)

As you can see they are camel-cased.

When uploading an asset, Statamic does: in_array(trim(strtolower($file->getClientOriginalExtension())), $extensions). So I cannot upload files with uppercased characters in the filename. For me, this is an issue at the moment, but it's pretty niche of course.

I would like to propose a new setting on the asset container that makes us able to toggle between allowing and disallowing uppercased characters? Of course defaulting to uppercase not allowed.

Again, this is a niche problem, but maybe something other people will run into as well.


Where the code is located: src/Validation/AllowedFile/IsAllowed()

It looks like this;

private function isAllowed(UploadedFile $file): bool
{
    $extensions = array_merge($this->extensions, config('statamic.assets.additional_uploadable_extensions', []));

    return in_array(trim(strtolower($file->getClientOriginalExtension())), $extensions);
}

geertjanknapen1 avatar Jul 12 '24 10:07 geertjanknapen1

You can opt-out of filenames being converted to lowercase, by toggling the lowercase config option in your config/statamic/assets.php config file.

/*
|--------------------------------------------------------------------------
| Enforce Lowercase Filenames
|--------------------------------------------------------------------------
|
| Control whether asset filenames will be converted to lowercase when
| uploading and renaming. This can help you avoid file conflicts
| when working in case-insensitive filesystem environments.
|
*/

'lowercase' => true,

duncanmcclean avatar Jul 12 '24 10:07 duncanmcclean

But that wouldn't be on an asset container-based level, right? If I set this to false it would happen for all my containers, right?

geertjanknapen1 avatar Jul 12 '24 11:07 geertjanknapen1

Correct.

There's currently no way to set it on a per-container basis. Good idea though!

duncanmcclean avatar Jul 12 '24 11:07 duncanmcclean

I just stumbled upon when migrating from a huge wordpress site where I need to import assets and have a similar problem as OP. I can't change the file paths as there are external services depending on the exact naming of the files.

When importing/uploading the files via the Asset::upload my filenames get mangled.. I don't however want to disable this system-wide, I just don't want my imported files to get renamed..

bernhardberger avatar Aug 20 '25 18:08 bernhardberger

I don't want to be 'that guy', but wouldn't it be as simple as the following? (Crude example, but you get the jist)

private function isAllowed(UploadedFile $file): bool
{
    $extensions = array_merge($this->extensions, config('statamic.assets.additional_uploadable_extensions', []));
    $ext = trim(strtolower($file->getClientOriginalExtension()));

    if ($upperAllowedAssetContainerSetting) {
        $ext = trim($file->getClientOriginalExtension());
    }

    return in_array($ext, $extensions);
}

Obviously, $upperAllowedAssetContainerSetting would have to be created (and given a better name ;) ). I might be missing something where it's absolutely needed that files are lowercased, but Statamic does support it (over the whole application, instead of only X asset container), so I don't think I'm missing something..?

geertjanknapen1 avatar Aug 21 '25 13:08 geertjanknapen1

I am using a AssetCreating Event to rename files upon upload. You'd need to give it a try, not sure if you are able to retrieve the original filename at this point. The renaming is buried in the AssetUploader.

andjsch avatar Aug 21 '25 14:08 andjsch

I might be missing something where it's absolutely needed that files are lowercased

So many people would run into case sensitivity issues.

Client uploads File.txt and file.txt. It's fine on the server because its Linux. Then when you clone the site, your Mac can't handle it because it thinks they're two of the same file.

jasonvarga avatar Aug 21 '25 14:08 jasonvarga