Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created February 3, 2013 15:59
Show Gist options
  • Save madrobby/4702279 to your computer and use it in GitHub Desktop.
Save madrobby/4702279 to your computer and use it in GitHub Desktop.
Filename sanitizer. Comments welcome.
# 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
@madrobby
Copy link
Author

madrobby commented Feb 3, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment