aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_vault/filename.rb
blob: 647d037b1f6616be5f0cc1ccdf215968b83ee8e7 (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 ActiveVault::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