graph_fs/fs_module/
remote_fs.rs1use std::path::Path;
2
3use juniper::FieldResult;
4
5use crate::{
6 fs_module::{
7 graphql_write_access,
8 utils::{get_remote_file_list, get_remote_folder_list},
9 },
10 schema::{Context, File, Folder, Message},
11 utils::check_auth_path,
12};
13pub struct RemoteFsQuery;
14
15#[juniper::graphql_object(context = Context)]
16impl RemoteFsQuery {
17 #[graphql(description = "Returns a list of all files in directory")]
18 fn read_file_in_dir(context: &Context, path: String) -> FieldResult<Vec<File>> {
19 let path = Path::new(&path);
20 check_auth_path(path)?;
21 let sftp = context.sess.as_ref().unwrap().sftp()?;
22 Ok(get_remote_file_list(path, sftp)?)
23 }
24
25 #[graphql(description = "Returns a list of all directories in directory")]
26 fn read_dir_in_dir(context: &Context, path: String) -> FieldResult<Vec<Folder>> {
27 let path = Path::new(&path);
28 check_auth_path(path)?;
29 let sftp = context.sess.as_ref().unwrap().sftp()?;
30 Ok(get_remote_folder_list(path, &sftp)?)
31 }
32}
33
34pub struct RemoteFsMutation;
35
36#[juniper::graphql_object(context = Context)]
37impl RemoteFsMutation {
38 #[graphql(description = "Create file")]
39 async fn create_file(context: &Context, path: String) -> FieldResult<Message> {
40 if !graphql_write_access(context).await {
41 return Ok(Message::new(String::from(
42 "Unauthorized to perform write operation",
43 )));
44 }
45 let path = Path::new(&path);
46 check_auth_path(path)?;
47 let sftp = context.sess.as_ref().unwrap().sftp()?;
48 sftp.create(path)?;
49 let return_msg = format!("{} created successfully", path.to_str().unwrap());
50 Ok(Message::new(return_msg))
51 }
52
53 #[graphql(
54 description = "Create directory. Set mode optionally, would default to allow user read and write without sudo"
55 )]
56 async fn create_dir(
57 context: &Context,
58 path: String,
59 mode: Option<i32>,
60 ) -> FieldResult<Message> {
61 if !graphql_write_access(context).await {
62 return Ok(Message::new(String::from(
63 "Unauthorized to perform write operation",
64 )));
65 }
66 let path = Path::new(&path);
67 check_auth_path(path)?;
68 let sftp = context.sess.as_ref().unwrap().sftp()?;
69 sftp.mkdir(path, mode.unwrap_or(1000))?;
71 let return_msg = format!("{} created successfully", path.to_str().unwrap());
72 Ok(Message::new(return_msg))
73 }
74
75 #[graphql(description = "Delete a file")]
76 async fn delete_file(context: &Context, path: String) -> FieldResult<Message> {
77 if !graphql_write_access(context).await {
78 return Ok(Message::new(String::from(
79 "Unauthorized to perform write operation",
80 )));
81 }
82 let path = Path::new(&path);
83 check_auth_path(path)?;
84 let sftp = context.sess.as_ref().unwrap().sftp()?;
85 sftp.unlink(path)?;
86 let return_msg = format!("{} deleted successfully", path.to_str().unwrap());
87 Ok(Message::new(return_msg))
88 }
89
90 #[graphql(description = "Delete a folder")]
91 async fn delete_dir(context: &Context, path: String) -> FieldResult<Message> {
92 if !graphql_write_access(context).await {
93 return Ok(Message::new(String::from(
94 "Unauthorized to perform write operation",
95 )));
96 }
97 let path = Path::new(&path);
98 check_auth_path(path)?;
99 let sftp = context.sess.as_ref().unwrap().sftp()?;
100 sftp.rmdir(path)?;
101 let return_msg = format!("{} deleted successfully", path.to_str().unwrap());
102 Ok(Message::new(return_msg))
103 }
104
105 #[graphql(description = "Rename a file or folder, also used to move item")]
106 async fn rename_item(context: &Context, from: String, to: String) -> FieldResult<Message> {
107 if !graphql_write_access(context).await {
108 return Ok(Message::new(String::from(
109 "Unauthorized to perform write operation",
110 )));
111 }
112 let from_path = Path::new(&from);
113 check_auth_path(from_path)?;
114
115 let to_path = Path::new(&to);
116 check_auth_path(to_path)?;
117 let sftp = context.sess.as_ref().unwrap().sftp()?;
118 sftp.rename(from_path, to_path, None)?;
119 Ok(Message::new(String::from("Operation successful")))
120 }
121}