filesvc_rs/
http.rs

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
use std::{fs, path::Path, time::Duration};

use derive_builder::Builder;
use reqwest::{Body, Method, StatusCode};
use thiserror::Error;

impl<'a> super::Client<'a> {
    pub async fn upload_web_file<P: AsRef<Path>>(
        &self,
        path: P,
        upload: Upload,
    ) -> Result<(String, String), UploadError> {
        let bytes = fs::read(path)?;
        self.upload_web(bytes, upload).await
    }

    pub async fn upload_web<'b, B: Into<Body>>(
        &self,
        body: B,
        upload: Upload,
    ) -> Result<(String, String), UploadError> {
        let mut request = self
            .http
            .request(
                Method::PUT,
                format!("{}{}/", upload.protocol.to_string(), self.base_uri),
            )
            .body(body);

        if let Some(filename) = upload.filename {
            request = request.query(&[("filename", filename)]);
        }

        if let Some(randomizefn) = upload.randomizefn {
            request = request.query(&[("randomizefn", randomizefn as i32)]);
        }

        if let Some(expire) = upload.expire {
            request = request.query(&[("expire", expire.as_secs() / 60)]);
        }

        if let Some(autodestroy) = upload.autodestroy {
            request = request.query(&[("autodestroy", autodestroy as i32)]);
        }

        if let Some(shorturl) = upload.shorturl {
            request = request.query(&[("shorturl", shorturl as i32)]);
        }

        let response = request.send().await?;

        let content = match response.status() {
            StatusCode::OK => response.text().await?,
            StatusCode::PAYLOAD_TOO_LARGE => return Err(UploadError::PayloadTooLarge),
            status => return Err(UploadError::UnknownStatusCode(status)),
        };

        if !content.contains("[Admin]") || !content.contains("[Download]") {
            return Err(UploadError::MalformedResponse(content));
        }

        let mut lines = content.lines().skip(1);

        let admin_url = lines
            .next()
            .ok_or(UploadError::MalformedResponse(content.to_owned()))?
            .strip_suffix(" [Admin]")
            .ok_or(UploadError::MalformedResponse(content.to_owned()))?
            .to_string();
        let download_url = lines
            .next()
            .ok_or(UploadError::MalformedResponse(content.to_owned()))?
            .strip_suffix(" [Download]")
            .ok_or(UploadError::MalformedResponse(content.to_owned()))?
            .to_string();

        Ok((admin_url, download_url))
    }
}

#[derive(Debug, Error)]
pub enum UploadError {
    #[error("The payload you are trying to upload is too large")]
    PayloadTooLarge,
    #[error("The server returned an unknown status code {0}")]
    UnknownStatusCode(StatusCode),
    #[error("The server returned a malformed response {0}")]
    MalformedResponse(String),
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    Http(#[from] reqwest::Error),
}

#[derive(Default, Debug, Clone, Builder)]
#[builder(setter(into), default)]
pub struct Upload {
    protocol: UploadProtocol,
    filename: Option<String>,
    randomizefn: Option<bool>,
    expire: Option<Duration>,
    autodestroy: Option<bool>,
    shorturl: Option<bool>,
}

impl Upload {
    pub fn builder() -> UploadBuilder {
        UploadBuilder::default()
    }
}

#[derive(Default, Debug, Clone)]
pub enum UploadMethod {
    #[default]
    Put,
    Post,
}

#[derive(Default, Debug, Clone)]
pub enum UploadProtocol {
    #[default]
    Https,
    Http,
}

impl ToString for UploadProtocol {
    fn to_string(&self) -> String {
        match self {
            UploadProtocol::Https => "https://".to_string(),
            UploadProtocol::Http => "http://".to_string(),
        }
    }
}