Methods
Public Class
Public Instance
Attributes
id | [R] |
The location where the file was uploaded to the storage. |
metadata | [R] |
A hash of file metadata that was extracted during upload. |
storage_key | [R] |
The identifier of the storage the file is uploaded to. |
Public Class methods
Initializes the uploaded file with the given data hash.
# File lib/shrine/uploaded_file.rb 35 def initialize(data) 36 @id = data[:id] || data["id"] 37 @storage_key = data[:storage]&.to_sym || data["storage"]&.to_sym 38 @metadata = data[:metadata] || data["metadata"] || {} 39 40 fail Error, "#{data.inspect} isn't valid uploaded file data" unless @id && @storage_key 41 42 storage # ensure storage is registered 43 end
Public Instance methods
Returns true if the other UploadedFile
is uploaded to the same storage and it has the same id
.
# File lib/shrine/uploaded_file.rb 225 def ==(other) 226 self.class == other.class && 227 self.id == other.id && 228 self.storage_key == other.storage_key 229 end
Shorthand for accessing metadata values.
# File lib/shrine/uploaded_file.rb 71 def [](key) 72 metadata[key] 73 end
Conform to ActiveSupport’s JSON interface.
# File lib/shrine/uploaded_file.rb 214 def as_json(*args) 215 data 216 end
Part of complying to the IO interface. It delegates to the internally opened IO object.
# File lib/shrine/uploaded_file.rb 171 def close 172 io.close if opened? 173 end
Returns serializable hash representation of the uploaded file.
# File lib/shrine/uploaded_file.rb 219 def data 220 { "id" => id, "storage" => storage_key.to_s, "metadata" => metadata } 221 end
Calls #delete
on the storage, which deletes the file from the storage.
# File lib/shrine/uploaded_file.rb 198 def delete 199 storage.delete(id) 200 end
Streams content into a newly created Tempfile and returns it.
If a block is given, the opened Tempfile object is yielded to the block, and at the end of the block it’s automatically closed and deleted. In this case the return value of the method is the block return value.
If no block is given, the opened Tempfile is returned.
uploaded_file.download #=> #<File:/var/folders/.../20180302-33119-1h1vjbq.jpg> # or uploaded_file.download { |tempfile| tempfile.read } # tempfile is deleted
# File lib/shrine/uploaded_file.rb 121 def download(**options) 122 tempfile = Tempfile.new(["shrine", ".#{extension}"], binmode: true) 123 stream(tempfile, **options) 124 tempfile.open 125 126 block_given? ? yield(tempfile) : tempfile 127 ensure 128 tempfile.close! if ($! || block_given?) && tempfile 129 end
Part of complying to the IO interface. It delegates to the internally opened IO object.
# File lib/shrine/uploaded_file.rb 159 def eof? 160 io.eof? 161 end
Calls #exists?
on the storage, which checks whether the file exists on the storage.
# File lib/shrine/uploaded_file.rb 187 def exists? 188 storage.exists?(id) 189 end
The extension derived from id
if present, otherwise it’s derived from original_filename
.
# File lib/shrine/uploaded_file.rb 52 def extension 53 identifier = id =~ URI::DEFAULT_PARSER.make_regexp ? id.sub(/\?.+$/, "") : id # strip query params for shrine-url 54 result = File.extname(identifier)[1..-1] 55 result ||= File.extname(original_filename.to_s)[1..-1] 56 result.downcase if result 57 end
Enables using UploadedFile
objects as hash keys.
# File lib/shrine/uploaded_file.rb 233 def hash 234 [id, storage_key].hash 235 end
Returns simplified inspect output.
# File lib/shrine/uploaded_file.rb 253 def inspect 254 "#<#{self.class.inspect} storage=#{storage_key.inspect} id=#{id.inspect} metadata=#{metadata.inspect}>" 255 end
The MIME type of the uploaded file.
# File lib/shrine/uploaded_file.rb 65 def mime_type 66 metadata["mime_type"] 67 end
Calls #open
on the storage to open the uploaded file for reading. Most storages will return a lazy IO object which dynamically retrieves file content from the storage as the object is being read.
If a block is given, the opened IO object is yielded to the block, and at the end of the block it’s automatically closed. In this case the return value of the method is the block return value.
If no block is given, the opened IO object is returned.
uploaded_file.open #=> IO object returned by the storage uploaded_file.read #=> "..." uploaded_file.close # or uploaded_file.open { |io| io.read } # the IO is automatically closed
# File lib/shrine/uploaded_file.rb 92 def open(**options) 93 @io.close if @io 94 @io = _open(**options) 95 96 return @io unless block_given? 97 98 begin 99 yield @io 100 ensure 101 close 102 @io = nil 103 end 104 end
Returns whether the file has already been opened.
# File lib/shrine/uploaded_file.rb 176 def opened? 177 !!@io 178 end
The filename that was extracted from the uploaded file.
# File lib/shrine/uploaded_file.rb 46 def original_filename 47 metadata["filename"] 48 end
Part of complying to the IO interface. It delegates to the internally opened IO object.
# File lib/shrine/uploaded_file.rb 153 def read(*args) 154 io.read(*args) 155 end
Uploads a new file to this file’s location and returns it.
# File lib/shrine/uploaded_file.rb 192 def replace(io, **options) 193 uploader.upload(io, **options, location: id) 194 end
Part of complying to the IO interface. It delegates to the internally opened IO object.
# File lib/shrine/uploaded_file.rb 165 def rewind 166 io.rewind 167 end
Returns the Shrine
class that this file’s class is namespaced under.
# File lib/shrine/uploaded_file.rb 248 def shrine_class 249 self.class.shrine_class 250 end
The filesize of the uploaded file.
# File lib/shrine/uploaded_file.rb 60 def size 61 (@io && @io.size) || (metadata["size"] && Integer(metadata["size"])) 62 end
Returns the storage that this file was uploaded to.
# File lib/shrine/uploaded_file.rb 243 def storage 244 shrine_class.find_storage(storage_key) 245 end
Streams uploaded file content into the specified destination. The destination object is given directly to IO.copy_stream
, so it can be either a path on disk or an object that responds to #write
.
If the uploaded file is already opened, it will be simply rewinded after streaming finishes. Otherwise the uploaded file is opened and then closed after streaming.
uploaded_file.stream(StringIO.new) # or uploaded_file.stream("/path/to/destination")
# File lib/shrine/uploaded_file.rb 142 def stream(destination, **options) 143 if opened? 144 IO.copy_stream(io, destination) 145 io.rewind 146 else 147 open(**options) { |io| IO.copy_stream(io, destination) } 148 end 149 end
Returns an opened IO object for the uploaded file.
# File lib/shrine/uploaded_file.rb 203 def to_io 204 io 205 end
Returns the data hash in the JSON format. Suitable for storing in a database column or passing to a background job.
# File lib/shrine/uploaded_file.rb 209 def to_json(*args) 210 data.to_json(*args) 211 end
Returns an uploader object for the corresponding storage.
# File lib/shrine/uploaded_file.rb 238 def uploader 239 shrine_class.new(storage_key) 240 end
Calls #url
on the storage, forwarding any given URL options.
# File lib/shrine/uploaded_file.rb 181 def url(**options) 182 storage.url(id, **options) 183 end