cpanel-rs 0.3.0

An unofficial Rust client for the cPanel & WHM APIs.
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
//! File manager operations via cPanel UAPI.
//!
//! These methods operate on a per-user basis (cPanel port **2083**) and provide
//! file system operations similar to the cPanel File Manager interface.
//!
//! # Overview
//!
//! | Method | UAPI Call | Description |
//! |--------|-----------|-------------|
//! | [`CpanelClient::list_files`] | `Fileman.listdir` | List files/directories |
//! | [`CpanelClient::create_file`] | `Fileman.create_file` | Create a new file |
//! | [`CpanelClient::delete_file`] | `Fileman.delete` | Delete a file or directory |
//! | [`CpanelClient::rename_file`] | `Fileman.rename` | Rename/move a file |
//! | [`CpanelClient::copy_file`] | `Fileman.copy` | Copy a file |
//! | [`CpanelClient::chmod`] | `Fileman.chmod` | Change file permissions |
//! | [`CpanelClient::mkdir`] | `Fileman.mkdir` | Create a directory |
//! | [`CpanelClient::download_file`] | `Fileman.getfile` | Download a file's contents |
//! | [`CpanelClient::upload_file`] | `Fileman.uploadfile` | Upload a file |
//! | [`CpanelClient::compress`] | `Fileman.compress` | Create a zip archive |
//! | [`CpanelClient::extract`] | `Fileman.extract` | Extract a zip archive |
//!
//! # Path Conventions
//!
//! cPanel file paths are absolute from the home directory root:
//!
//! - Web root: `/public_html/` or `/home/username/public_html/`
//! - Documents root: `/documents/`
//! - FTP root: `/ftp/`
//! - Backup root: `/backup/`
//!
//! # Permissions
//!
//! The `chmod` method accepts standard Unix permission strings:
//!
//! | Mode | Meaning |
//! |------|---------|
//! | `0755` | Owner: rwx, Group: rx, Others: rx (standard for directories) |
//! | `0644` | Owner: rw, Group: r, Others: r (standard for files) |
//! | `0777` | All: rwx (not recommended) |
//! | `0600` | Owner: rw only (for sensitive files) |
//!
//! # Example
//!
//! ```no_run
//! use cpanel_rs::{CpanelClient, filemanager::{CreateFile, RenameFile, CompressFiles, CreateDir}};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), cpanel_rs::CpanelError> {
//!     let client = CpanelClient::simple("server.example.com", 2083, "cpanel_user", "pass").unwrap();
//!
//!     // List files in public_html
//!     let files = client.list_files("cpanel_user", "/public_html").await?;
//!     for f in &files {
//!         println!("{:<30} {:<5} {:<10}", f.name, f.type_, f.size.as_ref().unwrap_or(&"0".into()));
//!     }
//!
//!     // Create a file
//!     client.create_file("cpanel_user", &CreateFile {
//!         path: "/public_html/hello.txt".into(),
//!         content: Some("Hello, World!".into()),
//!         overwrite: false,
//!     }).await?;
//!
//!     // Upload a file
//!     let contents = b"<!DOCTYPE html><html><body>Hello</body></html>";
//!     client.upload_file("cpanel_user", "/public_html/index.html", contents, "index.html").await?;
//!
//!     // Download a file
//!     let data = client.download_file("cpanel_user", "/public_html/hello.txt").await?;
//!     println!("Downloaded {} bytes", data.len());
//!
//!     // Rename a file
//!     client.rename_file("cpanel_user", &RenameFile {
//!         source: "/public_html/old.txt".into(),
//!         destination: "/public_html/new.txt".into(),
//!     }).await?;
//!
//!     // Set permissions
//!     client.chmod("cpanel_user", "/public_html", "0755").await?;
//!
//!     // Create a directory
//!     client.mkdir("cpanel_user", &CreateDir {
//!         path: "/public_html/assets".into(),
//!     }).await?;
//!
//!     // Compress files
//!     client.compress("cpanel_user", &CompressFiles {
//!         paths: vec!["/public_html/wp-content/uploads".into()],
//!         archive_name: "uploads_backup.zip".into(),
//!     }).await?;
//!
//!     Ok(())
//! }
//! ```

use crate::{models::FileManagerEntry, CpanelClient, CpanelError};
use serde::Serialize;
use tokio::io::AsyncRead;
use tokio::io::AsyncReadExt as _;

/// Parameters for creating a file via [`create_file`](CpanelClient::create_file).
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateFile {
    /// Full path where the file should be created (e.g. `/public_html/foo.txt`).
    pub path: String,
    /// File content. If `None`, an empty file is created.
    pub content: Option<String>,
    /// Overwrite the file if it already exists.
    pub overwrite: bool,
}

/// Parameters for renaming a file via [`rename_file`](CpanelClient::rename_file).
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct RenameFile {
    /// Current full path.
    pub source: String,
    /// New full path.
    pub destination: String,
}

/// Parameters for creating a directory via [`mkdir`](CpanelClient::mkdir).
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateDir {
    /// Full path for the new directory.
    pub path: String,
}

/// Parameters for compressing files via [`compress`](CpanelClient::compress).
#[derive(Debug, Clone, Default, Serialize)]
pub struct CompressFiles {
    /// List of file/directory paths to compress.
    pub paths: Vec<String>,
    /// Name of the resulting archive (e.g. `"backup.zip"`).
    pub archive_name: String,
}

impl CpanelClient {
    /// Lists files and directories at the given path.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `path` — The directory path to list (e.g. `"/public_html"`).
    ///
    /// # Returns
    ///
    /// A vector of [`FileManagerEntry`] for each item in the directory.
    pub async fn list_files(
        &self,
        user: &str,
        path: &str,
    ) -> Result<Vec<FileManagerEntry>, CpanelError> {
        let result: Vec<FileManagerEntry> = self
            .uapi("Fileman", "listdir", &[("path", path), ("user", user)])
            .await?;
        Ok(result)
    }

    /// Creates a new file with the given content.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `create` — A [`CreateFile`] with the path, content, and overwrite flag.
    pub async fn create_file(&self, user: &str, create: &CreateFile) -> Result<(), CpanelError> {
        let overwrite_str = create.overwrite.to_string();
        let mut params: Vec<(&str, &str)> = vec![
            ("path", &create.path),
            ("user", user),
            ("overwrite", &overwrite_str),
        ];

        if let Some(content) = &create.content {
            params.push(("content", content.as_str()));
        }

        let _: () = self.uapi("Fileman", "create_file", &params).await?;
        Ok(())
    }

    /// Deletes a file or directory.
    ///
    /// **Warning:** This operation is irreversible.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `path` — Full path to the file or directory to delete.
    pub async fn delete_file(&self, user: &str, path: &str) -> Result<(), CpanelError> {
        let _: () = self
            .uapi("Fileman", "delete", &[("path", path), ("user", user)])
            .await?;
        Ok(())
    }

    /// Renames or moves a file/directory.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `rename` — A [`RenameFile`] with source and destination paths.
    pub async fn rename_file(&self, user: &str, rename: &RenameFile) -> Result<(), CpanelError> {
        let _: () = self
            .uapi(
                "Fileman",
                "rename",
                &[
                    ("path", &rename.source),
                    ("newpath", &rename.destination),
                    ("user", user),
                ],
            )
            .await?;
        Ok(())
    }

    /// Copies a file to a new location.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `source` — Full path of the file to copy.
    /// * `destination` — Full path for the copy.
    pub async fn copy_file(
        &self,
        user: &str,
        source: &str,
        destination: &str,
    ) -> Result<(), CpanelError> {
        let _: () = self
            .uapi(
                "Fileman",
                "copy",
                &[("source", source), ("dest", destination), ("user", user)],
            )
            .await?;
        Ok(())
    }

    /// Changes file permissions (chmod).
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `path` — Full path to the file or directory.
    /// * `mode` — Unix permission mode (e.g. `"0755"`, `"0644"`).
    pub async fn chmod(&self, user: &str, path: &str, mode: &str) -> Result<(), CpanelError> {
        let _: () = self
            .uapi(
                "Fileman",
                "chmod",
                &[("path", path), ("mode", mode), ("user", user)],
            )
            .await?;
        Ok(())
    }

    /// Creates a new directory.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `create` — A [`CreateDir`] with the directory path.
    pub async fn mkdir(&self, user: &str, create: &CreateDir) -> Result<(), CpanelError> {
        let _: () = self
            .uapi(
                "Fileman",
                "mkdir",
                &[("path", &create.path), ("user", user)],
            )
            .await?;
        Ok(())
    }

    /// Downloads a file from the cPanel account.
    ///
    /// Returns the raw file contents as a byte vector.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `path` — Full path to the file to download.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use cpanel_rs::CpanelClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), cpanel_rs::CpanelError> {
    ///     let client = CpanelClient::simple("server.example.com", 2083, "user", "pass").unwrap();
    ///     let data = client.download_file("user", "/public_html/config.json").await?;
    ///     println!("Downloaded {} bytes", data.len());
    ///     Ok(())
    /// }
    /// ```
    pub async fn download_file(&self, user: &str, path: &str) -> Result<Vec<u8>, CpanelError> {
        let url = format!(
            "{}/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_func=getfile&user={}&path={}",
            self.base_url(),
            urlencoding::encode(user),
            urlencoding::encode(path),
        );

        let response = self
            .http_client()
            .get(&url)
            .headers(self.auth_headers())
            .send()
            .await?;

        Ok(response.bytes().await?.to_vec())
    }

    /// Uploads a file to the cPanel account from an async reader.
    ///
    /// Reads the source in chunks and streams them to the server, avoiding
    /// loading the entire file into memory.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `path` — Full path where the file should be saved.
    /// * `reader` — A type implementing [`tokio::io::AsyncRead`].
    /// * `filename` — The filename to use on the server.
    /// * `chunk_size` — Maximum bytes per multipart part (default: 1 MB).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use cpanel_rs::CpanelClient;
    /// use tokio::fs::File;
    /// use tokio::io::AsyncReadExt;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), cpanel_rs::CpanelError> {
    ///     let client = CpanelClient::simple("server.example.com", 2083, "user", "pass").unwrap();
    ///     let mut file = File::open("large_upload.zip").await?;
    ///     client.upload_file_from_reader("user", "/public_html/large_upload.zip", &mut file, "large_upload.zip", 1024 * 1024).await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn upload_file_from_reader<R: AsyncRead + Unpin>(
        &self,
        user: &str,
        path: &str,
        reader: &mut R,
        filename: &str,
        _chunk_size: usize,
    ) -> Result<(), CpanelError> {
        let path_owned = path.to_string();
        let filename_owned = filename.to_string();
        let url = format!(
            "{}/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_func=uploadfile&user={}",
            self.base_url(),
            urlencoding::encode(user),
        );

        // Read all chunks first — cPanel's upload API expects a single multipart part.
        // For truly large files, consider using direct HTTP upload instead.
        let mut buf = Vec::new();
        reader.read_to_end(&mut buf).await?;

        let form = reqwest::multipart::Form::new()
            .part("path", reqwest::multipart::Part::text(path_owned))
            .part(
                "file",
                reqwest::multipart::Part::bytes(buf).file_name(filename_owned),
            );

        let response = self
            .http_client()
            .post(&url)
            .headers(self.auth_headers())
            .multipart(form)
            .send()
            .await?;

        let text: String = response.text().await?;
        let api_resp: crate::types::ApiResponse<serde_json::Value> =
            serde_json::from_str(&text).map_err(CpanelError::Serialization)?;

        if !api_resp.is_ok() {
            return Err(CpanelError::ApiError(crate::types::ApiFailure {
                message: api_resp.status_msg,
                call: "Fileman.uploadfile".into(),
                ..Default::default()
            }));
        }

        Ok(())
    }

    /// Uploads a file to the cPanel account.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `path` — Full path where the file should be saved.
    /// * `content` — The raw file contents.
    /// * `filename` — The filename to use on the server.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use cpanel_rs::CpanelClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), cpanel_rs::CpanelError> {
    ///     let client = CpanelClient::simple("server.example.com", 2083, "user", "pass").unwrap();
    ///
    ///     let html = b"<!DOCTYPE html><html><body>Hello World</body></html>";
    ///     client.upload_file("user", "/public_html/index.html", html, "index.html").await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn upload_file(
        &self,
        user: &str,
        path: &str,
        content: &[u8],
        filename: &str,
    ) -> Result<(), CpanelError> {
        let path_owned = path.to_string();
        let filename_owned = filename.to_string();
        let form = reqwest::multipart::Form::new()
            .part("path", reqwest::multipart::Part::text(path_owned))
            .part(
                "file",
                reqwest::multipart::Part::bytes(content.to_vec()).file_name(filename_owned),
            );

        let url = format!(
            "{}/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_func=uploadfile&user={}",
            self.base_url(),
            urlencoding::encode(user),
        );

        let response = self
            .http_client()
            .post(&url)
            .headers(self.auth_headers())
            .multipart(form)
            .send()
            .await?;

        let text: String = response.text().await?;
        let api_resp: crate::types::ApiResponse<serde_json::Value> =
            serde_json::from_str(&text).map_err(CpanelError::Serialization)?;

        if !api_resp.is_ok() {
            return Err(CpanelError::ApiError(crate::types::ApiFailure {
                message: api_resp.status_msg,
                call: "Fileman.uploadfile".into(),
                ..Default::default()
            }));
        }

        Ok(())
    }

    /// Creates a zip archive from the given file paths.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `compress` — A [`CompressFiles`] with paths and archive name.
    pub async fn compress(&self, user: &str, compress: &CompressFiles) -> Result<(), CpanelError> {
        let paths_json =
            serde_json::to_string(&compress.paths).map_err(CpanelError::Serialization)?;

        let _: () = self
            .uapi(
                "Fileman",
                "compress",
                &[
                    ("paths", &paths_json),
                    ("name", &compress.archive_name),
                    ("user", user),
                ],
            )
            .await?;
        Ok(())
    }

    /// Extracts a zip archive to the destination path.
    ///
    /// # Arguments
    ///
    /// * `user` — The cPanel username.
    /// * `archive_path` — Full path to the zip archive.
    /// * `dest_path` — Full path to extract into.
    pub async fn extract(
        &self,
        user: &str,
        archive_path: &str,
        dest_path: &str,
    ) -> Result<(), CpanelError> {
        let _: () = self
            .uapi(
                "Fileman",
                "extract",
                &[
                    ("path", archive_path),
                    ("destpath", dest_path),
                    ("user", user),
                ],
            )
            .await?;
        Ok(())
    }
}