cloudinary 0.8.2

A Rust library for the Cloudinary API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
pub mod aspect_ratio;
pub mod background;
pub mod crop_mode;
pub mod gravity;
pub mod named_color;
pub mod pad_mode;
pub mod resize_mode;

use std::{
    cell::RefCell,
    fmt::{Display, Formatter},
    sync::Arc,
};

use url::Url;

pub use self::{crop_mode::CropMode, pad_mode::PadMode, resize_mode::ResizeMode};

#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum Transformations {
    /// These modes adjust the size of the delivered image without cropping out any elements of the original image.
    Resize(ResizeMode),
    Crop(CropMode),
    Pad(PadMode),
}

impl Display for Transformations {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Transformations::Resize(resize_mode) => write!(f, "{}", resize_mode),
            Transformations::Crop(crop_mode) => write!(f, "{}", crop_mode),
            Transformations::Pad(pad_mode) => write!(f, "{}", pad_mode),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Image {
    cloud_name: Arc<str>,
    public_id: Arc<str>,
    format: Option<Arc<str>>,
    transformations: RefCell<Vec<Transformations>>,
}

impl Image {
    pub fn new(cloud_name: Arc<str>, public_id: Arc<str>) -> Self {
        Image {
            cloud_name,
            public_id,
            format: None,
            transformations: RefCell::new(Vec::new()),
        }
    }

    pub fn set_format(&mut self, format: &str) {
        self.format = Some(format.into());
    }

    pub fn get_format(&self) -> Option<Arc<str>> {
        self.format.clone()
    }

    pub fn add_transformation(self, transformation: Transformations) -> Self {
        self.transformations.borrow_mut().push(transformation);
        self
    }

    /// Build a URL
    ///
    /// # Example:
    /// ```rust
    /// use cloudinary::transformation::Image;
    /// let mut image = Image::new("cloud_name".into(), "public_id".into());
    /// image.set_format("jpg");
    /// assert_eq!(image.build().as_str(), "https://res.cloudinary.com/cloud_name/image/upload/public_id.jpg");
    /// ```
    pub fn build(&self) -> Url {
        let mut url = Url::parse("https://res.cloudinary.com").unwrap();
        let transformations = self
            .transformations
            .borrow()
            .iter()
            .map(|t| t.to_string())
            .collect::<Vec<String>>()
            .join("/");
        let path = format!(
            "{}/image/upload/{}{}",
            self.cloud_name,
            if transformations.is_empty() {
                "".into()
            } else {
                format!("{}/", transformations)
            },
            self.public_id
        );

        match self.get_format() {
            Some(format) => {
                let file_name = self.public_id.split('/').next_back().unwrap().to_string();

                let new_file_name = format!("{}.{}", file_name, format);
                url.set_path(
                    path.replace(file_name.as_str(), new_file_name.as_str())
                        .as_str(),
                );
            }
            None => {
                url.set_path(path.as_str());
            }
        }

        url
    }
}

impl From<Image> for Url {
    fn from(image: Image) -> Self {
        image.build()
    }
}

impl Display for Image {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.build())
    }
}

/// Check if the string is a transformation
/// A transformation is a string that has underscore and the length of the head is less than 4
/// https://support.cloudinary.com/hc/en-us/community/posts/4414437232018-Invalid-transformation-parameter
fn is_transformation(s: &str) -> bool {
    if let Some((head, _)) = s.split_once('_') {
        return head.len() < 4;
    }
    false
}

/// Check if the string is a version where version is a string that starts with 'v' and the rest of the string a Unix
/// timestamp
/// https://support.cloudinary.com/hc/en-us/articles/202520912-What-are-image-versions-
fn is_version(s: &str) -> bool {
    s.starts_with('v') && s.len() == 11 && s[1..].chars().all(|c| c.is_ascii_digit())
}

/// Parse a URL to an Image
/// Unofficial. Can break at any time.
/// Official recommendation is to use public_id that you get after uploading an image to Cloudinary.
impl TryFrom<Url> for Image {
    type Error = &'static str;

    fn try_from(url: Url) -> Result<Self, Self::Error> {
        if url.host_str().unwrap() != "res.cloudinary.com" {
            return Err("Not a cloudinary url");
        }

        let mut cloud_name: Option<&str> = None;
        let mut public_id_parts: Vec<(&str, Option<&str>)> = Vec::new();
        let mut public_id_territory = false;
        for (pos, s) in url.path_segments().unwrap().enumerate() {
            match pos {
                0 => {
                    cloud_name = Some(s);
                }
                1 => {
                    if s != "image" {
                        return Err("Only image is supported");
                    }
                }
                2 => {
                    if ![
                        "upload",
                        "fetch",
                        "private",
                        "authenticated",
                        "sprite",
                        "facebook",
                        "twitter",
                        "youtube",
                        "vimeo",
                    ]
                    .contains(&s)
                    {
                        return Err("Invalid mode");
                    }
                }
                _ => {
                    if !public_id_territory && is_version(s) {
                        public_id_territory = true;
                    } else if !public_id_territory && is_transformation(s) {
                    } else if let Some((head, tail)) = s.rsplit_once('.') {
                        public_id_territory = true;
                        public_id_parts.push((head, Some(tail)));
                    } else {
                        public_id_territory = true;
                        public_id_parts.push((s, None));
                    }
                }
            }
        }

        let cloud_name = cloud_name.ok_or("No cloud_name is found")?;
        let last = public_id_parts.pop().ok_or("no public_id is found")?;
        let mut public_id = public_id_parts
            .iter()
            .map(|(head, tail)| {
                if let Some(tail) = tail {
                    format!("{}.{}/", head, tail)
                } else {
                    format!("{}/", head)
                }
            })
            .collect::<Vec<String>>()
            .join("");

        public_id.push_str(last.0);
        let format = last.1;

        let mut image = Image::new(cloud_name.into(), public_id.into());
        if let Some(extension) = format {
            image.set_format(extension);
        }

        Ok(image)
    }
}

#[cfg(test)]
mod tests {
    use crate::transformation::aspect_ratio::AspectRatio;

    use super::*;

    #[test]
    fn from_url_with_transformations() {
        let image: Image = Url::parse("https://res.cloudinary.com/i/image/upload/c_scale,h_800,q_auto/v1233456678/path/name.jpg")
            .ok().unwrap().try_into().ok().unwrap();

        assert_eq!(image.cloud_name, "i".into());
        assert_eq!(image.public_id, "path/name".into());
        assert_eq!(image.get_format(), Some("jpg".into()));
    }

    #[test]
    fn from_url_without_transformations() {
        let image: Image = Url::parse(
            "https://res.cloudinary.com/cloud/image/upload/v1233456678/path/with%20space/name.png",
        )
        .unwrap()
        .try_into()
        .unwrap();

        assert_eq!(image.cloud_name, "cloud".into());
        assert_eq!(image.public_id, "path/with%20space/name".into());
        assert_eq!(image.get_format(), Some("png".into()));
    }

    #[test]
    fn from_url_without_version() {
        let image: Image =
            Url::parse("https://res.cloudinary.com/test/image/upload/path/with%20space/name.png")
                .unwrap()
                .try_into()
                .unwrap();

        assert_eq!(image.cloud_name, "test".into());
        assert_eq!(image.public_id, "path/with%20space/name".into());
        assert_eq!(image.get_format(), Some("png".into()));
    }

    #[test]
    fn set_format() {
        let mut image = Image::new("test".into(), "path/name".into());
        image.set_format("png");
        assert_eq!(
            image.build().as_str(),
            "https://res.cloudinary.com/test/image/upload/path/name.png"
        );
    }

    #[test]
    fn replace_format() {
        let mut image: Image = Url::parse(
            "https://res.cloudinary.com/i/image/upload/c_scale,h_800,q_auto/path/name.jpg",
        )
        .unwrap()
        .try_into()
        .unwrap();

        image.set_format("png");
        assert_eq!(
            image.build().as_str(),
            "https://res.cloudinary.com/i/image/upload/path/name.png"
        );
    }

    #[test]
    fn add_scale() {
        let image = Image::new("test".into(), "path/name".into()).add_transformation(
            Transformations::Resize(ResizeMode::Scale {
                width: 100,
                height: 100,
                liquid: None,
            }),
        );
        assert_eq!(
            image.to_string(),
            "https://res.cloudinary.com/test/image/upload/c_scale,w_100,h_100/path/name"
        );
    }

    #[test]
    fn add_scale_by_width() {
        let image = Image::new("test".into(), "path/name".into()).add_transformation(
            Transformations::Resize(ResizeMode::ScaleByWidth {
                width: 100,
                ar: None,
                liquid: None,
            }),
        );
        assert_eq!(
            image.to_string(),
            "https://res.cloudinary.com/test/image/upload/c_scale,w_100/path/name"
        );
    }

    #[test]
    fn add_scale_by_height() {
        let image_url: Url = Image::new("test".into(), "path/name".into())
            .add_transformation(Transformations::Resize(ResizeMode::ScaleByHeight {
                height: 100,
                ar: None,
                liquid: None,
            }))
            .into();
        assert_eq!(
            image_url.as_str(),
            "https://res.cloudinary.com/test/image/upload/c_scale,h_100/path/name"
        );
    }

    #[test]
    fn add_scale_by_width_with_aspect_ratio() {
        let image = Image::new("test".into(), "path/name".into()).add_transformation(
            Transformations::Resize(ResizeMode::ScaleByWidth {
                width: 100,
                ar: Some(AspectRatio::Sides(16, 9)),
                liquid: None,
            }),
        );
        assert_eq!(
            image.to_string(),
            "https://res.cloudinary.com/test/image/upload/ar_16:9,c_scale,w_100/path/name"
        );
    }

    #[test]
    fn add_scale_by_height_with_aspect_ratio() {
        let image = Image::new("test".into(), "path/name".into()).add_transformation(
            Transformations::Resize(ResizeMode::ScaleByHeight {
                height: 100,
                ar: Some(AspectRatio::Result(0.5)),
                liquid: None,
            }),
        );
        assert_eq!(
            image.to_string(),
            "https://res.cloudinary.com/test/image/upload/ar_0.5,c_scale,h_100/path/name"
        );
    }

    #[test]
    fn add_scale_by_width_with_aspect_ratio_and_liquid() {
        let image_url: Url = Image::new("test".into(), "path/name".into())
            .add_transformation(Transformations::Resize(ResizeMode::ScaleByWidth {
                width: 100,
                ar: Some(AspectRatio::Sides(16, 9)),
                liquid: Some(()),
            }))
            .into();
        assert_eq!(
            image_url.as_str(),
            "https://res.cloudinary.com/test/image/upload/ar_16:9,c_scale,w_100,g_liquid/path/name"
        );
    }

    #[test]
    fn scale_ignore_aspect_ratio() {
        let image_url: Url = Image::new("test".into(), "path/name".into())
            .add_transformation(Transformations::Resize(ResizeMode::ScaleByWidth {
                width: 100,
                ar: Some(AspectRatio::Ignore),
                liquid: None,
            }))
            .into();
        assert_eq!(
            image_url.as_str(),
            "https://res.cloudinary.com/test/image/upload/fl_ignore_aspect_ratio,c_scale,w_100/path/name"
        );
    }

    #[test]
    fn from_url_with_dots_in_transformation() {
        let image: Image =
            Url::parse("https://res.cloudinary.com/test/image/upload/ar_0.5,foo/1.jpg")
                .unwrap()
                .try_into()
                .unwrap();

        assert_eq!(image.cloud_name, "test".into());
        assert_eq!(image.public_id, "1".into());
        assert_eq!(image.get_format(), Some("jpg".into()));
    }

    #[test]
    fn from_url_with_dots_in_public_id() {
        let image: Image =
            Url::parse("https://res.cloudinary.com/test/image/upload/ar_0.5,foo/1.2.jpg")
                .unwrap()
                .try_into()
                .unwrap();

        assert_eq!(image.cloud_name, "test".into());
        assert_eq!(image.public_id, "1.2".into());
        assert_eq!(image.get_format(), Some("jpg".into()));
        assert_eq!(
            image.to_string(),
            "https://res.cloudinary.com/test/image/upload/1.2.jpg"
        )
    }

    #[test]
    fn from_url_without_extension() {
        let image: Image =
            Url::parse("https://res.cloudinary.com/test/image/upload/ar_0.5,foo/v1640995200/1")
                .unwrap()
                .try_into()
                .unwrap();

        assert_eq!(image.cloud_name, "test".into());
        assert_eq!(image.public_id, "1".into());
        assert_eq!(image.get_format(), None);
    }

    #[test]
    fn pad_mode() {
        let image_url: Url = Image::new("test".into(), "path/name".into())
            .add_transformation(Transformations::Pad(PadMode::PadByWidth {
                width: 100,
                ar: None,
                background: None,
                gravity: None,
            }))
            .into();
        assert_eq!(
            image_url.as_str(),
            "https://res.cloudinary.com/test/image/upload/c_pad,w_100/path/name"
        );
    }
}