diff --git a/readme.md b/readme.md index f529194..a362a6e 100644 --- a/readme.md +++ b/readme.md @@ -9,6 +9,15 @@ me know. ## Upload an image +Upload can be done from different sources: + +- local file +- remote file +- data url [rfc2397](https://datatracker.ietf.org/doc/html/rfc2397) + + +### Local file + ```rust use cloudinary::upload::{UploadOptions, Source, Upload}; let options = UploadOptions::new().set_public_id("file.jpg".to_string()); @@ -16,6 +25,27 @@ let upload = Upload::new("api_key".to_string(), "cloud_name".to_string(), "api_s let result = upload.image(Source::Path("./image.jpg".into()), &options); ``` +### Remote file + +```rust +use cloudinary::upload::{UploadOptions, Source, Upload}; +let image_url = "https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png"; +let options = UploadOptions::new().set_public_id("1x1.png".to_string()); +let upload = Upload::new("api_key".to_string(), "cloud_name".to_string(), "api_secret".to_string() ); +let result = upload.image(Source::Url(image_url.try_into().unwrap(), &options); +``` + +### Data url + +```rust +use cloudinary::upload::{UploadOptions, Source, Upload}; +let data_url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; +let options = UploadOptions::new().set_public_id("1x1.png".to_string()); +let upload = Upload::new("api_key".to_string(), "cloud_name".to_string(), "api_secret".to_string() ); +let result = upload.image(Source::DataUrl(data_url.into()), &options); +``` + + ## Transform an image Currently supported transformations: diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 398aad4..1ba0bb1 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -9,19 +9,45 @@ use crate::{ }, }; -#[tokio::test] -async fn test_image_upload_from_url() { +fn env() -> (String, String, String) { dotenv().ok(); - let api_secret = var("CLOUDINARY_API_SECRET").expect("enviroment variables not set"); - let api_key = var("CLOUDINARY_API_KEY").expect("enviroment variables not set"); - let cloud_name = var("CLOUDINARY_CLOUD_NAME").expect("enviroment variables not set"); + let api_key = var("CLOUDINARY_API_KEY").expect("environment variables not set"); + let cloud_name = var("CLOUDINARY_CLOUD_NAME").expect("environment variables not set"); + let api_secret = var("CLOUDINARY_API_SECRET").expect("environment variables not set"); + + (api_key, cloud_name, api_secret) +} +#[tokio::test] +async fn test_image_upload_from_base64() { + let (api_key, cloud_name, api_secret) = env(); + let cloudinary = Upload::new(api_key, cloud_name, api_secret); + let image_base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; + let public_id = "image_upload_from_base64"; + + let options = UploadOptions::new() + .set_public_id(public_id.into()) + .set_overwrite(true); + let res = cloudinary + .image(Source::DataUrl(image_base64.into()), &options) + .await + .unwrap(); + + match res { + Success(img) => assert_eq!(img.public_id, public_id), + Error(err) => panic!("{}", err.error.message), + } +} + +#[tokio::test] +async fn test_image_upload_from_url() { + let (api_key, cloud_name, api_secret) = env(); let cloudinary = Upload::new(api_key, cloud_name, api_secret); let image_url = "https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png"; let public_id = "image_upload_from_url"; let options = UploadOptions::new() - .set_public_id(String::from(public_id)) + .set_public_id(public_id.into()) .set_overwrite(true); let res = cloudinary .image(Source::Url(image_url.try_into().unwrap()), &options) @@ -36,20 +62,16 @@ async fn test_image_upload_from_url() { #[tokio::test] async fn test_image_upload_from_path() { - dotenv().ok(); - let api_secret = var("CLOUDINARY_API_SECRET").expect("enviroment variables not set"); - let api_key = var("CLOUDINARY_API_KEY").expect("enviroment variables not set"); - let cloud_name = var("CLOUDINARY_CLOUD_NAME").expect("enviroment variables not set"); - + let (api_key, cloud_name, api_secret) = env(); let cloudinary = Upload::new(api_key, cloud_name, api_secret); let image_path = "./assets/1x1.png"; let public_id = "image_upload_from_path"; let options = UploadOptions::new() - .set_public_id(String::from(public_id)) + .set_public_id(public_id.into()) .set_overwrite(true); let res = cloudinary - .image(Source::Path(image_path.try_into().unwrap()), &options) + .image(Source::Path(image_path.into()), &options) .await .unwrap(); diff --git a/src/upload/mod.rs b/src/upload/mod.rs index ddd84b3..b1a0327 100644 --- a/src/upload/mod.rs +++ b/src/upload/mod.rs @@ -38,6 +38,7 @@ pub struct Upload { pub enum Source { Path(PathBuf), Url(Url), + DataUrl(String), } impl Upload { @@ -61,6 +62,7 @@ impl Upload { let file = match src { Source::Path(path) => prepare_file(&path).await?, Source::Url(url) => Part::text(url.as_str().to_string()), + Source::DataUrl(base64) => Part::text(base64), }; let multipart = self.build_form_data(options).part("file", file); let url = format!(