abstract class
Latch::StoredFile
- Latch::StoredFile
- Reference
- Object
Overview
Represents a file that has been uploaded to a storage backend.
This class is JSON serializable and stores the file's location (#id),
which storage it's in (#storage), and associated metadata.
NOTE The JSON format is compatible with Shrine.rb/Shrine.cr:
{
"id": "uploads/abc123.jpg",
"storage": "store",
"metadata": {
"filename": "photo.jpg",
"size": 102400,
"mime_type": "image/jpeg"
}
}
Included Modules
- JSON::Serializable
Defined in:
latch/stored_file.crConstructors
- .new(id : String, storage_key : String, metadata : MetadataHash = MetadataHash.new)
- .new(pull : JSON::PullParser)
- .new(*, __pull_for_json_serializable pull : JSON::PullParser)
Class Method Summary
-
.adapter
NOTE This mimics the behavior of Avram's
JSON::Serializableextension.
Instance Method Summary
-
#==(other : StoredFile) : Bool
Compares two
StoredFileby their id and storage. -
#==(other) : Bool
Returns
false(other can only be aValuehere). -
#[]?(key : String) : MetadataValue
Aliases the
#[]?method on the metadata property. -
#close : Nil
Closes the file if it is open.
-
#data : Hash(String, String | MetadataHash)
Returns a hash representation suitable for JSON serialization compatible with Shrine.
-
#delete : Nil
Deletes the file from storage.
-
#download(**options) : File
Downloads the file to a temporary file and returns it.
-
#download(**options, &)
Downloads to a tempfile, yields it to the block, then cleans up.
-
#exists? : Bool
Returns whether this file exists in storage.
-
#extension : String
The non-nilable variant of the
#extension?method. -
#extension? : String | Nil
Returns the file extension based on the id or original filename.
- #id : String
-
#io : IO
Returns the currently opened IO, or opens it if not already open.
- #metadata : MetadataHash
-
#open(**options, &)
Opens the file for reading.
-
#open(**options) : IO
Opens the file and stores the IO handle internally for subsequent reads.
-
#opened? : Bool
Tests whether the file has been opened or not.
-
#storage : Storage
Returns the storage instance this file is stored in.
- #storage_key : String
-
#stream(destination : IO, **options) : Nil
Streams the file content to the given IO destination.
-
#url(**options) : String
Returns the URL for accessing this file.
-
#variant_location(variant : String) : String
Returns the location for a named variant of this file.
Constructor Detail
Class Method Detail
Instance Method Detail
Returns false (other can only be a Value here).
Aliases the #[]? method on the metadata property.
file["width"]?
# => 800
file["custom"]?
# => "value"
Returns a hash representation suitable for JSON serialization compatible with Shrine.
Downloads the file to a temporary file and returns it. As opposed to the block variant, this temporary file needs to be closed and deleted manually:
tempfile = file.download
tempfile.path
# => "/tmp/latch123456789.jpg"
tempfile.gets_to_end
# => "file content"
tempfile.close
tempfile.delete
Downloads to a tempfile, yields it to the block, then cleans up.
file.download do |tempfile|
process(tempfile.path)
end
# tempfile is automatically deleted
Returns the file extension based on the id or original filename.
NOTE This method relies on filename? and filename which are generated
by the extract macro on Latch::Uploader. Concrete
StoredFile subclasses created through the Uploader.inherited macro
will have these methods available automatically.
file.extension?
# => "jpg"
Opens the file for reading. If a block is given, yields the IO and automatically closes it afterwards. Returns the block's return value.
file.open do |io|
io.gets_to_end
end
Opens the file and stores the IO handle internally for subsequent reads.
Remember to call #close when done.
file.open
content = file.io.gets_to_end
file.close
Streams the file content to the given IO destination.
file.stream(response.output)
Returns the URL for accessing this file.
file.url
# => "https://bucket.s3.amazonaws.com/uploads/abc123.jpg"
# for presigned URLs
file.url(expires_in: 1.hour)
Returns the location for a named variant of this file. The variant is stored as a sibling under a subdirectory matching the original basename.
file = StoredFile.new(id: "uploads/abc123.jpg", ...)
file.variant_location("sizes_large")
# => "uploads/abc123/sizes_large.jpg"