File extension not stored in public Id

Comments

14 comments

  • Avatar
    Yakir Perlin

    Hi Christian,

    I'm sorry for the late response.

    Could you please try to add "use_filename:true" :

    cloudinary.v2.uploader.upload("<file_path>", {use_filename :true, resource_type:'raw'}, function(result,err) {

    console.log(result)
    });
     
    Please let me know if it helps you.
     
    Best,
    Yakir
    0
    Comment actions Permalink
  • Avatar
    Parik Panchal

    @Yakir Will this ensure uniqueness of filename as well? So if I upload 2 files of same name of "audio.wav", how will these files stored?

    0
    Comment actions Permalink
  • Avatar
    Marissa Masangcay

    Hi Parik,

    When you use the parameter 'use_filename: true'  the file name is normalized and random characters are appended to ensure uniqueness so if you upload two files with the same name they would be given two different public IDs. However, if you don't want this feature you can also include the parameter 'unique_filename: false' and then random characters won't be appended to the public ID.

    Please note that if you use both the parameters 'use_filename: true' and 'unique_filename: false' and you upload two files with the same name then the original file will be overwritten by the newly uploaded file. If you don't want this behavior you can also include the parameter 'overwrite: false' so that it will not replace the original resource.

    I hope this helps!

    0
    Comment actions Permalink
  • Avatar
    Sunday

    I tried saving a file with extension on .doc and .docs but instead of getting url with the original file name and the extension, I got a different result at shown below. Uploading pdf file works perfectly. Please what may be the error.

    result:
    { public_id: 'jextaly/file_jwv888',
    version: 1581601783,
    resource_type: 'raw',
    created_at: '2020-02-13T13:49:43Z',
    tags: [],
    bytes: 141824,
    type: 'upload',
    placeholder: false,
    url:
    'http://res.cloudinary.com/raw/upload/v1581601783/jextaly/file_jwv888',
    secure_url:
    'https://res.cloudinary.com/raw/upload/v1581601783/jextaly/file_jwv888',
    access_mode: 'public',
    original_filename: 'file' }

    0
    Comment actions Permalink
  • Avatar
    Aleksandar Kostadinov

    Hi,

    Does your original file contain an extension prior to uploading? By default, when uploading raw files, we will take the original extension of the file, therefore, if the original file is missing one it will not get assigned.

    In addition, could you please share your upload code, are you using the upload method or upload_stream etc?

    0
    Comment actions Permalink
  • Avatar
    Sunday

    @Aleksandar, my original file contains the .doc or Docx extensions. .pdf extensions work perfectly but .doc and .docx does not work after upload from my Node server 

    0
    Comment actions Permalink
  • Avatar
    Aleksandar Kostadinov

    Thanks for these details. The PDFs work due to them being recognised and treated as Image files rather than Raw. May I please ask you to share the code you are using to upload and the parameters you are passing?

    0
    Comment actions Permalink
  • Avatar
    Sunday

    ```


    function uploadToCloudinary(buffer) {
    returnnewPromise((resolve, reject)=> {
    constoptions={
    folder: cloudinaryFolder,
    resource_type: "raw",
    // use_filename:true,
    overwrite: false
    // allowed_formats: ['jpeg', 'jpg', 'png', 'pdf','doc', 'docx', 'txt']
    }
    conststream=cloudinary.v2.uploader.upload_stream(
    options,
    (err,result)=>{
    console.trace({result})
    if(err) return reject(err)
    return resolve(result)
    }
    )
    intoStream(buffer).pipe(stream)
    })
    }
    ```
    0
    Comment actions Permalink
  • Avatar
    Eyal Katz Talmon

    Hi,

    In order for us to further investigate, it would be greatly helpful if you could kindly open a support ticket at support@cloudinary.com. Please attach a relevant file that may help us to reproduce the issue. Any additional information would be appreciated as well. Thanks!

    0
    Comment actions Permalink
  • Avatar
    Milan Simonović

    I've found out how to reproduce this with the latest node SDK (1.32.0). 

    This also happens when uploading from the Media Library - public id does not contain original file extension. Here's a file uploaded from the console:

    ```

     {
          asset_id: '426c3045d22cd7b9acbcf72638d60499',
          public_id: 'samples/food/image_uz4owh',
          format: 'png',
          version: 1664896621,
          resource_type: 'image',
          type: 'upload',
          created_at: '2022-10-04T15:17:01Z',
          folder: 'samples/food',
          url: 'http://res.cloudinary.com/***/image/upload/v1664896621/samples/food/image_uz4owh.png',
          secure_url: 'https://res.cloudinary.com/***/image/upload/v1664896621/samples/food/image_uz4owh.png'
        },

    ```

    original file name was image.png, and seems like the console uploader added `_uz4owh`. You can see from the url that the uploaded file did contain .png extension. 

    Same thing happens when uploading with the node SDK with `use_filename:true` and `unique_filename:false`:

    ```

    cloudinary.uploader.upload('test/resources/image.png', {
            folder: destinationFolder,
            resource_type: 'auto',
            use_filename: true,
            unique_filename: false,
            overwrite: true
        });

    ```

    so when the first argument is a path to the file then public id does not contain the .png extension. But it's fairly easy to fix by providing the public_id:

    ```

    cloudinary.uploader.upload('test/resources/image.png', {

            public_id: `${destinationFolder}/image.png`,
            folder: destinationFolder,
            resource_type: 'auto',
            use_filename: true,
            unique_filename: false,
            overwrite: true
        });

    ```

    0
    Comment actions Permalink
  • Avatar
    John Herrera

    Hi Milan,

    For the first question about file extension in the public_id, file extension is normally not included unless the file is "raw" type.

    From the docs here:
    https://cloudinary.com/documentation/image_upload_api_reference#upload_optional_parameters

    Description:
    The identifier that's used for accessing and delivering the uploaded asset.

    If not specified, then the Public ID of the asset will either be comprised of random characters or will use the original file's filename, depending whether use_filename was set to true.

    Notes:

    • The Public ID value for images and videos should not include a file extension. Include the file extension for raw files only.
    • Can be up to 255 characters, including non-English characters, periods (.), forward slashes (/), underscores (_), hyphens (-).
    • Public ID values cannot begin or end with a space or forward slash (/). Additionally, they cannot include the following characters: ? & # \ % < > +

    For the second question about the public_id value being set with the trailing "_uz4owh" value.
    The combination of "use_filename": true and "unique_filename": false should eliminate this behavior.

    If you can confirm these are configured as such and you're still receiving the trailing "_uz4owh" value please let us know and we will look closer.

    1
    Comment actions Permalink
  • Avatar
    Milan Simonović

    Hi John,

     

    thanks! I missed that part, but not using the extension makes it cumbersome for my use case - a file editor. What happens if two different resources have the same name but different extension (cat.png, cat.jpg)? 

    0
    Comment actions Permalink
  • Avatar
    Stephen Doyle

    Hi Milan,

    The file extension shouldn't be part of the Public ID for image or video assets, because you can change the extension on the URL to change the response format via our transformations features: https://cloudinary.com/documentation/image_transformations#transformation_url_structure

    As such, it's not possible to store two image files named 'cat' with the same delivery type. If you have a file named 'cat.jpg' and upload another file called 'cat.gif', if the naming method you choose for your files is base don the uploaded filename, without any additional characters, it will overwrite the previous file.

    If you want to upload two files to Cloudinary where the source filename [excluding extension] is the same, but the files should be stored differently, you can choose the public_id to use manually, use the unique_filename option to add random characters to ensure uniqueness, or use a totally random public_id

    Regards,
    Stephen

    1
    Comment actions Permalink
  • Avatar
    Milan Simonović

    Hi Stephen,

    thanks for the explanation, now it makes sense! I just started with cloudinary 4 days ago and havent yet looked at transformations. Think it would've been helpful to put the link to the transformation url page on the image upload section where it says public id should not include extension.

    best,
    Milan

    0
    Comment actions Permalink

Please sign in to leave a comment.