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
use crate::types::primitive::Integer;
use mime::Mime;
use serde::Deserialize;
use std::{fmt, io::Read, path::PathBuf};

/// File ready to be downloaded
///
/// The file can be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>
/// It is guaranteed that the link will be valid for at least 1 hour
/// When the link expires, a new one can be requested by calling getFile
/// Maximum file size to download is 20 MB
#[derive(Clone, Debug, Deserialize)]
pub struct File {
    /// Identifier for this file, which can be used to download or reuse the file
    pub file_id: String,
    /// Unique identifier for this file
    ///
    /// It is supposed to be the same over time and for different bots.
    /// Can't be used to download or reuse the file.
    pub file_unique_id: String,
    /// File size, if known
    pub file_size: Option<Integer>,
    /// File path
    /// Use https://api.telegram.org/file/bot<token>/<file_path> to get the file
    pub file_path: Option<String>,
}

/// Information about a file for reader
#[derive(Clone, Debug)]
pub struct InputFileInfo {
    pub(crate) name: String,
    pub(crate) mime_type: Option<Mime>,
}

impl InputFileInfo {
    /// Creates a new info object with given file name
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            name: name.into(),
            mime_type: None,
        }
    }

    /// Sets mime type of a file
    pub fn mime_type(mut self, mime_type: Mime) -> Self {
        self.mime_type = Some(mime_type);
        self
    }
}

impl From<&str> for InputFileInfo {
    fn from(name: &str) -> Self {
        InputFileInfo::new(name)
    }
}

impl From<(&str, Mime)> for InputFileInfo {
    fn from((name, mime_type): (&str, Mime)) -> Self {
        InputFileInfo::new(name).mime_type(mime_type)
    }
}

impl From<String> for InputFileInfo {
    fn from(name: String) -> Self {
        InputFileInfo::new(name)
    }
}

impl From<(String, Mime)> for InputFileInfo {
    fn from((name, mime_type): (String, Mime)) -> Self {
        InputFileInfo::new(name).mime_type(mime_type)
    }
}

/// File reader to upload
pub struct InputFileReader {
    pub(crate) reader: Box<dyn Read + Send>,
    pub(crate) info: Option<InputFileInfo>,
}

impl fmt::Debug for InputFileReader {
    fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(out, "InputFileReader(reader: ..., info: {:?})", self.info)
    }
}

impl InputFileReader {
    /// Creates a new file reader
    pub fn new<R>(reader: R) -> Self
    where
        R: Read + Send + 'static,
    {
        InputFileReader {
            reader: Box::new(reader),
            info: None,
        }
    }

    /// Sets a file info
    pub fn info<I: Into<InputFileInfo>>(mut self, info: I) -> Self {
        self.info = Some(info.into());
        self
    }
}

impl<R> From<R> for InputFileReader
where
    R: Read + Send + 'static,
{
    fn from(reader: R) -> Self {
        InputFileReader::new(reader)
    }
}

/// File to upload
#[derive(Debug)]
pub struct InputFile {
    pub(crate) kind: InputFileKind,
}

impl InputFile {
    /// Send a file_id that exists on the Telegram servers
    pub fn file_id<S: Into<String>>(file_id: S) -> Self {
        Self {
            kind: InputFileKind::Id(file_id.into()),
        }
    }

    /// Send an HTTP URL to get a file from the Internet
    ///
    /// Telegram will download a file from that URL
    pub fn url<S: Into<String>>(url: S) -> Self {
        Self {
            kind: InputFileKind::Url(url.into()),
        }
    }

    /// Path to file in FS (will be uploaded using multipart/form-data)
    pub fn path<P: Into<PathBuf>>(path: P) -> Self {
        Self {
            kind: InputFileKind::Path(path.into()),
        }
    }

    /// A reader (file will be uploaded using multipart/form-data)
    pub fn reader<R: Into<InputFileReader>>(reader: R) -> Self {
        Self {
            kind: InputFileKind::Reader(reader.into()),
        }
    }
}

pub(crate) enum InputFileKind {
    Id(String),
    Url(String),
    Path(PathBuf),
    Reader(InputFileReader),
}

impl fmt::Debug for InputFileKind {
    fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            InputFileKind::Id(ref s) => write!(out, "InputFileKind::Id({:?})", s),
            InputFileKind::Url(ref s) => write!(out, "InputFileKind::Url({:?})", s),
            InputFileKind::Path(ref s) => write!(out, "InputFileKind::Path({:?})", s),
            InputFileKind::Reader(ref r) => write!(out, "InputFileKind::Reader({:?})", r),
        }
    }
}

impl From<InputFileReader> for InputFile {
    fn from(reader: InputFileReader) -> Self {
        Self::reader(reader)
    }
}

impl<R> From<R> for InputFile
where
    R: Read + Send + 'static,
{
    fn from(reader: R) -> Self {
        InputFile::reader(InputFileReader::new(reader))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    #[test]
    fn deserialize_file_full() {
        let data: File = serde_json::from_value(serde_json::json!({
            "file_id": "id",
            "file_unique_id": "unique-id",
            "file_size": 123,
            "file_path": "path"
        }))
        .unwrap();
        assert_eq!(data.file_id, "id");
        assert_eq!(data.file_unique_id, "unique-id");
        assert_eq!(data.file_size.unwrap(), 123);
        assert_eq!(data.file_path.unwrap(), "path");
    }

    #[test]
    fn deserialize_file_partial() {
        let data: File = serde_json::from_value(serde_json::json!({
            "file_id": "id",
            "file_unique_id": "unique-id"
        }))
        .unwrap();
        assert_eq!(data.file_id, "id");
        assert_eq!(data.file_unique_id, "unique-id");
        assert!(data.file_size.is_none());
        assert!(data.file_path.is_none());
    }

    #[test]
    fn input_file() {
        let id = InputFile::file_id("file-id");
        assert_eq!(format!("{:?}", id.kind), r#"InputFileKind::Id("file-id")"#);
        let url = InputFile::url("http://example.com/archive.zip");
        assert_eq!(
            format!("{:?}", url.kind),
            r#"InputFileKind::Url("http://example.com/archive.zip")"#
        );
        let path = InputFile::path("/home/user/data/archive.zip");
        assert_eq!(
            format!("{:?}", path.kind),
            r#"InputFileKind::Path("/home/user/data/archive.zip")"#
        );

        let reader = InputFileReader::from(Cursor::new(b"data")).info(("name", mime::TEXT_PLAIN));
        let reader = InputFile::from(reader);
        assert!(format!("{:?}", reader.kind).starts_with("InputFileKind::Reader("));

        let reader = InputFile::from(Cursor::new(b"data"));
        assert!(format!("{:?}", reader.kind).starts_with("InputFileKind::Reader("));
    }

    #[test]
    fn input_file_info() {
        let info = InputFileInfo::from("name");
        assert_eq!(info.name, "name");
        assert!(info.mime_type.is_none());

        let info = InputFileInfo::from(("name", mime::TEXT_PLAIN));
        assert_eq!(info.name, "name");
        assert_eq!(info.mime_type.unwrap(), mime::TEXT_PLAIN);

        let info = InputFileInfo::from(String::from("name"));
        assert_eq!(info.name, "name");
        assert!(info.mime_type.is_none());

        let info = InputFileInfo::from((String::from("name"), mime::TEXT_PLAIN));
        assert_eq!(info.name, "name");
        assert_eq!(info.mime_type.unwrap(), mime::TEXT_PLAIN);
    }
}