filesystem.rs
1 use crate::api::FileEntry; 2 use reqwest::Error; 3 4 pub struct FileService; 5 6 impl FileService { 7 pub async fn list(base_url: &str, location: &str) -> Result<Vec<FileEntry>, Error> { 8 reqwest::get(format!("{base_url}/api/filesystem/{location}")) 9 .await? 10 .json() 11 .await 12 } 13 14 pub async fn upload( 15 base_url: &str, 16 location: &str, 17 filename: &str, 18 data: &[u8], 19 ) -> Result<reqwest::Response, Error> { 20 let part = reqwest::multipart::Part::bytes(data.to_vec()) 21 .file_name(filename.to_string()); 22 let form = reqwest::multipart::Form::new() 23 .part("file", part); 24 reqwest::Client::new() 25 .put(format!("{base_url}/api/filesystem/{location}/{filename}")) 26 .multipart(form) 27 .send() 28 .await 29 } 30 31 pub async fn delete( 32 base_url: &str, 33 location: &str, 34 path: &str, 35 ) -> Result<reqwest::Response, Error> { 36 reqwest::Client::new() 37 .delete(format!("{base_url}/api/filesystem/{location}/{path}")) 38 .send() 39 .await 40 } 41 42 pub async fn read_text( 43 base_url: &str, 44 location: &str, 45 path: &str, 46 ) -> Result<String, Error> { 47 reqwest::get(format!("{base_url}/api/filesystem/{location}/{path}")) 48 .await? 49 .text() 50 .await 51 } 52 53 pub async fn rename( 54 base_url: &str, 55 location: &str, 56 old_name: &str, 57 new_name: &str, 58 ) -> Result<reqwest::Response, Error> { 59 reqwest::Client::new() 60 .patch(format!("{base_url}/api/filesystem/{location}/{old_name}")) 61 .json(&serde_json::json!({ "name": new_name })) 62 .send() 63 .await 64 } 65 }