• Stars
    star
    222
  • Rank 179,123 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Filename sanitization for Ruby

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

More Repositories

1

zepto

Zepto.js is a minimalist JavaScript library for modern browsers, with a jQuery-compatible API
HTML
14,992
star
2

keymaster

A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
JavaScript
6,525
star
3

scriptaculous

script.aculo.us is an open-source JavaScript framework for visual effects and interface behaviours.
HTML
2,321
star
4

vapor.js

The World's Smallest & Fastest JavaScript Library
JavaScript
1,373
star
5

dom-monster

JavaScript
562
star
6

emile

emile.js is a no-frills stand-alone CSS animation JavaScript framework, named after Émile Cohl, early animator.
JavaScript
530
star
7

scripty2

scripty2: for a more delicious web
JavaScript
516
star
8

pragmatic.js

Pragmatic rules for writing JavaScript
381
star
9

textorize

Ruby
236
star
10

semicolon.js

A more secure. stable and reliable version of vapor.js
JavaScript
229
star
11

creditcard_js

A creditcard validation library for JavaScript using Prototype
JavaScript
184
star
12

downtime

Standardized JSON API to indicate downtime for apps and services
JavaScript
90
star
13

prototype_helpers

Various bits and helpers for Prototype and scriptaculous, that might prove useful or not. :)
JavaScript
75
star
14

bitarray.js

Pure JavaScript bit array/bitfield implementation
JavaScript
53
star
15

spark_pr

Pure Ruby sparklines.
Ruby
42
star
16

purrson-icon

39
star
17

creditcard_js_underscore

A port of creditcard_js to underscore.js
JavaScript
23
star
18

secure.js

JavaScript
20
star
19

jive

HTML
17
star
20

js-animation-course

JavaScript
16
star
21

gifs

Shell
15
star
22

box2d-js

Sprocketized version of box2jd-js
JavaScript
12
star
23

blink

12
star
24

zeptojs.com

JavaScript
12
star
25

dotvim

Vim Script
11
star
26

ponymizer

Generator of random pony names to be used as friendly and fun test data
Ruby
10
star
27

js1k-finals

7
star
28

jslogocrazyness

6
star
29

madrobby.github.com

5
star
30

acornkittens

JavaScript
5
star
31

yotta

A heavily-optimized JavaScript library with a large impact
HTML
5
star
32

plastic-clock

CSS
3
star
33

schnitzelconf-intro

1
star