mwbot/
file.rs

1/*
2Copyright (C) 2023 Kunal Mehta <legoktm@debian.org>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18use crate::upload::UploadRequest;
19use crate::{Page, Result};
20
21/// Represents a on-wiki file page.
22#[derive(Debug)]
23pub struct File {
24    page: Page,
25}
26
27impl File {
28    pub(crate) fn new(page: &Page) -> Self {
29        assert!(page.is_file());
30        Self { page: page.clone() }
31    }
32
33    /// Upload a new file
34    pub async fn upload(self, req: UploadRequest) -> Result<Self> {
35        let params = req.params();
36        let filename = self
37            .page
38            .bot
39            .api()
40            .upload(
41                self.page.title(),
42                req.file,
43                req.chunk_size,
44                req.ignore_warnings,
45                params,
46            )
47            .await?;
48        Ok(self
49            .page
50            .bot
51            .page(&filename)?
52            .as_file()
53            .expect("MediaWiki API returned non-file title"))
54    }
55}