sanitize-filename-0.6.0/.cargo_vcs_info.json0000644000000001360000000000100144430ustar { "git": { "sha1": "9428a9485243e0ab45206246e8fa1e9d42256d8a" }, "path_in_vcs": "" }sanitize-filename-0.6.0/.gitignore000064400000000000000000000000231046102023000152160ustar 00000000000000/target **/*.rs.bk sanitize-filename-0.6.0/Cargo.lock0000644000000024710000000000100124220ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "aho-corasick" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" dependencies = [ "memchr", ] [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "regex" version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "sanitize-filename" version = "0.6.0" dependencies = [ "regex", ] sanitize-filename-0.6.0/Cargo.toml0000644000000021700000000000100124410ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.70.0" name = "sanitize-filename" version = "0.6.0" authors = ["Jacob Brown "] build = false autobins = false autoexamples = false autotests = false autobenches = false description = "A simple filename sanitizer, based on Node's sanitize-filename" readme = "README.md" keywords = [ "filename", "sanitizer", ] license = "MIT" repository = "https://github.com/kardeiz/sanitize-filename" [lib] name = "sanitize_filename" path = "src/lib.rs" [[bin]] name = "sanitize-filename" path = "src/main.rs" [dependencies.regex] version = "1.11" features = [ "std", "unicode-case", ] default-features = false sanitize-filename-0.6.0/Cargo.toml.orig000064400000000000000000000007521046102023000161260ustar 00000000000000[package] authors = ["Jacob Brown "] name = "sanitize-filename" version = "0.6.0" keywords = ["filename", "sanitizer"] license = "MIT" readme = "README.md" edition = "2021" rust-version = "1.70.0" repository = "https://github.com/kardeiz/sanitize-filename" description = "A simple filename sanitizer, based on Node's sanitize-filename" [dependencies] regex = { version = "1.11", default-features = false, features = [ "std", "unicode-case", ] } sanitize-filename-0.6.0/LICENSE000064400000000000000000000021011046102023000142320ustar 00000000000000Copyright (c) 2024 Sanitize Filename contributors 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sanitize-filename-0.6.0/README.md000064400000000000000000000022621046102023000145140ustar 00000000000000# sanitize-filename A basic filename sanitizer, based on Node's [sanitize-filename](https://www.npmjs.com/package/sanitize-filename). Use like: ```rust extern crate sanitize_filename; fn main() { println!("{}", sanitize_filename::sanitize("some-user-defined/../../../string")); // prints some-user-defined......string } ``` You can also configure a few basic options: ```rust let options = sanitize_filename::Options { truncate: true, // true by default, truncates to 255 bytes windows: true, // default value depends on the OS, removes reserved names like `con` from start of strings on Windows replacement: "" // str to replace sanitized chars/strings }; let sanitized = sanitize_filename::sanitize_with_options("some-user-defined/../../../string", options); ``` Also provides a basic command line binary. Use like: ```bash cargo install sanitize-filename sanitize-filename my_filename.txt ``` ``` Pass a file name to clean to the program (also reads STDIN) FLAGS: -r, --replace Replacement characters --windows, --no-windows Whether to handle filenames for Windows --truncate, --no-truncate Whether to truncate file names to 255 bytes ``` sanitize-filename-0.6.0/src/lib.rs000064400000000000000000000164651046102023000151520ustar 00000000000000use std::sync::OnceLock; extern crate regex; use regex::{Regex, RegexBuilder}; static ILLEGAL_RE: OnceLock = OnceLock::new(); static CONTROL_RE: OnceLock = OnceLock::new(); static RESERVED_RE: OnceLock = OnceLock::new(); static WINDOWS_RESERVED_RE: OnceLock = OnceLock::new(); static WINDOWS_TRAILING_RE: OnceLock = OnceLock::new(); fn illegal_re() -> &'static Regex { ILLEGAL_RE.get_or_init(|| Regex::new(r#"[/\?<>\\:\*\|":]"#).unwrap()) } fn control_re() -> &'static Regex { CONTROL_RE.get_or_init(|| Regex::new(r#"[\x00-\x1f\x80-\x9f]"#).unwrap()) } fn reserved_re() -> &'static Regex { RESERVED_RE.get_or_init(|| Regex::new(r#"^\.+$"#).unwrap()) } fn windows_reserved_re() -> &'static Regex { WINDOWS_RESERVED_RE.get_or_init(|| { RegexBuilder::new(r#"(?i)^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$"#) .case_insensitive(true) .build() .unwrap() }) } fn windows_trailing_re() -> &'static Regex { WINDOWS_TRAILING_RE.get_or_init(|| Regex::new(r#"[\. ]+$"#).unwrap()) } #[derive(Clone)] pub struct Options<'a> { pub windows: bool, pub truncate: bool, pub replacement: &'a str, } impl<'a> Default for Options<'a> { fn default() -> Self { Options { windows: cfg!(windows), truncate: true, replacement: "", } } } pub fn sanitize>(name: S) -> String { sanitize_with_options(name, Options::default()) } pub fn sanitize_with_options>(name: S, options: Options) -> String { let Options { windows, truncate, replacement, } = options; let name = name.as_ref(); let name = illegal_re().replace_all(&name, replacement); let name = control_re().replace_all(&name, replacement); let name = reserved_re().replace(&name, replacement); let collect = |name: ::std::borrow::Cow| { if truncate && name.len() > 255 { let mut end = 255; loop { if name.is_char_boundary(end) { break; } end -= 1; } String::from(&name[..end]) } else { String::from(name) } }; if windows { let name = windows_reserved_re().replace(&name, replacement); let name = windows_trailing_re().replace(&name, replacement); collect(name) } else { collect(name) } } #[derive(Clone)] pub struct OptionsForCheck { pub windows: bool, pub truncate: bool, } impl Default for OptionsForCheck { fn default() -> Self { OptionsForCheck { windows: cfg!(windows), truncate: true, } } } pub fn is_sanitized>(name: S) -> bool { is_sanitized_with_options(name, OptionsForCheck::default()) } pub fn is_sanitized_with_options>(name: S, options: OptionsForCheck) -> bool { let OptionsForCheck { windows, truncate } = options; let name = name.as_ref(); if illegal_re().is_match(&name) { return false; } if control_re().is_match(&name) { return false; } if reserved_re().is_match(&name) { return false; } if truncate && name.len() > 255 { return false; } if windows { if windows_reserved_re().is_match(&name) { return false; } if windows_trailing_re().is_match(&name) { return false; } } return true; } #[cfg(test)] mod tests { // From https://github.com/parshap/node-sanitize-filename/blob/master/test.js static NAMES: &'static [&'static str] = &[ "the quick brown fox jumped over the lazy dog", "résumé", "hello\u{0000}world", "hello\nworld", "semi;colon.js", ";leading-semi.js", "slash\\.js", "slash/.js", "col:on.js", "star*.js", "question?.js", "quote\".js", "singlequote'.js", "brackts.js", "p|pes.js", "plus+.js", "'five and six(); let shorter = ::std::iter::repeat('a').take(255).collect::(); assert_eq!(super::sanitize_with_options(long, options.clone()), shorter); // is_sanitized let options = super::OptionsForCheck { windows: true, truncate: true, }; for (idx, name) in NAMES.iter().enumerate() { assert_eq!( super::is_sanitized_with_options(name, options.clone()), NAMES_IS_SANITIZED[idx] ); } let long = ::std::iter::repeat('a').take(300).collect::(); assert_eq!( super::is_sanitized_with_options(long, options.clone()), false ); } } sanitize-filename-0.6.0/src/main.rs000064400000000000000000000030261046102023000153150ustar 00000000000000extern crate sanitize_filename; fn main() -> Result<(), ::std::io::Error> { let mut input = None; let mut set_replacement = false; let mut replacement = None; let mut truncate = None; let mut windows = None; for arg in ::std::env::args().skip(1) { if set_replacement { replacement = Some(arg); set_replacement = false; } else if arg == "-r" || arg == "--replace" { set_replacement = true; } else if arg == "--truncate" { truncate = Some(true); } else if arg == "--no-truncate" { truncate = Some(false); } else if arg == "--windows" { windows = Some(true); } else if arg == "--no-windows" { windows = Some(false); } else if arg == "-" { input = None; } else { input = Some(arg); } } let input = if let Some(input) = input { input } else { let mut buffer = String::new(); ::std::io::Read::read_to_string(&mut ::std::io::stdin(), &mut buffer)?; buffer }; let mut options = sanitize_filename::Options::default(); if let Some(ref replacement) = replacement { options.replacement = replacement; } if let Some(windows) = windows { options.windows = windows; } if let Some(truncate) = truncate { options.truncate = truncate; } let output = sanitize_filename::sanitize_with_options(input, options); println!("{}", &output); Ok(()) }