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

Let Cloudinary resolve filename automatically #116

Open
wants to merge 3 commits into
base: 3.0
Choose a base branch
from

Conversation

RandyCupic
Copy link
Member

@RandyCupic RandyCupic commented Feb 17, 2025

In the past, we used the original filename, sanitized it, did some logic with appending extensions when needed and then served publicId to Cloudinary that we generated. This PR removes all this logic and lets Cloudinary use the original filename and resolve everything automatically.

The reason for this is the problem that we discovered on project, where some PDFs were recognized and uploaded as .ai files (see more here: https://support.cloudinary.com/hc/en-us/articles/23942629070866-Uploaded-PDF-is-detected-as-AI-format-or-vice-versa) and in order to solve that, Cloudinary had to tweak settings on our account to use actual extension to determine the format when the file is PDF or AI. This means that we have to always provide extension when uploading, so that Cloudinary knows it.

Also, I think this is a cleaner approach as we don't have to worry about this anymore, especially considering that this part of the NGRM was causing issues in the past. I tested with filenames that contain unicode characters as well as symbols and it seems like it's working as it should - unicodes actually do work while symbols are being automatically removed by Cloudinary. Also, this will preserve the original filename as much as possible.

Here are examples from my tests: the left side shows files uploaded with this PR, while the right side shows files uploaded without this PR:

Screenshot from 2025-02-17 17-48-25
Screenshot from 2025-02-17 17-51-38

Warning: this has also one drawback: if you try to upload extensionless files (images, videos and audio) of the same type (eg. video or image) and of the same name, but different format, it won't work. For example, if you have three videos: example.mp4, example.flv and example.3gp, it will fail because it will say that video with example name already exists.

I've introduced two parameters that can help to avoid this:

netgen_remote_media:
    cloudinary:
        append_extension: true
        unique_filenames: false

This configuration above also shows the default value. The first one will append extension to filename (so eg. sample.jpg will become sample_jpg.jpg). The second one uses Cloudinary's built-in feature to append unique string to the end of the filename so all files will always be unique. You can, of course, use both.

The picture below shows upload of the same file in all combinations:

image

PS: we will store the original filename (prior to any append) to the metadata on the resource and this can be used in your project, if needed. But Cloudinary's own URLs will always have the appended text, unfortunately.

I've also added a better display message when uploading, so that you can see what is the file which prevents you to upload your new file (filename and type/format). For example, here you can see what happens when you try to upload dictionary2.jpg in a folder which contain dictionary2.pdf:

image

@RandyCupic RandyCupic requested a review from iherak February 17, 2025 17:51
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

Successfully merging this pull request may close these issues.

1 participant