rust_anticaptcha/instruments/
image_instrument.rs

1//! # ImageInstrument module
2//!
3//! Module contains struct and functions to read and
4//! encode file from local system or web to base64 string
5//!
6//! ## Basic example with image file reading and encode in base64 string
7//!
8//! ```
9//! use rust_anticaptcha::instruments::image_instrument::ImageInstrument;
10//!
11//! let image_instrument = ImageInstrument::new();
12//! let base64_str = image_instrument.read_image_file("files/captcha-image.jpg".to_string());
13//! ```
14
15use std::fs;
16use std::io::ErrorKind;
17
18use base64::alphabet::STANDARD;
19use base64::engine::general_purpose;
20use base64::engine::Engine;
21use base64::engine::GeneralPurpose;
22
23use crate::core::request_interface::RequestInterface;
24
25/// Struct help in encoding image from file/url to base64 string
26///
27/// # Examples
28///
29/// ```
30/// use rust_anticaptcha::instruments::image_instrument::ImageInstrument;
31///
32/// let image_instrument = ImageInstrument::new();
33/// ```
34pub struct ImageInstrument {
35    engine: GeneralPurpose,
36}
37impl Default for ImageInstrument {
38    fn default() -> Self {
39        ImageInstrument::new()
40    }
41}
42impl ImageInstrument {
43    /// Method init new ImageInstrument struct with base64 engine
44    ///
45    /// # Examples
46    ///
47    /// ```
48    /// use rust_anticaptcha::instruments::image_instrument::ImageInstrument;
49    ///
50    /// let image_instrument = ImageInstrument::new();
51    /// ```
52    ///
53    /// # Returns
54    /// Method return `ImageInstrument` instance
55    ///
56    pub fn new() -> Self {
57        ImageInstrument {
58            engine: GeneralPurpose::new(&STANDARD, general_purpose::PAD),
59        }
60    }
61
62    /// Method read image file and return base64 string
63    ///
64    /// # Arguments
65    /// `file_path` - path to image on local system
66    ///
67    /// # Examples
68    ///
69    /// ```
70    /// use rust_anticaptcha::instruments::image_instrument::ImageInstrument;
71    ///
72    /// let image_instrument = ImageInstrument::new();
73    /// let base64_str = image_instrument.read_image_file("files/captcha-image.jpg".to_string());
74    /// ```
75    ///
76    /// # Returns
77    /// Method return image as base64 string
78    ///
79    pub fn read_image_file(&self, file_path: String) -> String {
80        let contents = match fs::read(&file_path) {
81            Ok(content) => content,
82            Err(error) => match error.kind() {
83                ErrorKind::NotFound => panic!("File `{file_path}` is not exists!"),
84                _ => panic!("Can't read file - {error}"),
85            },
86        };
87        self.engine.encode(contents)
88    }
89
90    /// Method read image file from link and return base64 string
91    ///
92    /// # Arguments
93    /// `file_url` - image URL
94    ///
95    /// # Examples
96    ///
97    /// ```
98    /// use rust_anticaptcha::instruments::image_instrument::ImageInstrument;
99    ///
100    /// let image_instrument = ImageInstrument::new();
101    /// let base64_str = image_instrument.read_image_link("https://some-file-url.jpg".to_string());
102    /// ```
103    ///
104    /// # Returns
105    /// Method return image as base64 string
106    ///
107    pub async fn read_image_link(&self, file_url: String) -> String {
108        let request_client = RequestInterface::new();
109        let result = match request_client.send_get_request(&file_url).await {
110            Ok(result) => result,
111            Err(error) => panic!("{}", error),
112        };
113        self.engine.encode(result.bytes().await.unwrap())
114    }
115}