supabase_storage/build/object/
render.rs

1use crate::{
2    build::{builder::Builder, executor::Executor},
3    model::options::Transform,
4};
5
6impl Builder {
7    /// get public object from the storage
8    ///
9    /// # Arguments
10    ///
11    /// * `bucket_id` - bucket id
12    /// * `object` - object name/path
13    /// * `transform` - tranformation options to transform before serving it to client
14    ///
15    /// # Returns
16    ///
17    /// * `Executor` - The constructed `Executor` instance for executing the request.
18    ///
19    /// # Example
20    /// ```
21    /// use supabase_storage::{
22    ///     Storage,
23    ///     config::SupabaseConfig,
24    ///     model::bucket::NewBucket,
25    ///     model::options::{Transform, Format, Resize}
26    /// };
27    /// use dotenv::dotenv;
28    ///
29    /// #[tokio::main]
30    /// async fn main() {
31    ///     dotenv().ok();
32    ///     let config = SupabaseConfig::default();
33    ///     let response = Storage::new_with_config(config)
34    ///         .from()
35    ///         .get_object_with_transform("thefux", "test.png", Transform {
36    ///             format: Some(Format::Origin),
37    ///             height: Some(0),
38    ///             quality: Some(0),
39    ///             resize: Some(Resize::Cover),
40    ///             width: Some(0),
41    ///         })
42    ///         .execute()
43    ///         .await
44    ///         .unwrap();
45    /// }
46    /// ```
47    pub fn get_object_with_transform(
48        mut self,
49        bucket_id: &str,
50        object: &str,
51        transform: Transform,
52    ) -> Executor {
53        self.url
54            .path_segments_mut()
55            .unwrap()
56            .push("render")
57            .push("image")
58            .push("authenticated")
59            .push(bucket_id)
60            .push(object);
61
62        self.url
63            .set_query(Some(&serde_qs::to_string(&transform).unwrap()));
64
65        self.create_executor()
66    }
67}
68
69#[cfg(test)]
70mod test {
71    use reqwest::{header::HeaderMap, Client, Method};
72    use url::{Host, Origin};
73
74    use crate::{
75        build::builder::BodyType,
76        model::options::{Format, Resize},
77    };
78
79    use super::*;
80
81    #[test]
82    fn test_get_object_with_transform() {
83        let executor = Builder::new(
84            url::Url::parse("http://localhost").unwrap(),
85            HeaderMap::new(),
86            Client::new(),
87        )
88        .get_object_with_transform(
89            "thefux",
90            "test.png",
91            Transform {
92                format: Some(Format::Origin),
93                height: Some(0),
94                quality: Some(0),
95                resize: Some(Resize::Cover),
96                width: Some(0),
97            },
98        );
99
100        if let Some(typ) = executor.builder.body {
101            match typ {
102                BodyType::StringBody(val) => assert_eq!(
103                    val,
104                    r#"
105                {
106                    "format":"origin",
107                    "height":0,
108                    "quality":"cover",
109                    "resize":0,
110                    "width":0,
111                }"#
112                ),
113                _ => panic!("nop"),
114            }
115        }
116
117        assert_eq!(executor.builder.method, Method::GET);
118        assert_eq!(
119            executor.builder.url.origin(),
120            Origin::Tuple("http".into(), Host::Domain("localhost".into()), 80)
121        );
122        assert_eq!(
123            executor.builder.url.path(),
124            "/render/image/authenticated/thefux/test.png"
125        );
126    }
127}