Skip to content

abstract struct Athena::Framework::AbstractFile
inherits Struct #

Represents a file on the filesystem without opening a file descriptor. This base type is needed as you can't inherit from non-abstract structs, and it makes sense to have a generic Athena::Framework::File type while also being able to share the logic with other sub-types.

Todo

Add more methods as needed.

Direct known subclasses

Athena::Framework::File Athena::Framework::UploadedFile

Constructors#

.new(path : String | Path, check_path : Bool = true)#

Create a new instance for the file at the provided path. If check_path is true, then an ATH::Exception::FileNotFound exception is raised if the file at the provided path does not exist.

View source

Methods#

#basename(suffix : String | Nil = nil) : String#

Returns the last component of this file's path. If suffix is present at the end of the path, it is removed.

file = ATH::File.new "/path/to/file.txt"
file.basename        # => "file.txt"
file.basename ".txt" # => "file"
View source

#content : String#

Returns the contents of this file as a string.

file = ATH::File.new "/path/to/foo.txt"
file.content # => "foo" (content of foo.txt)
View source

#extname : String#

Returns the extension of this file, or an empty string if it does not have one.

file = ATH::File.new "/path/to/file.txt"
file.extname # => "txt"
View source

#guess_extension : String | ::Nil#

Returns the extension based on the MIME type of this file, or nil if it is unknown. Uses the MIME type as guessed by #mime_type to guess the file extension.

file = ATH::File.new "/path/to/foo.txt"
file.guess_extension # => "txt"
View source

#mime_type : String | ::Nil#

Returns the MIME type of this file, using AMIME::Types under the hood.

file = ATH::File.new "/path/to/foo.txt"
file.mime_type # => "text/plain"
View source

#modification_time : Time#

Returns the time this file was last modified.

View source

#move(directory : Path | String, name : String | Nil = nil) : self#

Moves this file to the provided directory, optionally with the provided name. If no name is provided, its current #basename will be used.

View source

#path : String#

Returns the path to this file, which may be relative.

View source

#readable? : Bool#

Returns true if this file is readable by user of this process, otherwise returns false.

View source

#realpath : String#

Resolves the real path of this file by following symbolic links.

file = ATH::File.new "./../../etc/passwd"
file.realpath # => "/etc/passwd"
View source

#size : Int#

Returns the size in bytes of this file.

View source