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
use crate::resize;
use crate::convert;
use crate::convert::Color;
use crate::convert::Convert;
use crate::convert::Transform;
use crate::error::TinifyError;
use reqwest::blocking::Client as ReqwestClient;
use reqwest::blocking::Response;
use reqwest::header::HeaderValue;
use reqwest::header::CONTENT_TYPE;
use reqwest::StatusCode;
use reqwest::Method;
use std::time::Duration;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::fs::File;
use std::str;

const API_ENDPOINT: &str = "https://api.tinify.com";

#[derive(Debug)]
pub struct Source {
  url: Option<String>,
  key: Option<String>,
  buffer: Option<Vec<u8>>,
}

impl Source {
  pub(crate) fn new(
    url: Option<&str>,
    key: Option<&str>,
  ) -> Self {
    let url = url.map(|val| val.into());
    let key = key.map(|val| val.into());

    Self {
      url,
      key,
      buffer: None,
    }
  }

  fn request<U>(
    &self,
    method: Method,
    url: U,
    buffer: Option<&[u8]>,
  ) -> Result<Response, TinifyError>
  where
    U: AsRef<str>,
  {
    let full_url =
      format!("{}{}", API_ENDPOINT, url.as_ref());
    let reqwest_client = ReqwestClient::new();
    let response = match method {
      Method::POST => {
        reqwest_client
          .post(full_url)
          .body(buffer.unwrap().to_owned())
          .basic_auth("api", self.key.as_ref())
          .timeout(Duration::from_secs(300))
          .send()?
      },
      Method::GET => {
        reqwest_client
          .get(url.as_ref())
          .timeout(Duration::from_secs(300))
          .send()?
      },
      _ => unreachable!(),
    };

    match response.status() {
      StatusCode::UNAUTHORIZED => {
        return Err(TinifyError::ClientError);
      },
      StatusCode::UNSUPPORTED_MEDIA_TYPE => {
        return Err(TinifyError::ClientError);
      },
      StatusCode::SERVICE_UNAVAILABLE => {
        return Err(TinifyError::ServerError);
      },
      _  => {},
    };

    Ok(response)
  }

  pub(crate) fn from_file<P>(
    self,
    path: P,
  ) -> Result<Self, TinifyError>
  where
    P: AsRef<Path>,
  {
    let file = File::open(path)
      .map_err(|source| TinifyError::ReadError { source })?;
    let mut reader = BufReader::new(file);
    let mut buffer = Vec::with_capacity(reader.capacity());
    reader.read_to_end(&mut buffer)?;

    self.from_buffer(&buffer)
  }

  pub(crate) fn from_buffer(
    self,
    buffer: &[u8],
  ) -> Result<Self, TinifyError> {
    let response =
      self.request(Method::POST, "/shrink", Some(buffer))?;

    self.get_source_from_response(response)
  }

  pub(crate) fn from_url<U>(
    self,
    url: U,
  ) -> Result<Self, TinifyError>
  where
    U: AsRef<str>,
  {
    let get_request = self.request(Method::GET, url, None);
    let buffer = get_request?.bytes()?;
    let post_request =
      self.request(Method::POST, "/shrink", Some(&buffer))?;

    self.get_source_from_response(post_request)
  }
  
  /// Resize the current compressed image.
  ///
  /// # Examples
  ///
  /// ```
  /// use tinify::Tinify;
  /// use tinify::Client;
  /// use tinify::TinifyError;
  /// use tinify::resize::Method;
  /// use tinify::resize::Resize;
  /// 
  /// fn get_client() -> Result<Client, TinifyError> {
  ///   let key = "tinify api key";
  ///   let tinify = Tinify::new();
  ///
  ///   tinify
  ///     .set_key(key)
  ///     .get_client()
  /// }
  /// 
  /// fn main() -> Result<(), TinifyError> {
  ///   let client = get_client()?;
  ///   let _ = client
  ///     .from_file("./unoptimized.jpg")?
  ///     .resize(Resize::new(
  ///       Method::FIT,
  ///       Some(400),
  ///       Some(200)),
  ///     )?
  ///     .to_file("./resized.jpg")?;
  ///
  ///   Ok(())
  /// }
  /// ```
  pub fn resize(
    self,
    resize: resize::Resize,
  ) -> Result<Self, TinifyError> {
    let json_data = resize::JsonData::new(resize);
    let mut json_string =
      serde_json::to_string(&json_data).unwrap();
    let width = json_data.resize.width;
    let height = json_data.resize.height;
    json_string = match (
      (width.is_some(), height.is_none()),
      (height.is_some(), width.is_none()),
    ) {
      ((true, true), (_, _)) =>
        json_string.replace(",\"height\":null", ""),
      ((_, _), (true, true)) =>
        json_string.replace(",\"width\":null", ""),
      _ => json_string,
    };
    let reqwest_client = ReqwestClient::new();
    let response = reqwest_client
      .post(self.url.as_ref().unwrap())
      .header(
        CONTENT_TYPE,
        HeaderValue::from_static("application/json"),
      )
      .body(json_string)
      .basic_auth("api", self.key.as_ref())
      .timeout(Duration::from_secs(300))
      .send()?;

    if response.status() == StatusCode::BAD_REQUEST {
      return Err(TinifyError::ClientError);
    }
    
    self.get_source_from_response(response)
  }
  
  /// The following options are available as a type:
  /// One image type, specified as a string `"image/webp"`
  /// 
  /// Multiple image types, specified as a tuple (`"image/webp"`, `"image/png"`).
  /// The smallest of the provided image types will be returned.
  /// 
  /// The transform object specifies the stylistic transformations
  /// that will be applied to the image.
  ///
  /// Include a background property to fill a transparent image's background.
  ///
  /// Specify a background color to convert an image with a transparent background
  /// to an image type which does not support transparency (like JPEG).
  /// 
  /// # Examples
  ///
  /// ```
  /// use tinify::Tinify;
  /// use tinify::convert::Color;
  /// use tinify::convert::Type;
  /// use tinify::TinifyError;
  ///
  /// fn main() -> Result<(), TinifyError> {
  ///   let _ = Tinify::new()
  ///     .set_key("api key")
  ///     .get_client()?
  ///     .from_url("https://tinypng.com/images/panda-happy.png")?
  ///     .convert((
  ///          Some(Type::JPEG),
  ///          None,
  ///          None,
  ///       ),
  ///       Some(Color("#FF5733")),
  ///     )?
  ///     .to_file("./converted.webp");
  ///
  ///   Ok(())
  /// }
  /// ```
  pub fn convert<T>(
    self,
    convert_type: (Option<T>, Option<T>, Option<T>),
    transform: Option<Color>,
  ) -> Result<Self, TinifyError>
  where
    T: AsRef<str> + Into<String> + Copy,
  {
    let types = &[
      &convert_type.0,
      &convert_type.1,
      &convert_type.2,
    ];
    let count: Vec<String> = types
      .iter()
      .filter_map(|&val| val.and_then(|x| Some(x.into())))
      .collect();
    let len = count.len();
    let parse_type = match len {
      _ if len >= 2 => serde_json::to_string(&count).unwrap(),
      _ => count.first().unwrap().to_string(),
    };
    let template = if let Some(color) = transform {
      convert::JsonData::new(
        Convert::new(parse_type),
        Some(Transform::new(color.0)),
      )
    } else {
      convert::JsonData::new(Convert::new(parse_type), None)
    };

    // Using replace to avoid invalid JSON string.
    let json_string = serde_json::to_string(&template)
      .unwrap()
      .replace("\"convert_type\"", "\"type\"")
      .replace(",\"transform\":null", "")
      .replace("\"[", "[")
      .replace("]\"", "]")
      .replace("\\\"", "\"");
    let reqwest_client = ReqwestClient::new();
    let response = reqwest_client
      .post(self.url.as_ref().unwrap())
      .header(
        CONTENT_TYPE,
        HeaderValue::from_static("application/json"),
      )
      .body(json_string)
      .basic_auth("api", self.key.as_ref())
      .timeout(Duration::from_secs(300))
      .send()?;

    if response.status() == StatusCode::BAD_REQUEST {
      return Err(TinifyError::ClientError);
    }
      
    self.get_source_from_response(response)

  }
  
  /// Save the compressed image to a file.
  pub fn to_file<P>(
    &self,
    path: P,
  ) -> Result<(), TinifyError>
  where
    P: AsRef<Path>,
  {
    let file = File::create(path)
      .map_err(|source| TinifyError::WriteError { source })?;
    let mut reader = BufWriter::new(file);
    reader.write_all(self.buffer.as_ref().unwrap())?;
    reader.flush()?;

    Ok(())
  }
  
  /// Convert the compressed image to a buffer.
  pub fn to_buffer(&self) -> Vec<u8> {
    self.buffer.as_ref().unwrap().to_vec()
  }

  fn get_source_from_response(
    mut self,
    response: Response,
  ) -> Result<Self, TinifyError> {
    if let Some(location) = response.headers().get("location") {
      let mut url = String::new();

      if !location.is_empty() {
        let slice =
          str::from_utf8(location.as_bytes()).unwrap();
        url.push_str(slice);
      }
    
      let get_request = self.request(Method::GET, &url, None);
      let bytes = get_request?.bytes()?.to_vec();
      self.buffer = Some(bytes);
      self.url = Some(url);
    } else {
      let bytes = response.bytes()?.to_vec();
      self.buffer = Some(bytes);
      self.url = None; 
    }

    Ok(self)  
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::TinifyError;
  use assert_matches::assert_matches;

  #[test]
  fn test_request_error() {
    let source = Source::new(None, None);
    let request = source.request(Method::GET, "", None).unwrap_err();

    assert_matches!(request, TinifyError::ReqwestError { .. });
  }
}