module Shrine::InstanceMethods
Attributes
The symbol identifier for the storage used by the uploader.
Public Class Methods
Source
# File lib/shrine.rb 188 def initialize(storage_key) 189 @storage_key = storage_key.to_sym 190 191 storage # ensure storage is registered 192 end
Accepts a storage symbol registered in Shrine.storages.
Shrine.new(:store)
Public Instance Methods
Source
# File lib/shrine.rb 232 def extract_metadata(io, **) 233 { 234 "filename" => extract_filename(io), 235 "size" => extract_size(io), 236 "mime_type" => extract_mime_type(io), 237 } 238 end
Extracts filename, size and MIME type from the file, which is later accessible through UploadedFile#metadata.
# File lib/shrine.rb 226 def generate_location(io, metadata: {}, **) 227 basic_location(io, metadata:) 228 end
Generates a unique location for the uploaded file, preserving the file extension. Can be overriden in uploaders for generating custom location.
Source
# File lib/shrine.rb 242 def opts 243 self.class.opts 244 end
The class-level options hash. This should probably not be modified at the instance level.
Source
# File lib/shrine.rb 195 def storage 196 self.class.find_storage(storage_key) 197 end
Returns the storage object referenced by the identifier.
Source
# File lib/shrine.rb 208 def upload(io, **) 209 _enforce_io(io) 210 211 metadata = get_metadata(io, **) 212 location = get_location(io, **, metadata:) 213 214 _upload(io, **, location:, metadata:) 215 216 self.class::UploadedFile.new( 217 id: location, 218 storage: storage_key, 219 metadata: metadata, 220 ) 221 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