class ActiveStorage::Filename

def sanitized

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

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

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