Created
February 3, 2013 15:59
-
-
Save madrobby/4702279 to your computer and use it in GitHub Desktop.
Filename sanitizer. Comments welcome.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# clean filenames for OS X, Windows and Linux | |
# this is for Ruby 1.8 and Rails 2.3 | |
# requires activesupport 2.3.x | |
class Filename | |
CHARACTER_FILTER = /[\x00-\x1F\/\\:\*\?\"<>\|]/u | |
UNICODE_WHITESPACE = /[[:space:]]+/u | |
def initialize(filename) | |
@raw = filename.to_s.freeze | |
end | |
# strip whitespace on beginning and end | |
# collapse intra-string whitespace into single spaces | |
def normalize | |
@normalized ||= @raw.mb_chars.strip.to_s.gsub(UNICODE_WHITESPACE,' ').mb_chars | |
end | |
# remove characters that aren't allowed cross-OS | |
def sanitize | |
@sanitized ||= normalize.to_s.gsub(CHARACTER_FILTER,'') | |
end | |
# normalize unicode string and cut off at 255 characters | |
def truncate | |
@truncated ||= sanitize.mb_chars.normalize.mb_chars.slice(0..254) | |
end | |
# convert back from multibyte string | |
def to_s | |
truncate.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@peterc without the mb_chars the slicing won't work on Ruby 1.8