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