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

Cloudinary API corrupts UTF-8 public IDs/file names #607

Closed
lionel-rowe opened this issue May 15, 2023 · 2 comments
Closed

Cloudinary API corrupts UTF-8 public IDs/file names #607

lionel-rowe opened this issue May 15, 2023 · 2 comments

Comments

@lionel-rowe
Copy link

lionel-rowe commented May 15, 2023

There doesn't seem to be a separate repo for the REST API, so I'm putting this here.

Background: I'm using Deno, which gives error: Uncaught Error: Dynamic require of "proxy-agent" is not supported when I try to import via https://esm.sh/[email protected]. Maybe worth opening a separate issue for that, but my use case is pretty simple, so I'd prefer to just use a simple fetch request rather than adding bloat by pulling in a whole SDK.

Explain your use case

I'm trying to upload a file via POST request. Everything seems to be working OK, except when the file name contains non-ASCII characters. For example, if public_id in my request is "蜡笔小新", the public_id returned in the response is "è�¡ç¬�å°�æ�°".

When I run the Node SDK in runkit, I notice it deals gracefully with UTF-8 public_ids, but I can't figure out how from skimming the code. Perhaps this should be added to the documentation? Not sure if it counts as a bug per se, but usually I just assume modern web APIs will automatically use UTF-8 as default.

Describe the problem you’re trying to solve

My code looks like this:

const config = {
    cloudName: getEnvVar('CLOUDINARY_CLOUD_NAME'),
    apiKey: getEnvVar('CLOUDINARY_API_KEY'),
    secretKey: getEnvVar('CLOUDINARY_SECRET_KEY'),
}

const url = `https://api.cloudinary.com/v1_1/${config.cloudName}/image/upload`

const opts = {
    public_id: file.name.split('.').slice(0, -1).join('.'),
    timestamp: String(Math.floor(Date.now() / 1000)),
    // use_filename: true,
}

// don't use any URL encoding etc. otherwise signature won't work
const naiveQps = Object.keys(opts).sort().map(k => `${k}=${opts[k as keyof typeof opts]}`).join('&')

const signature = toHashString(
    await crypto.subtle.digest('SHA-1', new TextEncoder().encode(naiveQps + config.secretKey)),
)

const formData = new FormData()

for (const [k, v] of Object.entries({
    ...opts,
    file,
    api_key: config.apiKey,
    signature,
})) {
    formData.append(k, v)
}

const res = await fetch(url, { method: 'POST', body: formData })
const data: CloudinaryResponseData = await res.json()

If I comment out public_id and uncomment use_filename under opts, the same problem still occurs (for a filename of "蜡笔小新.gif").

@rnamba-cloudinary
Copy link

Hi @lionel-rowe

Would you please open up a support ticket with our team at [email protected] as we will need to enable an account level flag to enable special characters as a public_id.

@lionel-rowe
Copy link
Author

lionel-rowe commented May 16, 2023

Edit: Looks like the issue is on my end — either there's something wrong with the file, Postman is doing something weird with the encoding, or the framework I'm using is. Closing for now. @rnamba-cloudinary thanks for your help! 🙏

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