Class: ActiveStorage::Filename

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
app/models/active_storage/filename.rb

Overview

Encapsulates a string representing a filename to provide convenient access to parts of it and sanitization. A Filename instance is returned by ActiveStorage::Blob#filename, and is comparable so it can be used for sorting.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Filename

Returns a new instance of Filename.



16
17
18
# File 'app/models/active_storage/filename.rb', line 16

def initialize(filename)
  @filename = filename
end

Class Method Details

.wrap(filename) ⇒ Object

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.



11
12
13
# File 'app/models/active_storage/filename.rb', line 11

def wrap(filename)
  filename.kind_of?(self) ? filename : new(filename)
end

Instance Method Details

#<=>(other) ⇒ Object



74
75
76
# File 'app/models/active_storage/filename.rb', line 74

def <=>(other)
  to_s.downcase <=> other.to_s.downcase
end

#as_jsonObject



66
67
68
# File 'app/models/active_storage/filename.rb', line 66

def as_json(*)
  to_s
end

#baseObject

Returns the part of the filename preceding any extension.

ActiveStorage::Filename.new("racecar.jpg").base # => "racecar"
ActiveStorage::Filename.new("racecar").base     # => "racecar"
ActiveStorage::Filename.new(".gitignore").base  # => ".gitignore"


25
26
27
# File 'app/models/active_storage/filename.rb', line 25

def base
  File.basename @filename, extension_with_delimiter
end

#extension_with_delimiterObject

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.

ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter # => ".jpg"
ActiveStorage::Filename.new("racecar").extension_with_delimiter     # => ""
ActiveStorage::Filename.new(".gitignore").extension_with_delimiter  # => ""


35
36
37
# File 'app/models/active_storage/filename.rb', line 35

def extension_with_delimiter
  File.extname @filename
end

#extension_without_delimiterObject Also known as: extension

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.

ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter # => "jpg"
ActiveStorage::Filename.new("racecar").extension_without_delimiter     # => ""
ActiveStorage::Filename.new(".gitignore").extension_without_delimiter  # => ""


45
46
47
# File 'app/models/active_storage/filename.rb', line 45

def extension_without_delimiter
  extension_with_delimiter.from(1).to_s
end

#sanitizedObject

Returns the sanitized filename.

ActiveStorage::Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg"
ActiveStorage::Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"

Characters considered unsafe for storage (e.g. , $, and the RTL override character) are replaced with a dash.



57
58
59
# File 'app/models/active_storage/filename.rb', line 57

def sanitized
  @filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
end

#to_jsonObject



70
71
72
# File 'app/models/active_storage/filename.rb', line 70

def to_json
  to_s
end

#to_sObject

Returns the sanitized version of the filename.



62
63
64
# File 'app/models/active_storage/filename.rb', line 62

def to_s
  sanitized.to_s
end