Преглед изворни кода

Updated to v0.1.1, Cross-Platform & 'debug' feat

david пре 2 дана
родитељ
комит
22381e319e
5 измењених фајлова са 82 додато и 54 уклоњено
  1. 4 4
      Cargo.lock
  2. 8 4
      Cargo.toml
  3. 1 1
      LICENSE
  4. 6 0
      README.md
  5. 63 45
      src/main.rs

+ 4 - 4
Cargo.lock

@@ -1,6 +1,6 @@
 # This file is automatically @generated by Cargo.
 # It is not intended for manual editing.
-version = 3
+version = 4
 
 [[package]]
 name = "addr2line"
@@ -499,9 +499,9 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
 
 [[package]]
 name = "libm"
-version = "0.2.8"
+version = "0.2.13"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
+checksum = "c9627da5196e5d8ed0b0495e61e518847578da83483c37288316d9b2e03a7f72"
 
 [[package]]
 name = "linux-raw-sys"
@@ -984,7 +984,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
 
 [[package]]
 name = "selfserve"
-version = "0.1.0"
+version = "0.1.1"
 dependencies = [
  "humansize",
  "rocket",

+ 8 - 4
Cargo.toml

@@ -1,10 +1,14 @@
 [package]
 name = "selfserve"
-version = "0.1.0"
+version = "0.1.1"
 edition = "2021"
 
+[features]
+default = []
+debug = ["humansize", "serde", "serde_json"]
+
 [dependencies]
-humansize = "2.1.3"
+humansize = { version = "2.1.3", optional = true }
 rocket = { version = "0.5.1", features = ["json"] }
-serde = "1.0.203"
-serde_json = "1.0.117"
+serde = { version = "1.0.203", optional = true }
+serde_json = { version = "1.0.117", optional = true }

+ 1 - 1
LICENSE

@@ -1,5 +1,5 @@
 MIT License
-Copyright (c) 2024 David Thielemann (ApolloX939)
+Copyright (c) 2024-2025 David Thielemann (ApolloX939)
 
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 

+ 6 - 0
README.md

@@ -3,3 +3,9 @@
 Rust's Rocket set to self serve the files in the local directory
 
 Just give a port number via args (default 9000, i.e. `selfserve 9009` for port 9009)
+
+> Confirmed to now work cross platform (v0.1.1)
+
+## Debug Feature (v0.1.1)
+
+You can now enable the `debug` feature to gain access to `/ls/<path..>` (Gives a json payload of directories and files at given path, **warning**, it is unsafe as `/ls/../` would access the above directory, just as `/ls/~/` would as well, assuming a UNIX os)

+ 63 - 45
src/main.rs

@@ -1,8 +1,5 @@
-use rocket::{fs::FileServer, get, launch, routes, serde::json::{json, Value}};
-use std::{env, os::unix::fs::MetadataExt, path::PathBuf};
-use std::fs::read_dir;
-use humansize::{format_size, DECIMAL};
-
+use rocket::{fs::FileServer, launch, routes};
+use std::env;
 use rocket::http::Header;
 use rocket::{Request, Response};
 use rocket::fairing::{Fairing, Info, Kind};
@@ -29,51 +26,67 @@ impl Fairing for Cors {
     }
 }
 
-#[get("/ls/<path..>")]
-fn _list_dir(mut path: PathBuf) -> Value {
-    if path.to_string_lossy().is_empty() {
-        path = PathBuf::from(".");
-    }
-    let dir = read_dir(path);
-    match dir {
-        Ok(ls) => {
-            let mut v: Vec<Value> = Vec::new();
-            for elem in ls {
-                if elem.is_err() {
-                    continue;
-                }
-                let e = elem.unwrap();
-                let p = e.path();
-                let name = e.file_name().to_string_lossy().to_string();
-
-                if p.is_file() {
-                    let meta = e.metadata().unwrap();
-                    let size = meta.size();
-                    if name.starts_with(".") {
-                        v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL), "hidden": true}));
-                    } else {
-                        v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL)}));
-                    }
-                } else if p.is_symlink() {
-                    if name.starts_with(".") {
-                        v.push(json!({"name": name.clone(), "type": "LINK", "hidden": true}));
-                    } else {
-                        v.push(json!({"name": name.clone(), "type": "LINK"}));
+#[cfg(feature="debug")]
+mod debug_extras {
+    use rocket::{get, routes, serde::json::{json, Value}, Route};
+    use std::path::PathBuf;
+    use std::fs::read_dir;
+    use humansize::{format_size, DECIMAL};
+    #[get("/ls/<path..>")]
+    fn _list_dir(mut path: PathBuf) -> Value {
+        if path.to_string_lossy().is_empty() {
+            path = PathBuf::from(".");
+        }
+        let dir = read_dir(path);
+        match dir {
+            Ok(ls) => {
+                let mut v: Vec<Value> = Vec::new();
+                for elem in ls {
+                    if elem.is_err() {
+                        continue;
                     }
-                } else {
-                    if name.starts_with(".") {
-                        v.push(json!({"name": name.clone(), "type": "DIR", "hidden": true}))
+                    let e = elem.unwrap();
+                    let p = e.path();
+                    let name = e.file_name().to_string_lossy().to_string();
+
+                    if p.is_file() {
+                        let meta = e.metadata().unwrap();
+                        let size = meta.len();
+                        if name.starts_with(".") {
+                            v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL), "hidden": true}));
+                        } else {
+                            v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL)}));
+                        }
+                    } else if p.is_symlink() {
+                        if name.starts_with(".") {
+                            v.push(json!({"name": name.clone(), "type": "LINK", "hidden": true}));
+                        } else {
+                            v.push(json!({"name": name.clone(), "type": "LINK"}));
+                        }
                     } else {
-                        v.push(json!({"name": name.clone(), "type": "DIR"}))
+                        if name.starts_with(".") {
+                            v.push(json!({"name": name.clone(), "type": "DIR", "hidden": true}))
+                        } else {
+                            v.push(json!({"name": name.clone(), "type": "DIR"}))
+                        }
                     }
                 }
+                return json!({"status": 200, "dir": v});
+            }
+            Err(_) => {
+                return json!({"status": 404})
             }
-            return json!({"status": 200, "dir": v});
-        }
-        Err(_) => {
-            return json!({"status": 404})
         }
     }
+    pub fn extras(rock: &mut Vec<Route>) {
+        rock.append(&mut routes![_list_dir]);
+    }
+}
+
+#[cfg(not(feature="debug"))]
+mod debug_extras {
+    use rocket::Route;
+    pub fn extras(_rock: &mut Vec<Route>) {}
 }
 
 #[launch]
@@ -104,10 +117,15 @@ fn rocket() -> _ {
     let fig = rock.figment().clone()
         .merge((rocket::Config::PORT, port));
 
+    let mut r = routes![];
+    // This is a DEBUG only feature, it gives the option to list the given directory
+    if cfg!(feature="debug") {
+        debug_extras::extras(&mut r);
+    }
+
     rock
         .configure(fig)
         .attach(Cors)
         .mount("/", FileServer::from("."))
-        // This is a DEBUG only feature, it gives the option to list the given directory
-        //.mount("/", routes![_list_dir])
+        .mount("/", r)
 }