ffsend_api/api/
url.rs

1use url::Url;
2
3use crate::file::remote_file::RemoteFile;
4
5/// A struct, that helps building URLs for communicating with a remote host.
6pub struct UrlBuilder;
7
8impl UrlBuilder {
9    /// Get the download URL of the given file.
10    /// This URL is identical to the share URL, a term used in this API.
11    /// Set `secret` to `true`, to include it in the URL if known.
12    pub fn download(file: &RemoteFile, secret: bool) -> Url {
13        // Get the share URL, and update the secret fragment
14        let mut url = file.url().clone();
15        if secret && file.has_secret() {
16            url.set_fragment(Some(&file.secret()));
17        } else {
18            url.set_fragment(None);
19        }
20
21        url
22    }
23
24    /// Generate an API file URL, with the given endpoint.
25    /// The endpoint should not contain any slashes.
26    ///
27    /// Valid endpoints may be 'metadata', 'download' or for example
28    /// 'password'.
29    fn api(endpoint: &str, file: &RemoteFile) -> Url {
30        // Get the share URL, and add the secret fragment
31        let mut url = file.url().clone();
32        url.set_path(format!("/api/{}/{}", endpoint, file.id()).as_str());
33        url.set_fragment(None);
34
35        url
36    }
37
38    /// Get the API metadata URL for the given file.
39    pub fn api_metadata(file: &RemoteFile) -> Url {
40        Self::api("metadata", file)
41    }
42
43    /// Get the API download URL for the given file.
44    pub fn api_download(file: &RemoteFile) -> Url {
45        Self::api("download", file)
46    }
47
48    /// Get the API password URL for the given file.
49    pub fn api_password(file: &RemoteFile) -> Url {
50        Self::api("password", file)
51    }
52
53    /// Get the API params URL for the given file.
54    pub fn api_params(file: &RemoteFile) -> Url {
55        Self::api("params", file)
56    }
57
58    /// Get the API info URL for the given file.
59    pub fn api_info(file: &RemoteFile) -> Url {
60        Self::api("info", file)
61    }
62
63    /// Get the API exists URL for the given file.
64    pub fn api_exists(file: &RemoteFile) -> Url {
65        Self::api("exists", file)
66    }
67
68    /// Get the API delete URL for the given file.
69    pub fn api_delete(file: &RemoteFile) -> Url {
70        Self::api("delete", file)
71    }
72}