quarkdrive-webdav 1.3.6

A WebDAV client for quarkdrive
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
use std::collections::HashMap;

use serde::{Deserialize, Serialize, Deserializer};

/// Custom deserializer for file_name that HTML-unescapes the value
fn deserialize_file_name<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    Ok(htmlescape::decode_html(&s).unwrap_or(s))
}

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct QuarkFile {
    pub fid: String,
    #[serde(deserialize_with = "deserialize_file_name")]
    pub file_name: String,
    pub pdir_fid: String,
    #[serde(default)]
    pub size: u64,
    pub format_type: String,
    pub status: u8,
    pub created_at: u64,
    pub updated_at: u64,
    pub dir: bool,
    pub file: bool,
    pub download_url:Option<String>,
    pub content_hash: Option<String>,
    pub parent_path: Option<String>,
}


impl QuarkFile {
    pub fn new_root() -> Self {
        let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis() as u64;
        Self {
            pdir_fid: "".to_string(),
            size: 0u64,
            format_type: "".to_string(),
            parent_path: None,
            status: 1u8,
            created_at: now,
            updated_at: now,
            dir: true,
            file: false,
            file_name: "".to_string(),
            fid: "0".to_string(),
            download_url: None,
            content_hash: None,
        }
    }
}


#[derive(Debug, Serialize, Clone)]
pub struct GetFilesDownloadUrlsRequest {
    pub fids: Vec<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct GetFileItem {
    pub fid: String,
    #[serde(deserialize_with = "deserialize_file_name")]
    pub file_name: String,
    pub pdir_fid: String,
    pub category: u8,
    pub file_type: u8,
    #[serde(default)]
    pub size: u64,
    pub format_type: String,
    pub status: u8,
    pub tag: String,
    pub created_at: u64,
    pub updated_at: u64,
    pub dir: bool,
    pub file: bool,
}


#[derive(Debug, Serialize, Clone)]
pub struct DeleteFilesRequest {
    pub action_type: u8,
    pub exclude_fids: Vec<String>,
    pub filelist: Vec<String>,
}


#[derive(Debug, Serialize, Clone)]
pub struct CreateFolderRequest {
    pub pdir_fid: String,
    pub file_name: String,
    pub dir_path: String,
    pub dir_init_lock: bool,
}

#[derive(Debug, Serialize, Clone)]
pub struct RenameFileRequest {
    pub fid: String,
    pub file_name: String,
}

#[derive(Debug, Serialize, Clone)]
pub struct MoveFileRequest {
    pub filelist: Vec<String>,
    pub to_pdir_fid: String,
}

#[derive(Debug, Serialize, Clone)]
pub struct UpPreRequest {
    pub file_name: String,
    pub size: u64,
    pub pdir_fid: String,
    pub format_type: String,
    pub ccp_hash_update: bool,
    pub l_created_at: u64,
    pub l_updated_at: u64,
    pub parallel_upload: bool,
    pub dir_name: String,
}

#[derive(Debug, Serialize, Clone)]
pub struct UpHashRequest {
    pub md5: String,
    pub sha1: String,
    pub task_id: String,
}


#[derive(Debug, Serialize, Clone)]
pub struct AuthRequest {
    pub auth_info: String,
    pub auth_meta: String,
    pub task_id: String,
}

#[derive(Debug, Serialize, Clone)]
pub struct FinishRequest {
    pub obj_key: String,
    pub task_id: String,
}


pub struct UpPartMethodRequest {
    pub auth_key: String,
    pub mime_type: String,
    pub utc_time: String,
    pub bucket: String,
    pub upload_url: String,
    pub obj_key: String,
    pub part_number: u32,
    pub upload_id: String,
    pub part_bytes: Vec<u8>,
}


pub type GetFilesResponse = Response<FilesData, FilesMetadata>;

pub type GetFilesDownloadUrlsResponse = Response<Vec<FileDownloadUrlItem>, FileDownloadUrlMetadata>;

pub type DeleteFilesResponse = Response<DeleteFilesData, DeleteFilesMetadata>;

pub type CreateFolderResponse = Response<CreateFolderData, EmptyMetadata>;

pub type RenameFileResponse = Response<EmptyData, EmptyMetadata>;

pub type CommonResponse = Response<EmptyData, EmptyMetadata>;

pub type GetSpaceInfoResponse = Response<GetSpaceInfoResponseData, EmptyMetadata>;
pub type UpPreResponse = Response<UpPreResponseData, UpPreResponseMetaData>;

pub type UpHashResponse = Response<UpHashResponseData, EmptyMetadata>;

pub type AuthResponse = Response<AuthResponseData, EmptyMetadata>;

pub type FinishResponse = Response<EmptyData, EmptyMetadata>;


impl GetFilesDownloadUrlsResponse {
    pub fn into_map(self) -> HashMap<String, String> {
        self.data.into_iter().map(|item| (item.fid, item.download_url)).collect()
    }
}
#[derive(Debug, Clone, Deserialize)]
pub struct Response<T, U> {
    pub status: u8,
    pub code: u32,
    pub message: String,
    pub timestamp: u64,
    pub data: T,
    pub metadata: U,
}


#[derive(Debug, Clone, Deserialize)]
pub struct EmptyResponse {

}



#[derive(Debug, Clone, Deserialize)]
pub struct FilesData {
    pub list: Vec<QuarkFile>,

}

#[derive(Debug, Clone, Deserialize)]
pub struct FilesMetadata {
    #[serde(rename = "_total")]
    pub total: u32,
    #[serde(rename = "_count")]
    pub count: u32,
    #[serde(rename = "_page")]
    pub page: u32,

}

#[derive(Debug, Clone, Deserialize)]
pub struct DeleteFilesData {
    pub task_id: String,
    pub finish: bool,
}

#[derive(Debug, Clone, Deserialize)]
pub struct DeleteFilesMetadata {
    pub tq_gap: u32,
}


#[derive(Debug, Clone, Deserialize)]
pub struct CreateFolderData {
    pub finish: bool,
    pub fid: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct EmptyMetadata {

}

#[derive(Debug, Clone, Deserialize)]
pub struct EmptyData {

}


#[derive(Debug, Clone, Deserialize)]
pub struct QuarkFiles {
    pub list: Vec<QuarkFile>,
    pub total: u32,
}

#[derive(Debug, Clone, Deserialize)]
pub struct FileDownloadUrlItem {
    pub fid: String,
    pub download_url: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct FileDownloadUrlMetadata {

}
#[derive(Debug, Clone, Deserialize)]
pub struct GetSpaceInfoResponseData {
    pub total_capacity: u64,
    pub use_capacity: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct GetSpaceInfoResponseMetaData {

}

#[derive(Debug, Clone, Deserialize)]
pub struct AuthResponseData {
    pub auth_key: String,
}


#[derive(Debug, Clone, Deserialize)]
pub struct UpPreResponseData {
    pub finish: bool,
    pub task_id: String,
    pub upload_id: Option<String>,
    pub auth_info: String,
    pub upload_url: String,
    pub obj_key: String,
    pub fid: String,
    pub bucket: String,
    pub format_type: String,
    pub auth_info_expried: u64,
    pub callback: Callback,

}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpAuthAndCommitRequest {
    pub md5s: Vec<String>,
    pub callback: Callback,
    pub bucket: String,
    pub obj_key: String,
    pub upload_id: String,
    pub auth_info: String,
    pub task_id: String,
    pub upload_url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Callback {
    #[serde(rename = "callbackUrl")]
    pub callback_url: String,
    #[serde(rename = "callbackBody")]
    pub callback_body: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct UpPreResponseMetaData {
    pub part_size: u64,
    pub part_thread: u32
}

#[derive(Debug, Clone, Deserialize)]
pub struct UpHashResponseData {
    pub finish: bool,
}




impl From<GetFilesResponse> for QuarkFiles {
    fn from(response: GetFilesResponse) -> Self {
        QuarkFiles {
            list: response.data.list,
            total: response.metadata.total,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_quark_file_new_root() {
        let root = QuarkFile::new_root();
        assert_eq!(root.fid, "0");
        assert!(root.dir);
        assert!(!root.file);
        assert_eq!(root.size, 0);
        assert_eq!(root.pdir_fid, "");
        assert_eq!(root.file_name, "");
        assert!(root.created_at > 0);
        assert!(root.updated_at > 0);
    }

    #[test]
    fn test_quark_file_deserialize_basic() {
        let json = r#"{
            "fid": "abc123",
            "file_name": "test.txt",
            "pdir_fid": "0",
            "size": 1024,
            "format_type": "text/plain",
            "status": 1,
            "created_at": 1704067200000,
            "updated_at": 1704067200000,
            "dir": false,
            "file": true,
            "download_url": null,
            "content_hash": null,
            "parent_path": null
        }"#;
        let file: QuarkFile = serde_json::from_str(json).unwrap();
        assert_eq!(file.fid, "abc123");
        assert_eq!(file.file_name, "test.txt");
        assert_eq!(file.size, 1024);
        assert!(!file.dir);
        assert!(file.file);
    }

    #[test]
    fn test_quark_file_deserialize_html_entity() {
        let json = r#"{
            "fid": "abc123",
            "file_name": "test &amp; file &lt;1&gt;.txt",
            "pdir_fid": "0",
            "size": 0,
            "format_type": "",
            "status": 1,
            "created_at": 0,
            "updated_at": 0,
            "dir": false,
            "file": true,
            "download_url": null,
            "content_hash": null,
            "parent_path": null
        }"#;
        let file: QuarkFile = serde_json::from_str(json).unwrap();
        assert_eq!(file.file_name, "test & file <1>.txt");
    }

    #[test]
    fn test_get_files_download_urls_response_into_map() {
        let response = GetFilesDownloadUrlsResponse {
            status: 200,
            code: 0,
            message: "ok".to_string(),
            timestamp: 1704067200,
            data: vec![
                FileDownloadUrlItem {
                    fid: "fid1".to_string(),
                    download_url: "https://example.com/1".to_string(),
                },
                FileDownloadUrlItem {
                    fid: "fid2".to_string(),
                    download_url: "https://example.com/2".to_string(),
                },
            ],
            metadata: FileDownloadUrlMetadata {},
        };
        let map = response.into_map();
        assert_eq!(map.len(), 2);
        assert_eq!(map.get("fid1").unwrap(), "https://example.com/1");
        assert_eq!(map.get("fid2").unwrap(), "https://example.com/2");
    }

    #[test]
    fn test_get_files_download_urls_response_into_map_empty() {
        let response = GetFilesDownloadUrlsResponse {
            status: 200,
            code: 0,
            message: "ok".to_string(),
            timestamp: 0,
            data: vec![],
            metadata: FileDownloadUrlMetadata {},
        };
        let map = response.into_map();
        assert!(map.is_empty());
    }
}