2017-08-12 08:32:15 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2017-09-20 15:35:01 -04:00
|
|
|
|
# Encapsulates a string representing a filename to provide convenient access to parts of it and sanitization.
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# A Filename instance is returned by ActiveStorage::Blob#filename, and is comparable so it can be used for sorting.
|
2017-07-06 05:33:29 -04:00
|
|
|
|
class ActiveStorage::Filename
|
2017-06-30 13:12:58 -04:00
|
|
|
|
include Comparable
|
|
|
|
|
|
2018-02-07 21:15:55 -05:00
|
|
|
|
class << self
|
|
|
|
|
# Returns a Filename instance based on the given filename. If the filename is a Filename, it is
|
|
|
|
|
# returned unmodified. If it is a String, it is passed to ActiveStorage::Filename.new.
|
|
|
|
|
def wrap(filename)
|
|
|
|
|
filename.kind_of?(self) ? filename : new(filename)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-30 13:12:58 -04:00
|
|
|
|
def initialize(filename)
|
|
|
|
|
@filename = filename
|
|
|
|
|
end
|
|
|
|
|
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# Returns the part of the filename preceding any extension.
|
2017-08-29 13:40:03 -04:00
|
|
|
|
#
|
|
|
|
|
# ActiveStorage::Filename.new("racecar.jpg").base # => "racecar"
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# ActiveStorage::Filename.new("racecar").base # => "racecar"
|
|
|
|
|
# ActiveStorage::Filename.new(".gitignore").base # => ".gitignore"
|
2017-08-20 16:30:15 -04:00
|
|
|
|
def base
|
|
|
|
|
File.basename @filename, extension_with_delimiter
|
2017-06-30 13:12:58 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at the
|
|
|
|
|
# beginning) with the dot that precedes it. If the filename has no extension, an empty string is returned.
|
2017-08-29 13:40:03 -04:00
|
|
|
|
#
|
|
|
|
|
# ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter # => ".jpg"
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# ActiveStorage::Filename.new("racecar").extension_with_delimiter # => ""
|
|
|
|
|
# ActiveStorage::Filename.new(".gitignore").extension_with_delimiter # => ""
|
2017-08-20 16:30:15 -04:00
|
|
|
|
def extension_with_delimiter
|
|
|
|
|
File.extname @filename
|
2017-06-30 13:12:58 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at
|
|
|
|
|
# the beginning). If the filename has no extension, an empty string is returned.
|
2017-08-29 13:40:03 -04:00
|
|
|
|
#
|
|
|
|
|
# ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter # => "jpg"
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# ActiveStorage::Filename.new("racecar").extension_without_delimiter # => ""
|
|
|
|
|
# ActiveStorage::Filename.new(".gitignore").extension_without_delimiter # => ""
|
2017-08-20 16:30:15 -04:00
|
|
|
|
def extension_without_delimiter
|
|
|
|
|
extension_with_delimiter.from(1).to_s
|
2017-06-30 13:12:58 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-08-20 16:30:15 -04:00
|
|
|
|
alias_method :extension, :extension_without_delimiter
|
|
|
|
|
|
2017-08-29 13:40:03 -04:00
|
|
|
|
# Returns the sanitized filename.
|
|
|
|
|
#
|
|
|
|
|
# ActiveStorage::Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg"
|
|
|
|
|
# ActiveStorage::Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"
|
2017-07-24 13:05:15 -04:00
|
|
|
|
#
|
2017-09-20 15:34:04 -04:00
|
|
|
|
# Characters considered unsafe for storage (e.g. \, $, and the RTL override character) are replaced with a dash.
|
2017-06-30 13:12:58 -04:00
|
|
|
|
def sanitized
|
|
|
|
|
@filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "<EFBFBD>").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-24 13:05:15 -04:00
|
|
|
|
# Returns the sanitized version of the filename.
|
2017-06-30 13:12:58 -04:00
|
|
|
|
def to_s
|
|
|
|
|
sanitized.to_s
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-30 11:00:55 -04:00
|
|
|
|
def as_json(*)
|
|
|
|
|
to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_json
|
|
|
|
|
to_s
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-30 13:12:58 -04:00
|
|
|
|
def <=>(other)
|
|
|
|
|
to_s.downcase <=> other.to_s.downcase
|
|
|
|
|
end
|
|
|
|
|
end
|