Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add source from base64 #31

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 35 additions & 13 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();

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

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