use lexical_sort for ordering

This commit is contained in:
jb-alvarado 2022-12-06 08:11:18 +01:00
parent ad4975bd88
commit 8f0060568c
3 changed files with 24 additions and 9 deletions

16
Cargo.lock generated
View File

@ -308,6 +308,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "any_ascii"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70033777eb8b5124a81a1889416543dddef2de240019b674c81285a2635a7e1e"
[[package]] [[package]]
name = "argon2" name = "argon2"
version = "0.4.1" version = "0.4.1"
@ -1006,6 +1012,7 @@ dependencies = [
"ffplayout-lib", "ffplayout-lib",
"futures-util", "futures-util",
"jsonwebtoken", "jsonwebtoken",
"lexical-sort",
"once_cell", "once_cell",
"rand", "rand",
"regex", "regex",
@ -1729,6 +1736,15 @@ dependencies = [
"socket2", "socket2",
] ]
[[package]]
name = "lexical-sort"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c09e4591611e231daf4d4c685a66cb0410cc1e502027a20ae55f2bb9e997207a"
dependencies = [
"any_ascii",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.137" version = "0.2.137"

View File

@ -21,6 +21,7 @@ derive_more = "0.99"
faccess = "0.2" faccess = "0.2"
futures-util = { version = "0.3", default-features = false, features = ["std"] } futures-util = { version = "0.3", default-features = false, features = ["std"] }
jsonwebtoken = "8" jsonwebtoken = "8"
lexical-sort = "0.3"
once_cell = "1.10" once_cell = "1.10"
rand = "0.8" rand = "0.8"
regex = "1" regex = "1"

View File

@ -3,6 +3,7 @@ use std::{fs, io::Write, path::PathBuf};
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use futures_util::TryStreamExt as _; use futures_util::TryStreamExt as _;
use lexical_sort::{natural_lexical_cmp, PathSort};
use rand::{distributions::Alphanumeric, Rng}; use rand::{distributions::Alphanumeric, Rng};
use relative_path::RelativePath; use relative_path::RelativePath;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -98,31 +99,28 @@ pub async fn browser(
let (path, parent, path_component) = norm_abs_path(&config.storage.path, &path_obj.source); let (path, parent, path_component) = norm_abs_path(&config.storage.path, &path_obj.source);
let mut obj = PathObject::new(path_component, Some(parent)); let mut obj = PathObject::new(path_component, Some(parent));
let mut paths: Vec<_> = match fs::read_dir(path) { let mut paths: Vec<PathBuf> = match fs::read_dir(path) {
Ok(p) => p.filter_map(|r| r.ok()).collect(), Ok(p) => p.filter_map(|r| r.ok()).map(|p| p.path()).collect(),
Err(e) => { Err(e) => {
error!("{e} in {}", path_obj.source); error!("{e} in {}", path_obj.source);
return Err(ServiceError::NoContent(e.to_string())); return Err(ServiceError::NoContent(e.to_string()));
} }
}; };
paths.sort_by_key(|dir| dir.path().display().to_string().to_lowercase()); paths.path_sort(natural_lexical_cmp);
let mut files = vec![]; let mut files = vec![];
let mut folders = vec![]; let mut folders = vec![];
for path in paths { for path in paths {
let file_path = path.path().to_owned();
let path = file_path.clone();
// ignore hidden files/folders on unix // ignore hidden files/folders on unix
if path.display().to_string().contains("/.") { if path.display().to_string().contains("/.") {
continue; continue;
} }
if file_path.is_dir() { if path.is_dir() {
folders.push(path.file_name().unwrap().to_string_lossy().to_string()); folders.push(path.file_name().unwrap().to_string_lossy().to_string());
} else if file_path.is_file() { } else if path.is_file() {
if let Some(ext) = file_extension(&file_path) { if let Some(ext) = file_extension(&path) {
if extensions.contains(&ext.to_string().to_lowercase()) { if extensions.contains(&ext.to_string().to_lowercase()) {
let media = MediaProbe::new(&path.display().to_string()); let media = MediaProbe::new(&path.display().to_string());
let mut duration = 0.0; let mut duration = 0.0;