| 1 | # sanitize-filename |
| 2 | |
| 3 | A basic filename sanitizer, based on Node's [sanitize-filename](https://www.npmjs.com/package/sanitize-filename). |
| 4 | |
| 5 | Use like: |
| 6 | |
| 7 | ```rust |
| 8 | extern crate sanitize_filename; |
| 9 | |
| 10 | fn main() { |
| 11 | println!("{}", sanitize_filename::sanitize("some-user-defined/../../../string")); |
| 12 | // prints some-user-defined......string |
| 13 | } |
| 14 | ``` |
| 15 | |
| 16 | You can also configure a few basic options: |
| 17 | |
| 18 | ```rust |
| 19 | let options = sanitize_filename::Options { |
| 20 | truncate: true, // true by default, truncates to 255 bytes |
| 21 | windows: true, // default value depends on the OS, removes reserved names like `con` from start of strings on Windows |
| 22 | replacement: "" // str to replace sanitized chars/strings |
| 23 | }; |
| 24 | |
| 25 | let sanitized = sanitize_filename::sanitize_with_options("some-user-defined/../../../string", options); |
| 26 | ``` |
| 27 | |
| 28 | Also provides a basic command line binary. Use like: |
| 29 | |
| 30 | ```bash |
| 31 | cargo install sanitize-filename |
| 32 | sanitize-filename my_filename.txt |
| 33 | ``` |
| 34 | |
| 35 | ``` |
| 36 | Pass a file name to clean to the program (also reads STDIN) |
| 37 | |
| 38 | FLAGS: |
| 39 | -r, --replace <r> Replacement characters |
| 40 | --windows, --no-windows Whether to handle filenames for Windows |
| 41 | --truncate, --no-truncate Whether to truncate file names to 255 bytes |
| 42 | ``` |