Skip to content

Commit

Permalink
add windows manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleailes committed Jul 20, 2023
1 parent 8b7c251 commit 208cbf2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 58 deletions.
38 changes: 23 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use framels::{basic_listing, paths::Paths};
use glob::glob;
use jwalk::WalkDir;
use rayon::prelude::*;
use rocket::serde::{json::Json, Deserialize, Serialize};
use rocket_okapi::okapi::schemars;
use rocket_okapi::okapi::schemars::JsonSchema;
use std::fs;
use rayon::prelude::*;

/// get_walk is a function to walk the content of a directory and his
/// subfolders
Expand Down Expand Up @@ -51,19 +51,6 @@ fn test_get_glob() {
assert_eq!(5, get_glob("./samples/*.tif").len());
assert_eq!(3, get_glob("./samples/aaa.00[1-3].tif").len());
}
///Basic function to translate a Windows path to Unix
/// expecting a full path starting at a root level entry
pub fn from_slash(s: String) -> String {
let temp_str = str::replace(&s, "\\\\", "\\");
str::replace(&temp_str, "\\", "/")
}
#[test]
fn test_from_slash() {
assert_eq!(
"/caroline/bank/",
from_slash("\\\\caroline\\bank\\".to_string())
)
}

#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(crate = "rocket::serde")]
Expand All @@ -77,10 +64,16 @@ impl InputPath {
pub fn new(s: String) -> InputPath {
InputPath { input_path: s }
}
///Basic function to translate a Windows path to Unix
/// expecting a full path starting at a root level entry
fn from_slash(s: &String) -> String {
let temp_str = str::replace(&s, "\\\\", "\\");
str::replace(&temp_str, "\\", "/")
}
/// convert an InputPath in Windows format to a linux format
pub fn convert_to_unix(input: Json<InputPath>) -> Json<InputPath> {
let path = InputPath {
input_path: from_slash(input.input_path.to_string()),
input_path: InputPath::from_slash(&input.input_path.to_string()),
};
Json::from(path)
}
Expand Down Expand Up @@ -112,15 +105,30 @@ impl QuietPaths {
paths_list: get_walk(input_path.input_path.as_str()),
}
}
/// Return a Paths struct from a QuietPaths
pub fn to_paths(&self) -> Paths {
Paths::new(self.paths_list.clone())
}
/// return a QuietPaths struct form a Paths
pub fn from_paths(paths: Paths) -> Self {
QuietPaths {
paths_list: paths.to_vec(),
}
}
/// Return a packed QuietPaths struct using `framels` lib
pub fn packed(&self) -> Self {
QuietPaths::from_paths(basic_listing(self.to_paths()).get_paths())
}
/// Convert a QuietPaths using "/" to paths using "\\"
pub fn convert_windows(&mut self) {
let new_paths_list = self
.paths_list
.par_iter()
.map(|x| QuietPaths::to_slash(x))
.collect::<Vec<String>>();
self.paths_list = new_paths_list;
}
fn to_slash(s: &String) -> String {
str::replace(&s, "/", "\\")
}
}
116 changes: 74 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,30 @@ fn index() -> &'static str {
fn coffee() -> status::Custom<content::RawJson<&'static str>> {
status::Custom(Status::ImATeapot, content::RawJson("{ \"hi\": \"world\" }"))
}
fn commun_manipulations(
input_paths: QuietPaths,
packed: Option<bool>,
windows: Option<bool>,
) -> Json<QuietPaths> {
let mut path_packed = if packed.unwrap_or(false) {
input_paths.packed()
} else {
input_paths
};
let path_windows = if windows.unwrap_or(false) {
path_packed.convert_windows();
path_packed
} else {
path_packed
};
Json(path_windows)
}
#[openapi(tag = "FileSystem")]
#[post("/walk?<packed>", format = "application/json", data = "<input_path>")]
#[post(
"/walk?<packed>&<windows>",
format = "application/json",
data = "<input_path>"
)]
/// # walk
///
/// ## Description
Expand All @@ -46,32 +68,30 @@ fn coffee() -> status::Custom<content::RawJson<&'static str>> {
/// ## Tips
///
/// It is recommanded to use path with slash `/` instead of backslash `\`
///
///
/// ## Parameters
///
///
/// ### packed
///
/// You can use a filter `packed=true` to pack frame sequences
///
/// You can use a filter `packed=true` or `packed=true` to pack frame sequences
fn fwalk(input_path: Json<InputPath>, packed: Option<bool>) -> Json<QuietPaths> {
let input_path: QuietPaths = QuietPaths::from_walk(input_path);
if packed.unwrap_or(false) {
Json(input_path.packed())
} else {
Json(input_path)
}
}
#[openapi(tag = "FileSystem")]
#[post("/walk/<n>", format = "application/json", data = "<input_path>")]
fn fwalk_os(input_path: Json<InputPath>, n: String) -> Json<QuietPaths> {
if n == "windows" {
let path = InputPath::convert_to_unix(input_path);
Json(QuietPaths::from_walk(path))
} else {
Json(QuietPaths::from_walk(input_path))
}
/// ### windows
///
/// You can use a filter `windows=true` to force export path to use `\\`
/// instead of `/`
fn fwalk(
input_path: Json<InputPath>,
packed: Option<bool>,
windows: Option<bool>,
) -> Json<QuietPaths> {
commun_manipulations(QuietPaths::from_walk(input_path), packed, windows)
}
#[openapi(tag = "FileSystem")]
#[post("/listdir?<packed>", format = "application/json", data = "<input_path>")]
#[post(
"/listdir?<packed>&<windows>",
format = "application/json",
data = "<input_path>"
)]
/// # listdir
///
/// ## Description
Expand All @@ -83,20 +103,28 @@ fn fwalk_os(input_path: Json<InputPath>, n: String) -> Json<QuietPaths> {
/// It is recommanded to use path with slash `/` instead of backslash `\`
///
/// ## Parameters
///
///
/// ### packed
///
/// You can use a filter `packed=true` to pack frame sequences
///
/// ### windows
///
/// You can use a filter `packed=true` or `packed=true` to pack frame sequences
fn flistdir(input_path: Json<InputPath>, packed: Option<bool>) -> Json<QuietPaths> {
let input_path: QuietPaths = QuietPaths::from_listdir(input_path);
if packed.unwrap_or(false) {
Json(input_path.packed())
} else {
Json(input_path)
}
/// You can use a filter `windows=true` to force export path to use `\\`
/// instead of `/`
fn flistdir(
input_path: Json<InputPath>,
packed: Option<bool>,
windows: Option<bool>,
) -> Json<QuietPaths> {
commun_manipulations(QuietPaths::from_listdir(input_path), packed, windows)
}
#[openapi(tag = "FileSystem")]
#[post("/glob?<packed>", format = "application/json", data = "<input_path>")]
#[post(
"/glob?<packed>&<windows>",
format = "application/json",
data = "<input_path>"
)]
/// # glob
///
/// ## Description
Expand All @@ -108,25 +136,29 @@ fn flistdir(input_path: Json<InputPath>, packed: Option<bool>) -> Json<QuietPath
/// It is recommanded to use path with slash `/` instead of backslash `\`
///
/// ## Parameters
///
///
/// ### packed
///
/// You can use a filter `packed=true` to pack frame sequences
///
/// You can use a filter `packed=true` or `packed=true` to pack frame sequences
fn fglob(input_path: Json<InputPath>, packed: Option<bool>) -> Json<QuietPaths> {
let input_path: QuietPaths = QuietPaths::from_glob(input_path);
if packed.unwrap_or(false) {
Json(input_path.packed())
} else {
Json(input_path)
}
/// ### windows
///
/// You can use a filter `windows=true` to force export path to use `\\`
/// instead of `/`
fn fglob(
input_path: Json<InputPath>,
packed: Option<bool>,
windows: Option<bool>,
) -> Json<QuietPaths> {
commun_manipulations(QuietPaths::from_glob(input_path), packed, windows)
}

#[launch]
fn rocket() -> _ {
rocket::build()
.mount(
"/",
openapi_get_routes![index, flistdir, fglob, fwalk, fwalk_os, coffee],
openapi_get_routes![index, flistdir, fglob, fwalk, coffee],
)
.mount(
"/docs/",
Expand Down
5 changes: 4 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ fn test_listdir() {
fn test_listdir_packed() {
let message = InputPath::new("./samples/".to_string());
let client = Client::tracked(rocket()).expect("valid rocket instance");
let response = client.post("/listdir?packed=true").json(&message).dispatch();
let response = client
.post("/listdir?packed=true")
.json(&message)
.dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_string().unwrap().replace("\\\\", "/"),"{\"paths_list\":[\"./samples/aaa.***.tif@1-5\",\"./samples/bbb.***.exr@1\",\"./samples/subfolder\"]}");
}
Expand Down

0 comments on commit 208cbf2

Please sign in to comment.