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