aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/filename.rb
blob: 71614b511392e9f3c48b95e384c5eaee2b1e5885 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class ActiveStorage::Filename
  include Comparable

  def initialize(filename)
    @filename = filename
  end

  def extname
    File.extname(@filename)
  end

  def extension
    extname.from(1)
  end

  def base
    File.basename(@filename, extname)
  end

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

  def to_s
    sanitized.to_s
  end

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