tgbot/types/file/
mod.rs

1use serde::{Deserialize, Serialize};
2
3pub use self::{animation::*, audio::*, document::*, photo::*, video::*, video_note::*, voice::*};
4use crate::{
5    api::{Method, Payload},
6    types::Integer,
7};
8
9#[cfg(test)]
10mod tests;
11
12mod animation;
13mod audio;
14mod document;
15mod photo;
16mod video;
17mod video_note;
18mod voice;
19
20/// Represents a file ready to be downloaded.
21///
22/// The file can be downloaded via the link `https://api.telegram.org/file/bot<token>/<file_path>`.
23/// It is guaranteed that the link will be valid for at least 1 hour.
24/// When the link expires, a new one can be requested by calling [`GetFile`].
25/// Maximum file size to download is 20 MB.
26#[serde_with::skip_serializing_none]
27#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
28pub struct File {
29    /// Identifier of the file.
30    ///
31    /// Can be used to download or reuse the file.
32    pub file_id: String,
33    /// Unique identifier of the file.
34    ///
35    /// It is supposed to be the same over time and for different bots.
36    /// Can't be used to download or reuse the file.
37    pub file_unique_id: String,
38    /// File size in bytes.
39    pub file_size: Option<Integer>,
40    /// File path.
41    ///
42    /// Use [`crate::api::Client::download_file`] to get the file.
43    pub file_path: Option<String>,
44}
45
46impl File {
47    /// Creates a new `File`.
48    ///
49    /// # Arguments
50    ///
51    /// * `file_id` - Identifier of the file.
52    /// * `file_unique_id` - Unique identifier of the file.
53    pub fn new<A, B>(file_id: A, file_unique_id: B) -> Self
54    where
55        A: Into<String>,
56        B: Into<String>,
57    {
58        Self {
59            file_id: file_id.into(),
60            file_unique_id: file_unique_id.into(),
61            file_size: None,
62            file_path: None,
63        }
64    }
65
66    /// Sets a new size of the file.
67    ///
68    /// # Arguments
69    ///
70    /// * `value` - The size of the file in bytes.
71    pub fn with_file_size(mut self, value: Integer) -> Self {
72        self.file_size = Some(value);
73        self
74    }
75
76    /// Sets a new path of the file.
77    ///
78    /// # Arguments
79    ///
80    /// * `value` - The path of the file.
81    pub fn with_file_path<T>(mut self, value: T) -> Self
82    where
83        T: Into<String>,
84    {
85        self.file_path = Some(value.into());
86        self
87    }
88}
89
90/// Returns a basic information about a file and prepares it for downloading.
91///
92/// For the moment, bots can download files of up to 20MB in size.
93///
94/// The file can then be downloaded via the link
95/// `https://api.telegram.org/file/bot<token>/<file_path>`,
96/// where `<file_path>` is taken from the response.
97///
98/// It is guaranteed that the link will be valid for at least 1 hour.
99///
100/// When the link expires, a new one can be requested by calling `GetFile` again.
101///
102/// # Notes
103///
104/// This function may not preserve the original file name and MIME type.
105/// You should save the file's MIME type and name (if available) when the `File` object is received.
106#[derive(Clone, Debug, Serialize)]
107pub struct GetFile {
108    file_id: String,
109}
110
111impl GetFile {
112    /// Creates a new `GetFile`.
113    ///
114    /// # Arguments
115    ///
116    /// * `file_id` - File identifier to get info about.
117    pub fn new<T>(file_id: T) -> Self
118    where
119        T: Into<String>,
120    {
121        GetFile {
122            file_id: file_id.into(),
123        }
124    }
125}
126
127impl Method for GetFile {
128    type Response = File;
129
130    fn into_payload(self) -> Payload {
131        Payload::json("getFile", self)
132    }
133}