bitbucket_cli/api/
downloads.rs1use std::path::PathBuf;
2
3use anyhow::{Context, Result};
4use reqwest::multipart::{Form, Part};
5
6use super::BitbucketClient;
7use crate::api::util::urlencode_segment;
8use crate::models::Download;
9
10impl BitbucketClient {
11 pub async fn upload_downloads(
15 &self,
16 workspace: &str,
17 repo_slug: &str,
18 files: &[(String, PathBuf)],
19 ) -> Result<()> {
20 let mut form = Form::new();
21
22 for (upload_name, path) in files {
23 let bytes = std::fs::read(path)
27 .with_context(|| format!("Failed to read file '{}'", path.display()))?;
28 let part = Part::bytes(bytes).file_name(upload_name.clone());
29 form = form.part("files", part);
31 }
32
33 let path = format!("/repositories/{}/{}/downloads", workspace, repo_slug);
34 self.post_multipart(&path, form).await
35 }
36
37 pub async fn list_downloads(&self, workspace: &str, repo_slug: &str) -> Result<Vec<Download>> {
40 let path = format!("/repositories/{}/{}/downloads", workspace, repo_slug);
41 self.get_all_pages::<Download>(&path).await
42 }
43
44 pub async fn get_download(
46 &self,
47 workspace: &str,
48 repo_slug: &str,
49 name: &str,
50 ) -> Result<bytes::Bytes> {
51 let path = format!(
52 "/repositories/{}/{}/downloads/{}",
53 workspace,
54 repo_slug,
55 urlencode_segment(name)
56 );
57 self.get_bytes(&path).await
58 }
59
60 pub async fn delete_download(
62 &self,
63 workspace: &str,
64 repo_slug: &str,
65 filename: &str,
66 ) -> Result<()> {
67 let path = format!(
68 "/repositories/{}/{}/downloads/{}",
69 workspace,
70 repo_slug,
71 urlencode_segment(filename)
72 );
73 self.delete(&path).await
74 }
75}
76
77pub fn download_url(workspace: &str, repo_slug: &str, name: &str) -> String {
82 format!(
83 "https://bitbucket.org/{}/{}/downloads/{}",
84 workspace,
85 repo_slug,
86 urlencode_segment(name)
87 )
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn download_url_is_browser_facing_host() {
96 assert_eq!(
97 download_url("acme", "widgets", "shot.png"),
98 "https://bitbucket.org/acme/widgets/downloads/shot.png"
99 );
100 }
101
102 #[test]
103 fn download_url_encodes_spaces_and_specials() {
104 assert_eq!(
105 download_url("acme", "widgets", "my shot (1).png"),
106 "https://bitbucket.org/acme/widgets/downloads/my%20shot%20%281%29.png"
107 );
108 }
109
110 #[test]
111 fn urlencode_leaves_safe_chars_untouched() {
112 assert_eq!(urlencode_segment("a-b_c.d~1.png"), "a-b_c.d~1.png");
113 }
114}