oss_api/api/
oss_file_api.rs1use robotech::api::api_settings::ApiSettings;
2use robotech::api::base_api::BaseApi;
3use robotech::cst::user_id_cst::SYS_OPERATOR_USER_ID;
4use robotech::ro::Ro;
5use std::fmt::Display;
6use std::string::ToString;
7
8#[derive(Debug)]
10pub struct OssFileApi {
11 pub api_settings: ApiSettings,
12}
13
14impl BaseApi for OssFileApi {
15 fn get_api_settings(&self) -> &ApiSettings {
16 &self.api_settings
17 }
18}
19
20impl OssFileApi {
21 pub async fn upload_file(
33 &self,
34 bucket: &str,
35 file_path: &str,
36 file_name: &str,
37 ) -> Result<Ro<serde_json::Value>, Box<dyn std::error::Error + Send + Sync>> {
38 let url = format!("/oss/file/upload/{}", bucket);
39 let form = reqwest::multipart::Form::new()
40 .file("file", file_path)
41 .await?
42 .text("fileName", file_name.to_string());
43
44 self.multipart_with_current_user_id(&url, form, SYS_OPERATOR_USER_ID)
45 .await
46 }
47
48 pub async fn upload_file_content(
60 &self,
61 bucket: &str,
62 file_name: &str,
63 data: Vec<u8>,
64 ) -> Result<Ro<serde_json::Value>, Box<dyn std::error::Error + Send + Sync>> {
65 let url = format!("/oss/file/upload/{}", bucket);
66 let part = reqwest::multipart::Part::bytes(data).file_name(file_name.to_string());
67 let form = reqwest::multipart::Form::new().part("file", part);
68 self.multipart_with_current_user_id(&url, form, SYS_OPERATOR_USER_ID)
69 .await
70 }
71
72 pub async fn download_file(
82 &self,
83 obj_id: impl Display,
84 ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
85 let url = format!("/oss/file/download/{}", obj_id);
86 self.get_bytes(&url).await
87 }
88
89 pub async fn preview_file(
99 &self,
100 obj_id: impl Display,
101 ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
102 let url = format!("/oss/file/preview/{}", obj_id);
103 self.get_bytes(&url).await
104 }
105}