Giter Club home page Giter Club logo

zaru's Introduction

zaru

Build Status

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 Freckle).

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

Zaru works with Ruby 1.8.7 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.

TODO

  • Make sure truncation is correct on Ruby 1.8
  • Extend test suite

Wait, what, Zaru?

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

zaru's People

Contributors

madrobby avatar ricn avatar tobix avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.