oss_api_client/api_client/
oss_file_api_client.rs1use anyhow::anyhow;
2use reqwest::header::{HeaderMap, HeaderValue};
3use robotech::api_client::ApiClient;
4use robotech::api_client::ApiClientError;
5use robotech::cst::user_id_cst::USER_ID_HEADER_NAME;
6use robotech::micro_svc::FeignClient;
7use robotech::ro::Ro;
8use std::fmt::Display;
9
10enum ClientBackend {
11 Feign(FeignClient),
12 Static(ApiClient),
13}
14
15pub struct OssFileApiClient {
16 backend: ClientBackend,
17}
18
19impl OssFileApiClient {
20 pub fn new_feign(client: FeignClient) -> Self {
21 Self {
22 backend: ClientBackend::Feign(client),
23 }
24 }
25
26 pub fn new_static(client: ApiClient) -> Self {
27 Self {
28 backend: ClientBackend::Static(client),
29 }
30 }
31
32 pub async fn upload_file(
33 &self,
34 bucket: &str,
35 file_path: &str,
36 file_name: &str,
37 current_user_id: u64,
38 ) -> Result<Ro<serde_json::Value>, ApiClientError> {
39 let url = format!("/oss/file/upload/{}", bucket);
40 let form = reqwest::multipart::Form::new()
41 .file("file", file_path)
42 .await
43 .map_err(|e| ApiClientError::ReadFile(url.clone(), e))?
44 .text("fileName", file_name.to_string());
45 let mut headers = HeaderMap::new();
46 headers.insert(
47 USER_ID_HEADER_NAME,
48 HeaderValue::from_str(¤t_user_id.to_string().as_str())
49 .map_err(|e| anyhow!("current_user_id: {}", e))?,
50 );
51
52 match &self.backend {
53 ClientBackend::Feign(c) => {
54 c.multipart::<String>(&url, form, Some(headers)).await
55 }
56 ClientBackend::Static(c) => {
57 c.multipart(&url, form, Some(headers), None).await
58 }
59 }
60 }
61
62 pub async fn upload_file_content(
63 &self,
64 bucket: &str,
65 file_name: &str,
66 data: Vec<u8>,
67 current_user_id: u64,
68 ) -> Result<Ro<serde_json::Value>, ApiClientError> {
69 let url = format!("/oss/file/upload/{}", bucket);
70 let part = reqwest::multipart::Part::bytes(data).file_name(file_name.to_string());
71 let form = reqwest::multipart::Form::new().part("file", part);
72 let mut headers = HeaderMap::new();
73 headers.insert(
74 USER_ID_HEADER_NAME,
75 HeaderValue::from_str(¤t_user_id.to_string().as_str())
76 .map_err(|e| anyhow!("current_user_id: {}", e))?,
77 );
78 match &self.backend {
79 ClientBackend::Feign(c) => {
80 c.multipart::<String>(&url, form, Some(headers)).await
81 }
82 ClientBackend::Static(c) => {
83 c.multipart(&url, form, Some(headers), None).await
84 }
85 }
86 }
87
88 pub async fn download_file(
89 &self,
90 obj_id: impl Display,
91 current_user_id: u64,
92 ) -> Result<Vec<u8>, ApiClientError> {
93 let url = format!("/oss/file/download/{}", obj_id);
94 let mut headers = HeaderMap::new();
95 headers.insert(
96 USER_ID_HEADER_NAME,
97 HeaderValue::from_str(¤t_user_id.to_string().as_str())
98 .map_err(|e| anyhow!("current_user_id: {}", e))?,
99 );
100 match &self.backend {
101 ClientBackend::Feign(c) => {
102 c.get_bytes::<()>(&url, None, Some(headers)).await
103 }
104 ClientBackend::Static(c) => {
105 c.get_bytes::<()>(&url, None, Some(headers), None).await
106 }
107 }
108 }
109
110 pub async fn preview_file(
111 &self,
112 obj_id: impl Display,
113 current_user_id: u64,
114 ) -> Result<Vec<u8>, ApiClientError> {
115 let url = format!("/oss/file/preview/{}", obj_id);
116 let mut headers = HeaderMap::new();
117 headers.insert(
118 USER_ID_HEADER_NAME,
119 HeaderValue::from_str(¤t_user_id.to_string().as_str())
120 .map_err(|e| anyhow!("current_user_id: {}", e))?,
121 );
122 match &self.backend {
123 ClientBackend::Feign(c) => {
124 c.get_bytes::<()>(&url, None, Some(headers)).await
125 }
126 ClientBackend::Static(c) => {
127 c.get_bytes::<()>(&url, None, Some(headers), None).await
128 }
129 }
130 }
131}