Skip to content

Commit

Permalink
fix(filebrowser): don't allow viewing text files larger than 2MB
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenclaw900 committed Mar 28, 2022
1 parent 978be25 commit 63c8527
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 2 additions & 4 deletions src/backend/src/socket_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,7 @@ pub async fn file_handler(mut socket: warp::ws::WebSocket) {
match req.cmd.as_str() {
"open" => {
let _send = socket
.send(Message::text(
std::fs::read_to_string(std::path::Path::new(&req.path)).unwrap(),
))
.send(Message::text(std::fs::read_to_string(&req.path).unwrap()))
.await;
}
// Technically works for both files and directories
Expand Down Expand Up @@ -330,7 +328,7 @@ pub async fn file_handler(mut socket: warp::ws::WebSocket) {
upload_path = req.path;
upload_max_size = req.arg.parse::<usize>().unwrap();
}
"save" => std::fs::write(std::path::Path::new(&req.path), &req.arg).unwrap(),
"save" => std::fs::write(&req.path, &req.arg).unwrap(),
_ => {}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/backend/src/systemdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,12 @@ pub fn browser_dir(path: &std::path::Path) -> Vec<shared::BrowserDirData> {
subtype = "unknown".to_string();
prettytype = "Binary file".to_string();
} else {
if metadata.len() > 2 * 1000 * 1000 {
subtype = "large".to_string()
} else {
subtype = "plain".to_string();
}
maintype = "text".to_string();
subtype = "plain".to_string();
prettytype = "Plain Text File".to_string();
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/frontend/src/pages/FileBrowser.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,17 @@
currentPath = contents.path;
break;
case "text":
fileSend(contents.path, "open", "");
if (
contents.subtype == "large" &&
confirm(
"Can't view files above 2MB, would you like to download instead?"
)
) {
fileSend(selPath.path, "dl", "");
downloading = true;
} else {
fileSend(contents.path, "open", "");
}
currentPath = contents.path;
break;
default:
Expand Down

0 comments on commit 63c8527

Please sign in to comment.