alistapi 0.1.2

alist api sdk
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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};

/// 新建文件夹 POST /api/fs/mkdir
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(())
}

/// 重命名文件 POST /api/fs/rename
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,
}

/// 流式上传文件 PUT /api/fs/put
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>,
}

/// 列出文件目录 POST /api/fs/list
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(&params)
        .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>,
}

/// 获取某个文件/目录信息 POST /api/fs/get
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(&params)
        .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,
    // scope 0-全部 1-文件夹 2-文件
    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,
}

/// 搜索文件或文件夹 POST /api/fs/search
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(&params)
        .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,
}

/// 获取目录 POST /api/fs/dirs
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(&params)
        .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>,
}

/// 批量重命名 POST /api/fs/batch_rename
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(&params)
        .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>,
}

/// 正则重命名 POST /api/fs/regex_rename
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(&params)
        .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>,
}

/// 移动文件 POST /api/fs/move
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(&params)
        .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,
}

/// 聚合移动 POST /api/fs/recursive_move
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(&params)
        .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>,
}

/// 复制文件 POST /api/fs/copy
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(&params)
        .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>,
}

/// 删除文件或文件夹 POST /api/fs/remove
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(&params)
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    if resp.code != 200 {
        return Err(resp.message);
    }
    Ok(())
}

/// 删除空文件夹 POST /api/fs/remove_empty_directory
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>,
}

/// 添加aria2下载 POST /api/fs/add_aria2
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(&params)
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    if resp.code != 200 {
        return Err(resp.message);
    }
    Ok(())
}

/// 添加qBittorrent下载 POST /api/fs/add_qbit
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(&params)
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    if resp.code != 200 {
        return Err(resp.message);
    }
    Ok(())
}