mwbot/upload.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 std::collections::HashMap;
19use std::path::PathBuf;
20
21/// Builder to construct a request for file uploads
22pub struct UploadRequest {
23 pub(crate) comment: Option<String>,
24 pub(crate) tags: Vec<String>,
25 pub(crate) text: Option<String>,
26 pub(crate) ignore_warnings: bool,
27 pub(crate) file: PathBuf,
28 pub(crate) chunk_size: usize,
29}
30
31impl UploadRequest {
32 /// Upload a file that is at the specified path
33 pub fn from_path(path: PathBuf) -> Self {
34 Self {
35 comment: None,
36 tags: vec![],
37 text: None,
38 ignore_warnings: false,
39 file: path,
40 chunk_size: 5_000_000,
41 }
42 }
43
44 /// Set the upload comment
45 pub fn comment(mut self, comment: String) -> Self {
46 self.comment = Some(comment);
47 self
48 }
49
50 /// Add tags to the edit and upload entry
51 pub fn tags(mut self, tags: Vec<String>) -> Self {
52 self.tags.extend(tags);
53 self
54 }
55
56 /// If this is a new page, specify the initial wikitext
57 pub fn text(mut self, text: String) -> Self {
58 self.text = Some(text);
59 self
60 }
61
62 /// Ignore any upload warnings
63 pub fn ignore_warnings(mut self, val: bool) -> Self {
64 self.ignore_warnings = val;
65 self
66 }
67
68 /// Set the chunk size, in bytes. By default it is
69 /// 5MB (`5_000_000`).
70 pub fn chunk_size(mut self, val: usize) -> Self {
71 self.chunk_size = val;
72 self
73 }
74
75 pub(crate) fn params(&self) -> HashMap<&'static str, String> {
76 let mut params = HashMap::new();
77 if let Some(comment) = &self.comment {
78 params.insert("comment", comment.to_string());
79 }
80 if !self.tags.is_empty() {
81 params.insert("tags", self.tags.join("|"));
82 }
83 if let Some(text) = &self.text {
84 params.insert("text", text.to_string());
85 }
86 params
87 }
88}