Skip to content

Upload image issue #37

@DhvaniBhesaniya

Description

@DhvaniBhesaniya

i am using the upload method just as instructed but i am getting this parse error. uploading is working fine , in the cloudinary it is being uploaded. i am using data url as a source to upload the image.

here is the error :
"
Upload result :: , Err(failed to parse:

{"asset_id":"c49b1d4902a9dfc2bfee942a38d675c0","public_id":"pwoyepnl4re9skaban5j","version":1728368129,"version_id":"d12154246f5b4b4fe949d8f6ddb8d59f","signature":"209f3fc898796e3d82d59cc6e193f4c544aa39cb","width":223,"height":226,"format":"png","resource_type":"image","created_at":"2024-10-08T06:15:29Z","tags":[],"bytes":4824,"type":"upload","etag":"0652a25b4542ff8a0312f814c6c9a8b1","placeholder":false,"url":"http://res.cloudinary.com/djigcn0dy/image/upload/v1728368129/pwoyepnl4re9skaban5j.png","secure_url":"https://res.cloudinary.com/djigcn0dy/image/upload/v1728368129/pwoyepnl4re9skaban5j.png","asset_folder":"","display_name":"pwoyepnl4re9skaban5j","api_key":"4273989199567"}

Caused by:
data did not match any variant of untagged enum UploadResult)
"

for now i have done like if it is ok then from the success response i will takew the secure url , but if this error comes i am extracting the secure url from the error msg, which in coding sense not good , can you help me to solve this error ,

here below is my code.

pub async fn upload_image_to_cloudinary(profile_img: &str) -> Result<String, String> {
let cloud_name = gett::("CLOUDINARY_CLOUD_NAME");
let api_key = gett::("CLOUDINARY_API_KEY");
let api_secret = gett::("CLOUDINARY_API_SECRET");

let options = UploadOptions::new();
let upload = Upload::new(
    api_key.to_string(),
    cloud_name.to_string(),
    api_secret.to_string(),
);

// Upload the image to Cloudinary
let upload_result = upload
    .image(Source::DataUrl(profile_img.into()), &options)
    .await;

println!("Upload result :: , {:?}", upload_result);

match upload_result {
    // If successful, return the secure URL
    Ok(result) => {
        if let UploadResult::Success(response) = result {
            Ok(response.secure_url)
        } else {
            Err("Upload failed, but no valid URL returned.".to_string())
        }
    }
    // Handle errors
    Err(e) => {
        // Attempt to parse and extract the secure_url from the error message
        let error_message = format!("{:?}", e);

        // Use regex to find the secure_url in the error message
        let re = Regex::new(r#""secure_url":"(https://[^"]+)""#).unwrap();
        if let Some(captures) = re.captures(&error_message) {
            if let Some(secure_url) = captures.get(1) {
                return Ok(secure_url.as_str().to_string());
            }
        }

        Err("Failed to extract secure_url from error".to_string())
    }
}

}
can you check if there is any problem ?.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions