Skip to main content

oss_api_client/api_client/
oss_file_api_client.rs

1use anyhow::anyhow;
2use reqwest::header::{HeaderMap, HeaderValue};
3use robotech::api_client::ApiClientError;
4use robotech::cst::user_id_cst::USER_ID_HEADER_NAME;
5use robotech::micro_svc::FeignClient;
6use robotech::ro::Ro;
7use std::fmt::Display;
8
9pub struct OssFileApiClient {
10    pub feign_client: FeignClient,
11}
12
13impl OssFileApiClient {
14    pub fn new(feign_client: FeignClient) -> Self {
15        Self { feign_client }
16    }
17
18    pub async fn upload_file(
19        &self,
20        bucket: &str,
21        file_path: &str,
22        file_name: &str,
23        current_user_id: u64,
24    ) -> Result<Ro<serde_json::Value>, ApiClientError> {
25        let url = format!("/oss/file/upload/{}", bucket);
26        let form = reqwest::multipart::Form::new()
27            .file("file", file_path)
28            .await
29            .map_err(|e| ApiClientError::ReadFile(url.clone(), e))?
30            .text("fileName", file_name.to_string());
31        let mut headers = HeaderMap::new();
32        headers.insert(
33            USER_ID_HEADER_NAME,
34            HeaderValue::from_str(&current_user_id.to_string().as_str())
35                .map_err(|e| anyhow!("current_user_id: {}", e))?,
36        );
37
38        self.feign_client
39            .multipart::<String>(&url, form, Some(headers))
40            .await
41    }
42
43    pub async fn upload_file_content(
44        &self,
45        bucket: &str,
46        file_name: &str,
47        data: Vec<u8>,
48        current_user_id: u64,
49    ) -> Result<Ro<serde_json::Value>, ApiClientError> {
50        let url = format!("/oss/file/upload/{}", bucket);
51        let part = reqwest::multipart::Part::bytes(data).file_name(file_name.to_string());
52        let form = reqwest::multipart::Form::new().part("file", part);
53        let mut headers = HeaderMap::new();
54        headers.insert(
55            USER_ID_HEADER_NAME,
56            HeaderValue::from_str(&current_user_id.to_string().as_str())
57                .map_err(|e| anyhow!("current_user_id: {}", e))?,
58        );
59        self.feign_client
60            .multipart::<String>(&url, form, Some(headers))
61            .await
62    }
63
64    pub async fn download_file(
65        &self,
66        obj_id: impl Display,
67        current_user_id: u64,
68    ) -> Result<Vec<u8>, ApiClientError> {
69        let url = format!("/oss/file/download/{}", obj_id);
70        let mut headers = HeaderMap::new();
71        headers.insert(
72            USER_ID_HEADER_NAME,
73            HeaderValue::from_str(&current_user_id.to_string().as_str())
74                .map_err(|e| anyhow!("current_user_id: {}", e))?,
75        );
76        self.feign_client
77            .get_bytes::<()>(&url, None, Some(headers))
78            .await
79    }
80
81    pub async fn preview_file(
82        &self,
83        obj_id: impl Display,
84        current_user_id: u64,
85    ) -> Result<Vec<u8>, ApiClientError> {
86        let url = format!("/oss/file/preview/{}", obj_id);
87        let mut headers = HeaderMap::new();
88        headers.insert(
89            USER_ID_HEADER_NAME,
90            HeaderValue::from_str(&current_user_id.to_string().as_str())
91                .map_err(|e| anyhow!("current_user_id: {}", e))?,
92        );
93        self.feign_client
94            .get_bytes::<()>(&url, None, Some(headers))
95            .await
96    }
97}