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
use crate::convert::Convert;
use crate::error::TinifyError;
use crate::error::Upstream;
use crate::resize::Resize;
use crate::transform::Transform;
use crate::Operations;
use crate::SourceUrl;
use crate::API_ENDPOINT;
use reqwest::blocking::Client as ReqwestClient;
use reqwest::header::HeaderValue;
use reqwest::header::CONTENT_TYPE;
use reqwest::StatusCode;
use serde_json::json;
use serde_json::Value;
use std::fs::File;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::str;
use std::time::Duration;
use url::Url;

#[derive(Debug)]
pub struct Source {
  key: Option<String>,
  buffer: Option<Vec<u8>>,
  output: Option<String>,
  reqwest_client: ReqwestClient,
  operations: Operations,
}

impl Source {
  pub(crate) fn new(key: Option<&str>) -> Self {
    let key = key.map(|val| val.into());
    let reqwest_client = ReqwestClient::new();
    let operations = Operations {
      convert: None,
      resize: None,
      transform: None,
    };

    Self {
      key,
      buffer: None,
      output: None,
      reqwest_client,
      operations,
    }
  }

  fn get_source_from_response(
    mut self,
    buffer: Option<&[u8]>,
    json: Option<Value>,
  ) -> Result<Self, TinifyError> {
    let parse = Url::parse(API_ENDPOINT)?;
    let url = parse.join("/shrink")?;
    let compressed_image = if let Some(json) = json {
      self
        .reqwest_client
        .post(url)
        .header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
        .body(json.to_string())
        .basic_auth("api", self.key.as_ref())
        .timeout(Duration::from_secs(300))
        .send()?
    } else {
      self
        .reqwest_client
        .post(url)
        .body(buffer.unwrap().to_vec())
        .basic_auth("api", self.key.as_ref())
        .timeout(Duration::from_secs(300))
        .send()?
    };

    match compressed_image.status() {
      StatusCode::CREATED => {
        if let Some(location) = compressed_image.headers().get("location") {
          let location = location.to_str()?.to_string();
          let bytes = self
            .reqwest_client
            .get(&location)
            .timeout(Duration::from_secs(300))
            .send()?
            .bytes()?
            .to_vec();

          self.buffer = Some(bytes);
          self.output = Some(location);

          Ok(self)
        } else {
          let upstream = Upstream {
            error: "Empty".to_string(),
            message: "The location of the compressed image is empty."
              .to_string(),
          };
          Err(TinifyError::ServerError { upstream })
        }
      }
      StatusCode::UNAUTHORIZED | StatusCode::UNSUPPORTED_MEDIA_TYPE => {
        let upstream: Upstream =
          serde_json::from_str(&compressed_image.text()?)?;
        Err(TinifyError::ClientError { upstream })
      }
      StatusCode::SERVICE_UNAVAILABLE => {
        let upstream: Upstream =
          serde_json::from_str(&compressed_image.text()?)?;
        Err(TinifyError::ServerError { upstream })
      }
      _ => unreachable!(),
    }
  }

  #[allow(clippy::wrong_self_convention)]
  pub(crate) fn from_buffer(self, buffer: &[u8]) -> Result<Self, TinifyError> {
    self.get_source_from_response(Some(buffer), None)
  }

  #[allow(clippy::wrong_self_convention)]
  pub(crate) fn from_file<P>(self, path: P) -> Result<Self, TinifyError>
  where
    P: AsRef<Path>,
  {
    let file = File::open(path)?;
    let mut reader = BufReader::new(file);
    let mut buffer = Vec::with_capacity(reader.capacity());
    reader.read_to_end(&mut buffer)?;

    self.get_source_from_response(Some(&buffer), None)
  }

  #[allow(clippy::wrong_self_convention)]
  pub(crate) fn from_url<P>(self, path: P) -> Result<Self, TinifyError>
  where
    P: AsRef<str> + Into<String>,
  {
    let json = json!({
      "source": SourceUrl { url: path.into() },
    });

    self.get_source_from_response(None, Some(json))
  }

  /// Resize the current compressed image.
  pub fn resize(mut self, resize: Resize) -> Result<Self, TinifyError> {
    self.operations.resize = Some(resize);
    Ok(self)
  }

  /// Convert the current compressed image.
  pub fn convert(mut self, convert: Convert) -> Result<Self, TinifyError> {
    self.operations.convert = Some(convert);
    Ok(self)
  }

  /// Transform the current compressed image.
  pub fn transform(
    mut self,
    transform: Transform,
  ) -> Result<Self, TinifyError> {
    self.operations.transform = Some(transform);
    Ok(self)
  }

  fn run_operations(&mut self) -> Result<(), TinifyError> {
    let operations = serde_json::to_string(&self.operations)?;

    if let Some(output) = self.output.take() {
      let response = self
        .reqwest_client
        .post(output)
        .header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
        .body(operations)
        .basic_auth("api", self.key.as_ref())
        .timeout(Duration::from_secs(300))
        .send()?;

      match response.status() {
        StatusCode::OK => {
          let bytes = response.bytes()?.to_vec();

          self.buffer = Some(bytes);

          Ok(())
        }
        StatusCode::BAD_REQUEST
        | StatusCode::UNAUTHORIZED
        | StatusCode::UNSUPPORTED_MEDIA_TYPE => {
          let upstream: Upstream = serde_json::from_str(&response.text()?)?;
          Err(TinifyError::ClientError { upstream })
        }
        StatusCode::SERVICE_UNAVAILABLE => {
          let upstream: Upstream = serde_json::from_str(&response.text()?)?;
          Err(TinifyError::ServerError { upstream })
        }
        _ => unreachable!(),
      }
    } else {
      let upstream = Upstream {
        error: "Empty".to_string(),
        message: "Output of the compressed image is empty.".to_string(),
      };
      Err(TinifyError::ClientError { upstream })
    }
  }
  
  /// Save the current compressed image to a file.
  pub fn to_file<P>(&mut self, path: P) -> Result<(), TinifyError>
  where
    P: AsRef<Path>,
  {
    if self.operations.convert.is_some()
      || self.operations.resize.is_some()
      || self.operations.transform.is_some()
    {
      self.run_operations()?;
    }

    if let Some(ref buffer) = self.buffer {
      let file = File::create(path)?;
      let mut reader = BufWriter::new(file);
      reader.write_all(buffer)?;
      reader.flush()?;
    }

    Ok(())
  }

  /// Save the current compressed image to a buffer.
  pub fn to_buffer(&mut self) -> Result<Vec<u8>, TinifyError> {
    if self.operations.convert.is_some()
      || self.operations.resize.is_some()
      || self.operations.transform.is_some()
    {
      self.run_operations()?;
    }

    if let Some(buffer) = self.buffer.as_ref() {
      Ok(buffer.to_vec())
    } else {
      let upstream = Upstream {
        error: "Empty".to_string(),
        message: "Buffer of the compressed image is empty.".to_string(),
      };
      Err(TinifyError::ClientError { upstream })
    }
  }
}