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
use std::{fs::{self, File}, io::Read, path::PathBuf, str::FromStr};

use base64::{engine::general_purpose::STANDARD, Engine};
use getset::{Getters, Setters};
use oxipng::Options;

use crate::dossier;

use super::ResourceError;


/// Image resource to manipulate images
#[derive(Debug, Getters, Setters)]
pub struct ImageResource {

    #[getset(get = "pub", set = "pub")]
    src: PathBuf,

    #[getset(get = "pub", set = "pub")]
    mime_type: Option<String>,

    #[getset(get = "pub", set = "pub")]
    caption: Option<String>,

    #[getset(get = "pub", set = "pub")]
    label: Option<String>,
}

impl ImageResource {

    pub fn new(src: PathBuf, caption: Option<String>, label: Option<String>) -> Self {

        Self {
            src,
            mime_type: None,
            caption,
            label
        }
    }

    /// Infer mime type from image path using `infer` lib.
    /// `text/xml` is replaced by `image/svg+xml`
    pub fn inferring_mime_type(mut self) -> Result<Self, ResourceError> {

        let mime_type = infer::get_from_path(&self.src)?;

        if let Some(t) = mime_type {

            let mut mime_type = t.mime_type().to_string();

            // work-around svg+xml
            if mime_type.contains("text/xml") {
                mime_type = String::from("image/svg+xml");
            }

            self.set_mime_type(Some(mime_type));

            return Ok(self);

        } else {
            return Err(ResourceError::InvalidResourceVerbose(format!("image {:?} mime type not found", self.src)));
        }
    }

    /// Elaborate `src` path if it is relative appending if it doesn't exist dossier `assets` directory
    pub fn elaborating_relative_path_as_dossier_assets(mut self, base_location: &PathBuf) -> Self {

        let mut base_location: PathBuf = base_location.clone();

        if !base_location.is_dir() {
            base_location = PathBuf::from(base_location.parent().unwrap());
        }

        if self.src().is_relative() {

            self.set_src(base_location.join(self.src()));

            if !self.src().exists() {

                log::debug!("{:?} not found, try adding images directory path", self.src());

                let image_file_name = self.src().file_name().unwrap();

                self.set_src(base_location.join(dossier::ASSETS_DIR).join(dossier::IMAGES_DIR).join(image_file_name));

                if !self.src().exists() {
                    if let Ok(src) = fs::canonicalize(self.src().clone()) {
                        self.set_src(src);
                    }
                }
            }
        }

        self
    }

    /// Encode image in base64
    pub fn to_base64(&self, compression: bool) -> Result<String, ResourceError> {

        let buffer = self.to_vec_u8()?;

        if compression {

            let original_log_level = log::max_level();
            log::set_max_level(log::LevelFilter::Warn);

            let options = Options::max_compression();

            let optimized_png = oxipng::optimize_from_memory(&buffer, &options);

            log::set_max_level(original_log_level);
    
            match optimized_png {
                Ok(image) => return Ok(STANDARD.encode(image)),
                Err(err) => return Err(ResourceError::ElaborationError(format!("image compression error: {}", err)))
            }

        } else {

            Ok(STANDARD.encode(buffer))
        }
    }

    pub fn to_vec_u8(&self) -> Result<Vec<u8>, ResourceError> {

        let mut image_file = File::open(self.src.clone())?;
        let mut raw_image: Vec<u8> = Vec::new();
        image_file.read_to_end(&mut raw_image)?;

        Ok(raw_image)
    }
}

impl FromStr for ImageResource {
    type Err = ResourceError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::new(PathBuf::from(s), None, None).inferring_mime_type()?)
    }
}

#[cfg(test)]
mod test {

}