main.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use rocket::{fs::FileServer, get, launch, routes, serde::json::{json, Value}};
  2. use std::{env, os::unix::fs::MetadataExt, path::PathBuf};
  3. use std::fs::read_dir;
  4. use humansize::{format_size, DECIMAL};
  5. use rocket::http::Header;
  6. use rocket::{Request, Response};
  7. use rocket::fairing::{Fairing, Info, Kind};
  8. pub struct Cors;
  9. #[rocket::async_trait]
  10. impl Fairing for Cors {
  11. fn info(&self) -> Info {
  12. Info {
  13. name: "Cross-Origin-Resource-Sharing Middleware",
  14. kind: Kind::Response,
  15. }
  16. }
  17. async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
  18. response.set_header(Header::new(
  19. "access-control-allow-origin",
  20. "http://127.0.0.1",
  21. ));
  22. response.set_header(Header::new(
  23. "access-control-allow-methods",
  24. "GET, PATCH, OPTIONS",
  25. ));
  26. }
  27. }
  28. #[get("/ls/<path..>")]
  29. fn _list_dir(mut path: PathBuf) -> Value {
  30. if path.to_string_lossy().is_empty() {
  31. path = PathBuf::from(".");
  32. }
  33. let dir = read_dir(path);
  34. match dir {
  35. Ok(ls) => {
  36. let mut v: Vec<Value> = Vec::new();
  37. for elem in ls {
  38. if elem.is_err() {
  39. continue;
  40. }
  41. let e = elem.unwrap();
  42. let p = e.path();
  43. let name = e.file_name().to_string_lossy().to_string();
  44. if p.is_file() {
  45. let meta = e.metadata().unwrap();
  46. let size = meta.size();
  47. if name.starts_with(".") {
  48. v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL), "hidden": true}));
  49. } else {
  50. v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL)}));
  51. }
  52. } else if p.is_symlink() {
  53. if name.starts_with(".") {
  54. v.push(json!({"name": name.clone(), "type": "LINK", "hidden": true}));
  55. } else {
  56. v.push(json!({"name": name.clone(), "type": "LINK"}));
  57. }
  58. } else {
  59. if name.starts_with(".") {
  60. v.push(json!({"name": name.clone(), "type": "DIR", "hidden": true}))
  61. } else {
  62. v.push(json!({"name": name.clone(), "type": "DIR"}))
  63. }
  64. }
  65. }
  66. return json!({"status": 200, "dir": v});
  67. }
  68. Err(_) => {
  69. return json!({"status": 404})
  70. }
  71. }
  72. }
  73. #[launch]
  74. fn rocket() -> _ {
  75. let mut args: Vec<String> = Vec::new();
  76. for a in env::args() {
  77. args.push(a);
  78. }
  79. let mut port: u16 = 9000;
  80. if args.len() != 1 {
  81. let mut index = 0;
  82. for a in args.iter() {
  83. let ptry: Result<u16, _> = a.parse();
  84. match ptry {
  85. Ok(val) => {
  86. port = val;
  87. break;
  88. }
  89. Err(err) => {
  90. println!("arg #{} : {}", index, err);
  91. }
  92. }
  93. index += 1;
  94. }
  95. }
  96. println!("Launching @ 127.0.0.1:{}", port);
  97. let rock = rocket::build();
  98. let fig = rock.figment().clone()
  99. .merge((rocket::Config::PORT, port));
  100. rock
  101. .configure(fig)
  102. .attach(Cors)
  103. .mount("/", FileServer::from("."))
  104. // This is a DEBUG only feature, it gives the option to list the given directory
  105. //.mount("/", routes![_list_dir])
  106. }