use super::{NullResponse, Response};
use reqwest::Body;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
pub async fn mkdir(server: &str, token: &str, path: &str) -> Result<(), String> {
let url = format!("{}/api/fs/mkdir", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Connection", "keep-alive")
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(&json!({"path": path}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
pub async fn rename(server: &str, token: &str, path: &str, name: &str) -> Result<(), String> {
let url = format!("{}/api/fs/rename", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(&json!({"path":path,"name":name}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
pub struct UploadParams {
pub local_file: String,
pub remote_path: String,
pub remote_name: String,
}
pub async fn upload(server: &str, token: &str, params: UploadParams) -> Result<(), String> {
let url = format!("{}/api/fs/put", server);
let file = match File::open(params.local_file).await {
Ok(file) => file,
Err(err) => {
return Err(err.to_string());
}
};
let filesize = file.metadata().await.unwrap().len();
let stream = FramedRead::new(file, BytesCodec::new());
let file_body = Body::wrap_stream(stream);
let resp: Response<NullResponse> = reqwest::Client::new()
.put(url)
.header("Authorization", token)
.header(
"File-Path",
format!("{}/{}", params.remote_path, params.remote_name),
)
.header("Content-Length", filesize)
.header("Connection", "keep-alive")
.body(file_body)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ListdirData {
pub content: Vec<DirFileInfo>,
pub total: usize,
pub readme: String,
pub write: bool,
pub provider: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DirFileInfo {
pub name: String,
pub size: u128,
pub is_dir: bool,
pub modified: String,
pub sign: String,
pub thumb: String,
pub r#type: isize,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct FileParams {
pub path: Option<String>,
pub password: Option<String>,
pub page: Option<usize>,
pub per_page: Option<usize>,
pub refresh: Option<bool>,
}
pub async fn listdir(server: &str, token: &str, params: FileParams) -> Result<ListdirData, String> {
let url = format!("{}/api/fs/list", server);
let resp: Response<ListdirData> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(resp.data.unwrap())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FileInfo {
pub name: String,
pub size: u128,
pub is_dir: bool,
pub modified: String,
pub sign: String,
pub thumb: String,
pub r#type: isize,
pub row_url: String,
pub readme: String,
pub provider: String,
pub related: Option<String>,
}
pub async fn fileinfo(server: &str, token: &str, params: FileParams) -> Result<FileInfo, String> {
let url = format!("{}/api/fs/get", server);
let resp: Response<FileInfo> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(resp.data.unwrap())
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct SearchParams {
pub parent: String,
pub keywords: String,
pub scope: Option<u8>,
pub page: Option<usize>,
pub per_page: Option<usize>,
pub password: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SearchFileData {
pub content: Vec<SearchFileInfo>,
pub total: usize,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SearchFileInfo {
pub name: String,
pub parent: String,
pub size: u128,
pub is_dir: bool,
pub r#type: isize,
}
pub async fn search(
server: &str,
token: &str,
params: SearchParams,
) -> Result<SearchFileData, String> {
let url = format!("{}/api/fs/search", server);
let resp: Response<SearchFileData> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(resp.data.unwrap())
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct GetDirParams {
pub parent: String,
pub page: Option<usize>,
pub per_page: Option<usize>,
pub password: Option<String>,
pub force_root: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SearchDirData {
pub content: Vec<SearchDirInfo>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SearchDirInfo {
pub name: String,
pub modified: String,
}
pub async fn get_dirs(
server: &str,
token: &str,
params: GetDirParams,
) -> Result<SearchDirData, String> {
let url = format!("{}/api/fs/dirs", server);
let resp: Response<SearchDirData> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(resp.data.unwrap())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RenameParams {
pub src_name: String,
pub new_name: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BatchRenameParams {
pub src_dir: String,
pub rename_objects: Vec<RenameParams>,
}
pub async fn batch_rename(
server: &str,
token: &str,
params: BatchRenameParams,
) -> Result<(), String> {
let url = format!("{}/api/fs/batch_rename", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RegexRenameParams {
pub src_name_regex: String,
pub new_name_regex: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BatchRegexRenameParams {
pub src_dir: String,
pub rename_objects: Vec<RegexRenameParams>,
}
pub async fn regex_rename(
server: &str,
token: &str,
params: BatchRegexRenameParams,
) -> Result<(), String> {
let url = format!("{}/api/fs/regex_rename", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MoveParams {
pub src_dir: String,
pub dst_dir: String,
pub names: Vec<String>,
}
pub async fn move_file(server: &str, token: &str, params: MoveParams) -> Result<(), String> {
let url = format!("{}/api/fs/move", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RecursiveMoveParams {
pub src_dir: String,
pub dst_dir: String,
}
pub async fn recursive_move(
server: &str,
token: &str,
params: RecursiveMoveParams,
) -> Result<(), String> {
let url = format!("{}/api/fs/recursive_move", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CopyParams {
pub src_dir: String,
pub dst_dir: String,
pub names: Vec<String>,
}
pub async fn copy_file(server: &str, token: &str, params: CopyParams) -> Result<(), String> {
let url = format!("{}/api/fs/copy", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteParams {
pub dir: String,
pub names: Vec<String>,
}
pub async fn remove_directory(
server: &str,
token: &str,
params: DeleteParams,
) -> Result<(), String> {
let url = format!("{}/api/fs/remove", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
pub async fn remove_empty_directory(
server: &str,
token: &str,
src_dir: String,
) -> Result<(), String> {
let url = format!("{}/api/fs/remove_empty_directory", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(&json!({"src_dir": src_dir}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
#[derive(Debug, Serialize, Deserialize)]
pub struct OfflineTaskParams {
pub path: String,
pub urls: Vec<String>,
}
pub async fn add_aria2_task(
server: &str,
token: &str,
params: OfflineTaskParams,
) -> Result<(), String> {
let url = format!("{}/api/fs/add_aria2", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}
pub async fn add_qbit_task(
server: &str,
token: &str,
params: OfflineTaskParams,
) -> Result<(), String> {
let url = format!("{}/api/fs/add_qbit", server);
let resp: Response<NullResponse> = reqwest::Client::new()
.post(url)
.header("Authorization", token)
.header("Content-Type", "application/json")
.json(¶ms)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
if resp.code != 200 {
return Err(resp.message);
}
Ok(())
}