From 2c308edc11d6c31fce91f93408c46d8c5c3ab659 Mon Sep 17 00:00:00 2001 From: Serhiy Barhamon Date: Tue, 23 Jul 2024 22:32:15 +0200 Subject: [PATCH] Test refactoring, readme update, Base64->DataUrl rename --- readme.md | 30 ++++++++++++++++++++++++++++++ src/tests/mod.rs | 14 +++++++------- src/upload/mod.rs | 4 ++-- 3 files changed, 39 insertions(+), 9 deletions(-) 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 cd5c118..1ba0bb1 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -11,11 +11,11 @@ use crate::{ fn env() -> (String, String, String) { dotenv().ok(); - let api_secret = var("CLOUDINARY_API_SECRET").expect("environment 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_secret, api_key, cloud_name) + (api_key, cloud_name, api_secret) } #[tokio::test] @@ -26,10 +26,10 @@ async fn test_image_upload_from_base64() { let public_id = "image_upload_from_base64"; 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::Base64(String::from(image_base64)), &options) + .image(Source::DataUrl(image_base64.into()), &options) .await .unwrap(); @@ -47,7 +47,7 @@ async fn test_image_upload_from_url() { 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) @@ -68,10 +68,10 @@ async fn test_image_upload_from_path() { 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 e566e5d..b1a0327 100644 --- a/src/upload/mod.rs +++ b/src/upload/mod.rs @@ -38,7 +38,7 @@ pub struct Upload { pub enum Source { Path(PathBuf), Url(Url), - Base64(String), + DataUrl(String), } impl Upload { @@ -62,7 +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::Base64(base64) => Part::text(base64), + Source::DataUrl(base64) => Part::text(base64), }; let multipart = self.build_form_data(options).part("file", file); let url = format!(