Skip to content

Commit

Permalink
Test refactoring, readme update, Base64->DataUrl rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurk committed Jul 23, 2024
1 parent eaa06d5 commit 2c308ed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,43 @@ 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());
let upload = Upload::new("api_key".to_string(), "cloud_name".to_string(), "api_secret".to_string() );
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:
Expand Down
14 changes: 7 additions & 7 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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();

Expand All @@ -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)
Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/upload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Upload {
pub enum Source {
Path(PathBuf),
Url(Url),
Base64(String),
DataUrl(String),
}

impl Upload {
Expand All @@ -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!(
Expand Down

0 comments on commit 2c308ed

Please sign in to comment.