struct ActiveStorage::Filename

Overview

Wraps a string representing a filename to give access to its parts and to its sanitized form.

https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/filename.rb

Included Modules

Defined in:

active_storage/filename.cr

Constant Summary

STRIPPED_CHARACTERS = " \t\n\v\f\r\u0000"

Characters trimmed from both ends by #sanitized, matching Ruby's String#strip.

UNSAFE_CHARACTERS = "\u202E%$|:;/<>?*\"\t\r\n\\"

Characters considered unsafe for storage. #sanitized replaces each of them with a dash.

Constructors

Instance Method Summary

Constructor Detail

def self.new(filename : String) #

def self.wrap(filename : Filename) : Filename #

Returns the passed filename unchanged when it is already a Filename.


def self.wrap(filename : String) : Filename #

Returns a Filename built from the passed string.


Instance Method Detail

def <=>(other : Filename) #
Description copied from module Comparable(ActiveStorage::Filename)

The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.

Subclasses define this method to provide class-specific ordering.

The comparison operator is usually used to sort values:

# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]

# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]

def base : String #

Returns the part of the filename preceding any extension.

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

def extension : String #

def extension_with_delimiter : String #

Returns the extension of the filename, including the dot that precedes it.

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

def extension_without_delimiter : String #

Returns the extension of the filename, without the dot that precedes it.

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

def filename : String #

def sanitized : String #

Returns the sanitized filename.

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

def to_s(io : IO) : Nil #

Returns the sanitized version of the filename.