This repository was archived by the owner on Nov 21, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathrclone.rs
More file actions
406 lines (366 loc) · 11.7 KB
/
rclone.rs
File metadata and controls
406 lines (366 loc) · 11.7 KB
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
//! Structs and functions for use with Rclone RPC calls.
use crate::util;
use adw::glib;
use serde::Deserialize;
use serde_json::json;
use std::collections::HashMap;
use time::OffsetDateTime;
/// Get a remote from the config file.
pub fn get_remote<T: ToString>(remote: T) -> Option<Remote> {
let remote = remote.to_string();
let config_str = util::run_in_background(
glib::clone!(@strong remote => move || librclone::rpc("config/get", json!({
"name": remote
}).to_string()).unwrap()),
);
let config: HashMap<String, String> = serde_json::from_str(&config_str).unwrap();
match config["type"].as_str() {
"dropbox" => Some(Remote::Dropbox(DropboxRemote {
remote_name: remote,
client_id: config["client_id"].clone(),
client_secret: config["client_secret"].clone(),
})),
"drive" => Some(Remote::GDrive(GDriveRemote {
remote_name: remote,
client_id: config["client_id"].clone(),
client_secret: config["client_secret"].clone(),
})),
"onedrive" => Some(Remote::OneDrive(OneDriveRemote {
remote_name: remote,
client_id: config["client_id"].clone(),
client_secret: config["client_secret"].clone(),
})),
"pcloud" => Some(Remote::PCloud(PCloudRemote {
remote_name: remote,
client_id: config["client_id"].clone(),
client_secret: config["client_secret"].clone(),
})),
"protondrive" => Some(Remote::ProtonDrive(ProtonDriveRemote {
remote_name: remote,
username: config["username"].clone(),
})),
"webdav" => {
let vendor = match config["vendor"].as_str() {
"nextcloud" => WebDavVendors::Nextcloud,
"owncloud" => WebDavVendors::Owncloud,
"webdav" => WebDavVendors::WebDav,
_ => unreachable!(),
};
Some(Remote::WebDav(WebDavRemote {
remote_name: remote,
user: config["user"].clone(),
pass: config["pass"].clone(),
url: config["user"].clone(),
vendor,
}))
}
_ => None,
}
}
/// Get all the remotes from the config file.
pub fn get_remotes() -> Vec<Remote> {
let configs_str = util::run_in_background(move || {
librclone::rpc("config/listremotes", json!({}).to_string())
.unwrap_or_else(|_| unreachable!())
});
let configs = {
let config: HashMap<String, Vec<String>> = serde_json::from_str(&configs_str).unwrap();
config.get(&"remotes".to_string()).unwrap().to_owned()
};
let mut celeste_configs = vec![];
for config in configs {
celeste_configs.push(get_remote(&config).unwrap());
}
celeste_configs
}
/// The types of remotes in the config.
#[derive(Clone)]
pub enum Remote {
Dropbox(DropboxRemote),
GDrive(GDriveRemote),
OneDrive(OneDriveRemote),
PCloud(PCloudRemote),
ProtonDrive(ProtonDriveRemote),
WebDav(WebDavRemote),
}
impl Remote {
pub fn remote_name(&self) -> String {
match self {
Remote::Dropbox(remote) => remote.remote_name.clone(),
Remote::GDrive(remote) => remote.remote_name.clone(),
Remote::OneDrive(remote) => remote.remote_name.clone(),
Remote::PCloud(remote) => remote.remote_name.clone(),
Remote::ProtonDrive(remote) => remote.remote_name.clone(),
Remote::WebDav(remote) => remote.remote_name.clone(),
}
}
}
// The Dropbox remote type.
#[derive(Clone, Debug)]
pub struct DropboxRemote {
/// The name of the remote.
pub remote_name: String,
/// The client id.
pub client_id: String,
/// The client secret.
pub client_secret: String,
}
// The Google Drive remote type.
#[derive(Clone, Debug)]
pub struct GDriveRemote {
/// The name of the remote.
pub remote_name: String,
/// The client id.
pub client_id: String,
/// The client secret.
pub client_secret: String,
}
// The OneDrive remote type.
#[derive(Clone, Debug)]
pub struct OneDriveRemote {
/// The name of the remote.
pub remote_name: String,
/// The client id.
pub client_id: String,
/// The client secret.
pub client_secret: String,
}
// The pCloud remote type.
#[derive(Clone, Debug)]
pub struct PCloudRemote {
/// The name of the remote.
pub remote_name: String,
/// The client id.
pub client_id: String,
/// The client secret.
pub client_secret: String,
}
// The Proton Drive remote type.
#[derive(Clone, Debug)]
pub struct ProtonDriveRemote {
/// The name of the remote.
pub remote_name: String,
/// the username.
pub username: String,
}
// The WebDav remote type.
#[derive(Clone, Debug)]
pub struct WebDavRemote {
/// The name of the remote.
pub remote_name: String,
/// The username for the remote.
pub user: String,
/// The password for the remote.
pub pass: String,
/// The URL for the remote.
pub url: String,
/// The vendor of the remote.
pub vendor: WebDavVendors,
}
/// Possible WebDav vendors.
#[derive(Clone, Debug)]
pub enum WebDavVendors {
Nextcloud,
Owncloud,
GDrive,
PCloud,
WebDav,
}
impl ToString for WebDavVendors {
fn to_string(&self) -> String {
match self {
Self::Nextcloud => "Nextcloud",
Self::Owncloud => "Owncloud",
Self::GDrive => "Google Drive",
Self::PCloud => "pCloud",
Self::WebDav => "WebDav",
}
.to_string()
}
}
/// Error returned from Rclone.
#[derive(Clone, Deserialize, Debug)]
pub struct RcloneError {
pub error: String,
}
/// The output of an `operations/stat` command.
#[derive(Clone, Deserialize, Debug)]
pub struct RcloneStat {
item: Option<RcloneRemoteItem>,
}
/// The output of an `operations/list` command.
#[derive(Clone, Deserialize, Debug)]
pub struct RcloneList {
#[serde(rename = "list")]
list: Vec<RcloneRemoteItem>,
}
/// The list of items in a folder, from the `list` object in the output of the
/// `operations/list` command.
#[derive(Clone, Deserialize, Debug)]
pub struct RcloneRemoteItem {
#[serde(rename = "IsDir")]
pub is_dir: bool,
#[serde(rename = "Path")]
pub path: String,
#[serde(rename = "Name")]
pub name: String,
#[serde(rename = "ModTime", with = "time::serde::rfc3339")]
pub mod_time: OffsetDateTime,
}
/// The types of items to show in an `operations/list` command.
#[derive(Clone, Debug)]
pub enum RcloneListFilter {
/// Return all items.
All,
/// Only return directories.
Dirs,
/// Only return files.
#[allow(dead_code)]
Files,
}
/// Functions for syncing to a remote.
/// All functions in this module automatically run under
/// [`util::run_in_background`], so they don't need to be wrapped around
/// such to be ran during UI execution.
pub mod sync {
use super::{RcloneError, RcloneList, RcloneListFilter, RcloneRemoteItem, RcloneStat};
use crate::util;
use serde_json::json;
/// Get a remote name.
fn get_remote_name(remote: &str) -> String {
if remote.ends_with(':') {
panic!("Remote '{remote}' is not allowed to end with a ':'. Please omit it.",);
}
format!("{remote}:")
}
/// Run an Rclone command without blocking the GUI.
fn run<T: ToString>(method: T, input: T) -> Result<String, String> {
let method = method.to_string();
let input = input.to_string();
util::run_in_background(|| librclone::rpc(method, input))
}
/// Common function for some of the below command.
fn common(command: &str, remote_name: &str, path: &str) -> Result<(), RcloneError> {
let resp = run(
command,
&json!({
"fs": get_remote_name(remote_name),
"remote": util::strip_slashes(path),
})
.to_string(),
);
match resp {
Ok(_) => Ok(()),
Err(json_str) => Err(serde_json::from_str(&json_str).unwrap()),
}
}
/// Delete a config.
pub fn delete_config(remote_name: &str) -> Result<(), RcloneError> {
let resp = run("config/delete", &json!({ "name": remote_name }).to_string());
match resp {
Ok(_) => Ok(()),
Err(json_str) => Err(serde_json::from_str(&json_str).unwrap()),
}
}
/// Get statistics about a file or folder.
pub fn stat(remote_name: &str, path: &str) -> Result<Option<RcloneRemoteItem>, RcloneError> {
let resp = run(
"operations/stat",
&json!({
"fs": get_remote_name(remote_name),
"remote": util::strip_slashes(path)
})
.to_string(),
);
match resp {
Ok(json_str) => Ok(serde_json::from_str::<RcloneStat>(&json_str).unwrap().item),
Err(json_str) => Err(serde_json::from_str(&json_str).unwrap()),
}
}
/// List the files/folders in a path.
pub fn list(
remote_name: &str,
path: &str,
recursive: bool,
filter: RcloneListFilter,
) -> Result<Vec<RcloneRemoteItem>, RcloneError> {
let opts = match filter {
RcloneListFilter::All => json!({ "recurse": recursive }),
RcloneListFilter::Dirs => json!({"dirsOnly": true, "recurse": recursive}),
RcloneListFilter::Files => json!({"filesOnly": true, "recurse": recursive}),
};
let resp = run(
"operations/list",
&json!({
"fs": get_remote_name(remote_name),
"remote": util::strip_slashes(path),
"opt": opts
})
.to_string(),
);
match resp {
Ok(json_str) => Ok(serde_json::from_str::<RcloneList>(&json_str).unwrap().list),
Err(json_str) => Err(serde_json::from_str(&json_str).unwrap()),
}
}
/// make a directory on the remote.
pub fn mkdir(remote_name: &str, path: &str) -> Result<(), RcloneError> {
common("operations/mkdir", remote_name, path)
}
/// Delete a file.
pub fn delete(remote_name: &str, path: &str) -> Result<(), RcloneError> {
common("operations/delete", remote_name, path)
}
/// Remove a directory and all of its contents.
pub fn purge(remote_name: &str, path: &str) -> Result<(), RcloneError> {
common("operations/purge", remote_name, path)
}
/// Utility for copy functions.
fn copy(
src_fs: &str,
src_remote: &str,
dst_fs: &str,
dst_remote: &str,
) -> Result<(), RcloneError> {
let resp = run(
"operations/copyfile",
&json!({
"srcFs": src_fs,
"srcRemote": util::strip_slashes(src_remote),
"dstFs": dst_fs,
"dstRemote": util::strip_slashes(dst_remote)
})
.to_string(),
);
match resp {
Ok(_) => Ok(()),
Err(json_str) => Err(serde_json::from_str(&json_str).unwrap()),
}
}
/// Copy a file from the local machine to the remote.
pub fn copy_to_remote(
local_file: &str,
remote_name: &str,
remote_destination: &str,
) -> Result<(), RcloneError> {
copy(
"/",
local_file,
&get_remote_name(remote_name),
remote_destination,
)
}
/// Copy a file from the remote to the local machine.
pub fn copy_to_local(
local_destination: &str,
remote_name: &str,
remote_file: &str,
) -> Result<(), RcloneError> {
copy(
&get_remote_name(remote_name),
remote_file,
"/",
local_destination,
)
}
}