oss_api/api/
oss_file_api.rs1use robotech::api::api_settings::ApiSettings;
2use robotech::api::base_api::BaseApi;
3use robotech::ro::Ro;
4use std::fmt::Display;
5use std::string::ToString;
6use std::sync::OnceLock;
7
8pub static OSS_FILE_API: OnceLock<OssFileApi> = OnceLock::new();
10
11#[derive(Debug)]
13pub struct OssFileApi {
14 pub api_settings: ApiSettings,
15}
16
17impl BaseApi for OssFileApi {
18 fn get_api_settings(&self) -> &ApiSettings {
19 &self.api_settings
20 }
21}
22
23impl OssFileApi {
24 pub async fn upload_file(
36 &self,
37 bucket: &str,
38 file_path: &str,
39 file_name: &str,
40 ) -> Result<Ro<serde_json::Value>, Box<dyn std::error::Error>> {
41 let url = format!("/oss/file/upload/{}", bucket);
42 let form = reqwest::multipart::Form::new()
43 .file("file", file_path)
44 .await?
45 .text("fileName", file_name.to_string());
46
47 self.multipart(&url, form).await
48 }
49
50 pub async fn download_file(
60 &self,
61 obj_id: impl Display,
62 ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
63 let url = format!("/oss/file/download/{}", obj_id);
64 self.get_bytes(&url).await
65 }
66
67 pub async fn preview_file(
77 &self,
78 obj_id: impl Display,
79 ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
80 let url = format!("/oss/file/preview/{}", obj_id);
81 self.get_bytes(&url).await
82 }
83}