Project

zaru

0.13
No release in over 3 years
Low commit activity in last 3 years
Zaru takes a given filename (a string) and normalizes, filters and truncates it, so it can be safely used as a filename in modern operating systems. Zaru doesn't remove Unicode characters when not necessary.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

zaru

Tests

Filename sanitization for Ruby. This is useful when you generate filenames for downloads from user input (we're using it for PDF invoice downloads in Noko).

Zaru.sanitize! "  whatver//wëird:user:înput:"
# => "whatēverwëirduserînput"

Zaru takes a given filename (a string) and normalizes, filters and truncates it.

It removes the bad stuff but leaves unicode characters in place, so users can use whatever alphabets they want to. Zaru also doesn't remove whitespace—instead, any sequence of whitespace that is 1 or more characters in length is collapsed to a single space. Filenames are truncated so that they are at maximum 255 characters long.

If extra breathing room is required (for example to add your own filename extension later), you can leave extra room with the :padding option:

Zaru.sanitize! "A"*400, :padding => 100
# resulting filename is 145 characters long

If you need to customize the fallback filename you can add your own fallback with the :fallback option:

Zaru.sanitize! "<<<", :fallback => 'no_file'
# resulting filename is 'no_file'

Zaru works with Ruby 1.9 or later. It's experimental and may eat your cat. Don't trust it in production systems.

Bad things in filenames

Wikipedia has a good overview on filenames. Basically, on modern-ish operating systems, the following characters are considered no-no (Zaru filters these):

/ \ ? * : | " < >

Additionally the ASCII control characters (hexadecimal 00 to 1f) are filtered.

All Unicode whitespace at the beginning and end of the potential filename is removed, and any Unicode whitespace within the filename is collapse to a single space character.

Certain filenames are reserved in Windows and are filtered.

Wait, what, Zaru?

Zaru is licensed under the terms of the MIT license. (c) 2013-2023 Thomas Fuchs