Shrine::UploadedFile::InstanceMethods (original) (raw)
Public Instance Aliases
content_type | -> | mime_type |
---|---|---|
eql? | -> | == |
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
new(data)
Initializes the uploaded file with the given data hash.
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 43 end
Public Instance methods
==(other)
Returns true if the other UploadedFile is uploaded to the same storage and it has the same id.
225 def ==(other) 226 self.class == other.class && 227 self.id == other.id && 228 self.storage_key == other.storage_key 229 end
[](key)
Shorthand for accessing metadata values.
as_json(*args)
Conform to ActiveSupport’s JSON interface.
close()
Part of complying to the IO interface. It delegates to the internally opened IO object.
data()
Returns serializable hash representation of the uploaded file.
219 def data 220 { "id" => id, "storage" => storage_key.to_s, "metadata" => metadata } 221 end
delete()
Calls #delete
on the storage, which deletes the file from the storage.
download(**options)
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
uploaded_file.download { |tempfile| tempfile.read }
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
eof?()
Part of complying to the IO interface. It delegates to the internally opened IO object.
exists?()
Calls #exists?
on the storage, which checks whether the file exists on the storage.
extension()
The extension derived from id if present, otherwise it’s derived from original_filename.
52 def extension 53 identifier = id =~ URI::DEFAULT_PARSER.make_regexp ? id.sub(/?.+$/, "") : id 54 result = File.extname(identifier)[1..-1] 55 result ||= File.extname(original_filename.to_s)[1..-1] 56 result.downcase if result 57 end
inspect()
Returns simplified inspect output.
253 def inspect 254 "#<#{self.class.inspect} storage=#{storage_key.inspect} id=#{id.inspect} metadata=#{metadata.inspect}>" 255 end
mime_type()
The MIME type of the uploaded file.
open(**options)
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 uploaded_file.read uploaded_file.close
uploaded_file.open { |io| io.read }
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
opened?()
Returns whether the file has already been opened.
original_filename()
The filename that was extracted from the uploaded file.
46 def original_filename 47 metadata["filename"] 48 end
read(*args)
Part of complying to the IO interface. It delegates to the internally opened IO object.
replace(io, **options)
Uploads a new file to this file’s location and returns it.
192 def replace(io, **options) 193 uploader.upload(io, **options, location: id) 194 end
rewind()
Part of complying to the IO interface. It delegates to the internally opened IO object.
shrine_class()
Returns the Shrine class that this file’s class is namespaced under.
248 def shrine_class 249 self.class.shrine_class 250 end
size()
The filesize of the uploaded file.
60 def size 61 (@io && @io.size) || (metadata["size"] && Integer(metadata["size"])) 62 end
storage()
Returns the storage that this file was uploaded to.
243 def storage 244 shrine_class.find_storage(storage_key) 245 end
stream(destination, **options)
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)
uploaded_file.stream("/path/to/destination")
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
to_io()
Returns an opened IO object for the uploaded file.
to_json(*args)
Returns the data hash in the JSON format. Suitable for storing in a database column or passing to a background job.
209 def to_json(*args) 210 data.to_json(*args) 211 end
uploader()
Returns an uploader object for the corresponding storage.
238 def uploader 239 shrine_class.new(storage_key) 240 end
url(**options)
Calls #url
on the storage, forwarding any given URL options.
181 def url(**options) 182 storage.url(id, **options) 183 end