Skip to main content

baidu_netdisk_sdk/file/
mod.rs

1//! File management and query functionality
2//!
3//! Provides file and folder management functionality, organized into three sub-modules:
4//! - management: File/folder management operations (create, rename, delete, move, copy)
5//! - query: File/folder query operations (list, get info, get metadata)
6//! - category: File category search operations
7//!
8//! All operations are accessible through a unified FileClient interface.
9//!
10//! # Quick Start
11//!
12//! ```rust
13//! # use tokio::main;
14//! use baidu_netdisk_sdk::BaiduNetDiskClient;
15//!
16//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
17//! let client = BaiduNetDiskClient::builder().build()?;
18//! client.load_token_from_env()?;
19//!
20//! // List directory
21//! let files = client.file().list_directory("/").await?;
22//!
23//! // Create folder
24//! let folder = client.file().create_folder("/my_new_folder").await?;
25//!
26//! // Get file metadata for download
27//! let file_info = client.file().get_file_info("/myfile.txt").await?;
28//! if let Some(fs_id) = file_info.fs_id {
29//!     let meta = client.file().get_file_meta(fs_id).await?;
30//! }
31//! # Ok(())
32//! # }
33//! ```
34pub mod category;
35pub mod management;
36pub mod query;
37
38pub use category::{
39    BtListOptions, Category, CategoryCountOptions, CategorySearchOptions, DocumentListOptions,
40    ImageListOptions, VideoListOptions,
41};
42pub use management::{FolderCreateOptions, FolderInfo};
43pub use query::{
44    FileInfo, FileMeta, ListAllOptions, ListAllResult, ListOptions, SearchOptions,
45    SemanticSearchOptions,
46};
47
48use std::sync::Arc;
49
50use crate::client::TokenGetter;
51use crate::errors::NetDiskResult;
52use crate::http::HttpClient;
53use category::FileCategoryExt;
54use management::FileManagementExt;
55use query::FileQueryExt;
56
57/// Unified File Client
58///
59/// Provides access to all file operations through a single interface.
60/// Operations are implemented via extension traits in sub-modules.
61///
62/// # Examples
63///
64/// ```rust
65/// use baidu_netdisk_sdk::BaiduNetDiskClient;
66///
67/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
68/// let client = BaiduNetDiskClient::builder().build()?;
69/// client.load_token_from_env()?;
70///
71/// // List files in directory
72/// let files = client.file().list_directory("/").await?;
73///
74/// // Create a new folder
75/// let folder = client.file().create_folder("/test").await?;
76/// # Ok(())
77/// # }
78/// ```
79#[derive(Debug, Clone)]
80pub struct FileClient {
81    http_client: HttpClient,
82    token_getter: Arc<dyn TokenGetter>,
83}
84
85impl FileClient {
86    /// Create a new FileClient instance
87    ///
88    /// Usually you don't need to call this directly - use `BaiduNetDiskClient::file()` instead.
89    pub fn new(http_client: HttpClient, token_getter: Arc<dyn TokenGetter>) -> Self {
90        FileClient {
91            http_client,
92            token_getter,
93        }
94    }
95
96    /// Get a reference to the internal HTTP client
97    pub fn http_client(&self) -> &HttpClient {
98        &self.http_client
99    }
100
101    // ===== Query Operations =====
102
103    /// List directory contents with default options
104    pub async fn list_directory(&self, dir: &str) -> NetDiskResult<Vec<FileInfo>> {
105        FileQueryExt::list_directory(self, dir).await
106    }
107
108    /// List directory contents with custom options
109    pub async fn list_directory_with_options(
110        &self,
111        dir: &str,
112        options: ListOptions,
113    ) -> NetDiskResult<Vec<FileInfo>> {
114        FileQueryExt::list_directory_with_options(self, dir, options).await
115    }
116
117    /// List all files recursively with pagination
118    ///
119    /// This method defaults to recursive mode and provides simple pagination.
120    /// For advanced options, use `list_all_with_options()` instead.
121    pub async fn list_all(&self, path: &str, start: i32, limit: i32) -> NetDiskResult<ListAllResult> {
122        FileQueryExt::list_all(self, path, start, limit).await
123    }
124
125    /// List all files recursively with custom options
126    pub async fn list_all_with_options(
127        &self,
128        path: &str,
129        options: ListAllOptions,
130    ) -> NetDiskResult<ListAllResult> {
131        FileQueryExt::list_all_with_options(self, path, options).await
132    }
133
134    /// Get file or folder information by path
135    pub async fn get_file_info(&self, path: &str) -> NetDiskResult<FileInfo> {
136        FileQueryExt::get_file_info(self, path).await
137    }
138
139    /// Get file metadata (including download link) by fs_id
140    pub async fn get_file_meta(&self, fs_id: u64) -> NetDiskResult<FileMeta> {
141        FileQueryExt::get_file_meta(self, fs_id).await
142    }
143
144    /// Search files by keyword
145    pub async fn search_files(&self, key: &str, dir: &str) -> NetDiskResult<(Vec<FileInfo>, bool)> {
146        FileQueryExt::search_files(self, key, dir).await
147    }
148
149    /// Search files by keyword with custom options
150    pub async fn search_files_with_options(
151        &self,
152        key: &str,
153        options: SearchOptions,
154    ) -> NetDiskResult<(Vec<FileInfo>, bool)> {
155        FileQueryExt::search_files_with_options(self, key, options).await
156    }
157
158    /// Semantic search for files (AI-powered search)
159    pub async fn semantic_search(&self, query: &str) -> NetDiskResult<Vec<FileInfo>> {
160        FileQueryExt::semantic_search(self, query).await
161    }
162
163    /// Semantic search with custom options
164    pub async fn semantic_search_with_options(
165        &self,
166        query: &str,
167        options: SemanticSearchOptions,
168    ) -> NetDiskResult<Vec<FileInfo>> {
169        FileQueryExt::semantic_search_with_options(self, query, options).await
170    }
171
172    // ===== Management Operations =====
173
174    /// Create a folder
175    pub async fn create_folder(&self, path: &str) -> NetDiskResult<FolderInfo> {
176        FileManagementExt::create_folder(self, path).await
177    }
178
179    /// Create a folder with custom options
180    pub async fn create_folder_with_options(
181        &self,
182        path: &str,
183        options: FolderCreateOptions,
184    ) -> NetDiskResult<FolderInfo> {
185        FileManagementExt::create_folder_with_options(self, path, options).await
186    }
187
188    /// Rename a file or folder
189    pub async fn rename(&self, path: &str, new_name: &str) -> NetDiskResult<()> {
190        FileManagementExt::rename(self, path, new_name).await
191    }
192
193    /// Delete a file or folder
194    pub async fn delete(&self, path: &str) -> NetDiskResult<()> {
195        FileManagementExt::delete(self, path).await
196    }
197
198    /// Move a file or folder
199    pub async fn move_file(&self, path: &str, dest: &str) -> NetDiskResult<()> {
200        FileManagementExt::move_file(self, path, dest).await
201    }
202
203    /// Copy a file or folder
204    pub async fn copy_file(&self, path: &str, dest: &str) -> NetDiskResult<()> {
205        FileManagementExt::copy_file(self, path, dest).await
206    }
207
208    // ===== Category Operations =====
209
210    /// Get the count of files in a specific category (simple version)
211    pub async fn get_category_file_count(&self, category: u32) -> NetDiskResult<u64> {
212        let token = self.token_getter.get_token().await?;
213        FileCategoryExt::get_category_file_count(self, &token, category).await
214    }
215
216    /// Get the count of files in a specific category (full parameters)
217    pub async fn get_category_file_count_with_options(
218        &self,
219        category: u32,
220        options: CategoryCountOptions,
221    ) -> NetDiskResult<u64> {
222        let token = self.token_getter.get_token().await?;
223        FileCategoryExt::get_category_file_count_with_options(self, &token, category, options).await
224    }
225
226    /// Search files in a specific category (simple version)
227    pub async fn search_category_files(
228        &self,
229        category: u32,
230        start: i32,
231        limit: i32,
232    ) -> NetDiskResult<(Vec<FileInfo>, u64)> {
233        let token = self.token_getter.get_token().await?;
234        FileCategoryExt::search_category_files(self, &token, category, start, limit).await
235    }
236
237    /// Search files in a specific category (full parameters)
238    pub async fn search_category_files_with_options(
239        &self,
240        category: &str,
241        options: CategorySearchOptions,
242    ) -> NetDiskResult<(Vec<FileInfo>, u64)> {
243        let token = self.token_getter.get_token().await?;
244        FileCategoryExt::search_category_files_with_options(self, &token, category, options).await
245    }
246
247    /// List documents
248    pub async fn list_documents(
249        &self,
250        parent_path: &str,
251        page: i32,
252        num: i32,
253    ) -> NetDiskResult<Vec<FileInfo>> {
254        let token = self.token_getter.get_token().await?;
255        FileCategoryExt::list_documents(self, &token, parent_path, page, num).await
256    }
257
258    /// List documents with custom options
259    pub async fn list_documents_with_options(
260        &self,
261        options: DocumentListOptions,
262    ) -> NetDiskResult<Vec<FileInfo>> {
263        let token = self.token_getter.get_token().await?;
264        FileCategoryExt::list_documents_with_options(self, &token, options).await
265    }
266
267    /// List images
268    pub async fn list_images(
269        &self,
270        parent_path: &str,
271        page: i32,
272        num: i32,
273    ) -> NetDiskResult<Vec<FileInfo>> {
274        let token = self.token_getter.get_token().await?;
275        FileCategoryExt::list_images(self, &token, parent_path, page, num).await
276    }
277
278    /// List images with custom options
279    pub async fn list_images_with_options(
280        &self,
281        options: ImageListOptions,
282    ) -> NetDiskResult<Vec<FileInfo>> {
283        let token = self.token_getter.get_token().await?;
284        FileCategoryExt::list_images_with_options(self, &token, options).await
285    }
286
287    /// List videos
288    pub async fn list_videos(
289        &self,
290        parent_path: &str,
291        page: i32,
292        num: i32,
293    ) -> NetDiskResult<Vec<FileInfo>> {
294        let token = self.token_getter.get_token().await?;
295        FileCategoryExt::list_videos(self, &token, parent_path, page, num).await
296    }
297
298    /// List videos with custom options
299    pub async fn list_videos_with_options(
300        &self,
301        options: VideoListOptions,
302    ) -> NetDiskResult<Vec<FileInfo>> {
303        let token = self.token_getter.get_token().await?;
304        FileCategoryExt::list_videos_with_options(self, &token, options).await
305    }
306
307    /// List torrents
308    pub async fn list_torrents(
309        &self,
310        parent_path: &str,
311        page: i32,
312        num: i32,
313    ) -> NetDiskResult<Vec<FileInfo>> {
314        let token = self.token_getter.get_token().await?;
315        FileCategoryExt::list_torrents(self, &token, parent_path, page, num).await
316    }
317
318    /// List torrents with custom options
319    pub async fn list_torrents_with_options(
320        &self,
321        options: BtListOptions,
322    ) -> NetDiskResult<Vec<FileInfo>> {
323        let token = self.token_getter.get_token().await?;
324        FileCategoryExt::list_torrents_with_options(self, &token, options).await
325    }
326}