abstract class Latch::Storage

Overview

Storage backends handle the actual persistence of uploaded files. Implementations must provide methods for uploading, retrieving, checking existence, and deleting files.

Direct Known Subclasses

Defined in:

latch/storage.cr

Instance Method Summary

Instance Method Detail

abstract def delete(id : String) : Nil #

Deletes the file at the given location.

storage.delete("uploads/photo.jpg")

Does not raise if the file doesn't exist.


[View source]
abstract def exists?(id : String) : Bool #

Returns whether a file exists at the given location.

storage.exists?("uploads/photo.jpg")
# => true

[View source]
def move(io : IO, id : String, **options) : Nil #

Moves an IO from another location.


[View source]
def move(file : Latch::StoredFile, id : String, **options) : Nil #

Moves a file from another location.


[View source]
abstract def open(id : String, **options) : IO #

Opens the file at the given location and returns an IO for reading.

io = storage.open("uploads/photo.jpg")
content = io.gets_to_end
io.close

Raises Latch::FileNotFound if the file doesn't exist.


[View source]
abstract def upload(io : IO, id : String, **options) : Nil #

Uploads an IO to the given location (id) in the storage.

storage.upload(io, "uploads/photo.jpg")
storage.upload(io, "uploads/photo.jpg", metadata: {"filename" => "original.jpg"})

[View source]
abstract def url(id : String, **options) : String #

Returns the URL for accessing the file at the given location.

storage.url("uploads/photo.jpg")
# => "/uploads/photo.jpg"
storage.url("uploads/photo.jpg", host: "https://example.com")
# => "https://example.com/uploads/photo.jpg"

[View source]