Methods
Public Class
Public Instance
Attributes
storage_key | [R] |
The symbol identifier for the storage used by the uploader. |
Public Class methods
Accepts a storage symbol registered in Shrine.storages
.
Shrine.new(:store)
# File lib/shrine.rb 184 def initialize(storage_key) 185 @storage_key = storage_key.to_sym 186 187 storage # ensure storage is registered 188 end
Public Instance methods
Extracts filename, size and MIME type from the file, which is later accessible through UploadedFile#metadata
.
# File lib/shrine.rb 228 def extract_metadata(io, **options) 229 { 230 "filename" => extract_filename(io), 231 "size" => extract_size(io), 232 "mime_type" => extract_mime_type(io), 233 } 234 end
Generates a unique location for the uploaded file, preserving the file extension. Can be overriden in uploaders for generating custom location.
# File lib/shrine.rb 222 def generate_location(io, metadata: {}, **options) 223 basic_location(io, metadata: metadata) 224 end
The class-level options hash. This should probably not be modified at the instance level.
# File lib/shrine.rb 238 def opts 239 self.class.opts 240 end
Returns the storage object referenced by the identifier.
# File lib/shrine.rb 191 def storage 192 self.class.find_storage(storage_key) 193 end
The main method for uploading files. Takes an IO-like object and an optional context hash (used internally by Shrine::Attacher
). It calls user-defined process, and afterwards it calls store. The io
is closed after upload.
uploader.upload(io) uploader.upload(io, metadata: { “foo” => “bar” }) # add metadata uploader.upload(io, location: “path/to/file”) # specify location uploader.upload(io, upload_options: { acl: “public-read” }) # add upload options
# File lib/shrine.rb 204 def upload(io, **options) 205 _enforce_io(io) 206 207 metadata = get_metadata(io, **options) 208 location = get_location(io, **options, metadata: metadata) 209 210 _upload(io, **options, location: location, metadata: metadata) 211 212 self.class::UploadedFile.new( 213 id: location, 214 storage: storage_key, 215 metadata: metadata, 216 ) 217 end