Skip to content

Commit b999819

Browse files
authored
Added support for image transformation
Added support for image transformation in create_signed_url
2 parents 1e365c7 + 7ebfc45 commit b999819

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "supabase-storage-rs"
33
authors = ["Eric Biggs"]
44
description = "Supabase storage implementation following the official client libraries."
55
readme = "README.md"
6-
version = "0.1.7"
6+
version = "0.1.8"
77
edition = "2021"
88
license = "MIT OR Apache-2.0"
99
keywords = ["supabase", "supabase-storage", "storage"]
@@ -16,7 +16,9 @@ default = ["reqwest/default-tls"]
1616
use-rustls = ["reqwest/rustls-tls"]
1717

1818
[dependencies]
19-
reqwest = { version = "0.12.9", default-features = false, features = ["multipart"] }
19+
reqwest = { version = "0.12.9", default-features = false, features = [
20+
"multipart",
21+
] }
2022
serde = { version = "1.0.210", features = ["derive"] }
2123
serde_json = "1.0.128"
2224
thiserror = "2.0.3"

src/client.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ impl StorageClient {
702702
bucket_id: &str,
703703
path: &str,
704704
expires_in: u64,
705+
options: Option<DownloadOptions<'_>>,
705706
) -> Result<String, Error> {
706707
let mut headers = self.headers.clone();
707708
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
@@ -712,7 +713,10 @@ impl StorageClient {
712713
);
713714
}
714715

715-
let payload = CreateSignedUrlPayload { expires_in };
716+
let payload = CreateSignedUrlPayload {
717+
expires_in,
718+
transform: options.and_then(|opts| opts.transform),
719+
};
716720

717721
let body = serde_json::to_string(&payload)?;
718722

@@ -736,7 +740,10 @@ impl StorageClient {
736740
message: res_body,
737741
})?;
738742

739-
Ok(signed_url_response.signed_url)
743+
Ok(format!(
744+
"{}{}/{}",
745+
self.project_url, STORAGE_V1, signed_url_response.signed_url
746+
))
740747
}
741748

742749
/// Create multiple signed download urls, returns a `Vec` of signed_urls on success

src/models.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,14 @@ pub enum Column {
127127
}
128128

129129
// TODO: Forgot to add transform
130-
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
131-
pub(crate) struct CreateSignedUrlPayload {
130+
#[derive(Default, Debug, Clone, Serialize, PartialEq)]
131+
pub(crate) struct CreateSignedUrlPayload<'a> {
132132
#[serde(rename = "expiresIn")]
133133
/// The number of seconds until the signed URL expires
134134
/// After this duration, the URL will no longer grant access to the object
135135
pub(crate) expires_in: u64,
136+
#[serde(skip_serializing_if = "Option::is_none")]
137+
pub(crate) transform: Option<TransformOptions<'a>>,
136138
}
137139

138140
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
@@ -272,22 +274,27 @@ pub struct DownloadOptions<'a> {
272274
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
273275
pub struct TransformOptions<'a> {
274276
/// The width of the image in pixels
277+
#[serde(skip_serializing_if = "Option::is_none")]
275278
pub width: Option<u64>,
276279
/// The height of the image in pixels
280+
#[serde(skip_serializing_if = "Option::is_none")]
277281
pub height: Option<u64>,
278282
/// The resize mode can be cover, contain or fill. Defaults to cover.
279283
/// Cover resizes the image to maintain it's aspect ratio while filling the entire width and height.
280284
/// Contain resizes the image to maintain it's aspect ratio while fitting the entire image within the width and height.
281285
/// Fill resizes the image to fill the entire width and height. If the object's aspect ratio does not match the width and height, the image will be stretched to fit.
286+
#[serde(skip_serializing_if = "Option::is_none")]
282287
pub resize: Option<&'a str>,
283288
/// Specify the format of the image requested.
284289
///
285290
/// When using 'origin' we force the format to be the same as the original image.
286291
/// When this option is not passed in, images are optimized to modern image formats like Webp.
292+
#[serde(skip_serializing_if = "Option::is_none")]
287293
pub format: Option<&'a str>,
288294
/// Sets the quality of the returned image
289295
///
290296
/// A number from 20 to 100, with 100 being the highest quality. Defaults to 80
297+
#[serde(skip_serializing_if = "Option::is_none")]
291298
pub quality: Option<u8>,
292299
}
293300

tests/client_tests.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use supabase_storage_rs::models::{
2-
Column, FileSearchOptions, MimeType, Order, SortBy, StorageClient,
2+
Column, DownloadOptions, FileSearchOptions, MimeType, Order, SortBy, StorageClient,
3+
TransformOptions,
34
};
45
use uuid::Uuid;
56

@@ -354,10 +355,34 @@ async fn test_copy_file() {
354355
async fn test_create_signed_url() {
355356
let client = create_test_client().await;
356357

357-
client
358-
.create_signed_url("list_files", "3.txt", 12431234)
358+
let signed_url = client
359+
.create_signed_url("list_files", "1.txt", 2000, None)
359360
.await
360361
.unwrap();
362+
363+
assert!(signed_url.contains(&format!("/object/sign/{}/{}", "list_files", "1.txt")));
364+
}
365+
366+
#[tokio::test]
367+
async fn test_create_signed_url_with_transform() {
368+
let client = create_test_client().await;
369+
370+
let download_options: Option<DownloadOptions> = None;
371+
// #commented out due to requiring a Pro plan for transformations
372+
// download_options = Some(DownloadOptions {
373+
// transform: Some(TransformOptions {
374+
// width: Some(100),
375+
// height: Some(100),
376+
// resize: None,
377+
// format: None,
378+
// quality: None,
379+
// }),
380+
// download: Some(false),
381+
// });
382+
client
383+
.create_signed_url("list_files", "/folder/aaa.jpg", 2000, download_options)
384+
.await
385+
.expect("expected signed url to be created");
361386
}
362387

363388
#[tokio::test]

0 commit comments

Comments
 (0)