module Shrine::Plugins::Base::FileMethods

  1. lib/shrine.rb

Public Instance Aliases

content_type -> mime_type
eql? -> ==

Attributes

data [R]

The hash of information which defines this uploaded file.

Public Class methods

new (data)

Initializes the uploaded file with the given data hash.

[show source]
# File lib/shrine.rb, line 725
def initialize(data)
  raise Error, "#{data.inspect} isn't valid uploaded file data" unless data["id"] && data["storage"]

  @data = data
  @data["metadata"] ||= {}
  storage # ensure storage is registered
end

Public Instance methods

== (other)

Returns true if the other UploadedFile is uploaded to the same storage and it has the same id.

[show source]
# File lib/shrine.rb, line 896
def ==(other)
  other.is_a?(self.class) &&
  self.id == other.id &&
  self.storage_key == other.storage_key
end
as_json (*args)

Conform to ActiveSupport's JSON interface.

[show source]
# File lib/shrine.rb, line 890
def as_json(*args)
  data
end
close ()

Part of complying to the IO interface. It delegates to the internally opened IO object.

[show source]
# File lib/shrine.rb, line 846
def close
  io.close if @io
end
delete ()

Calls #delete on the storage, which deletes the file from the storage.

[show source]
# File lib/shrine.rb, line 874
def delete
  storage.delete(id)
end
download (*args)

Calls #download on the storage if the storage implements it, otherwise uses open to stream the underlying IO to a Tempfile.

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
#=> "..."
[show source]
# File lib/shrine.rb, line 818
def download(*args)
  if storage.respond_to?(:download)
    tempfile = storage.download(id, *args)
  else
    tempfile = Tempfile.new(["shrine", ".#{extension}"], binmode: true)
    open(*args) { |io| IO.copy_stream(io, tempfile) }
    tempfile.open
  end

  block_given? ? yield(tempfile) : tempfile
ensure
  tempfile.close! if ($! || block_given?) && tempfile
end
eof? ()

Part of complying to the IO interface. It delegates to the internally opened IO object.

[show source]
# File lib/shrine.rb, line 840
def eof?
  io.eof?
end
exists? ()

Calls #exists? on the storage, which checks whether the file exists on the storage.

[show source]
# File lib/shrine.rb, line 863
def exists?
  storage.exists?(id)
end
extension ()

The extension derived from id if present, otherwise from

original_filename.

[show source]
# File lib/shrine.rb, line 755
def extension
  result = File.extname(id)[1..-1] || File.extname(original_filename.to_s)[1..-1]
  result.downcase if result
end
hash ()

Enables using UploadedFile objects as hash keys.

[show source]
# File lib/shrine.rb, line 904
def hash
  [id, storage_key].hash
end
id ()

The location where the file was uploaded to the storage.

[show source]
# File lib/shrine.rb, line 734
def id
  @data.fetch("id")
end
metadata ()

A hash of file metadata that was extracted during upload.

[show source]
# File lib/shrine.rb, line 744
def metadata
  @data.fetch("metadata")
end
mime_type ()

The MIME type of the uploaded file.

[show source]
# File lib/shrine.rb, line 766
def mime_type
  metadata["mime_type"]
end
open (*args)

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 }
#=> "..."
[show source]
# File lib/shrine.rb, line 789
def open(*args)
  return to_io unless block_given?

  begin
    @io = storage.open(id, *args)
    yield @io
  ensure
    @io.close if @io
    @io = nil
  end
end
original_filename ()

The filename that was extracted from the uploaded file.

[show source]
# File lib/shrine.rb, line 749
def original_filename
  metadata["filename"]
end
read (*args)

Part of complying to the IO interface. It delegates to the internally opened IO object.

[show source]
# File lib/shrine.rb, line 834
def read(*args)
  io.read(*args)
end
replace (io, context = {})

Uploads a new file to this file's location and returns it.

[show source]
# File lib/shrine.rb, line 868
def replace(io, context = {})
  uploader.upload(io, context.merge(location: id))
end
rewind ()

Part of complying to the IO interface. It delegates to the internally opened IO object.

[show source]
# File lib/shrine.rb, line 852
def rewind
  io.rewind
end
shrine_class ()

Returns the Shrine class that this file's class is namespaced under.

[show source]
# File lib/shrine.rb, line 919
def shrine_class
  self.class.shrine_class
end
size ()

The filesize of the uploaded file.

[show source]
# File lib/shrine.rb, line 761
def size
  (@io && @io.size) || (metadata["size"] && Integer(metadata["size"]))
end
storage ()

Returns the storage that this file was uploaded to.

[show source]
# File lib/shrine.rb, line 914
def storage
  shrine_class.find_storage(storage_key)
end
storage_key ()

The string identifier of the storage the file is uploaded to.

[show source]
# File lib/shrine.rb, line 739
def storage_key
  @data.fetch("storage")
end
to_io ()

Returns an opened IO object for the uploaded file.

[show source]
# File lib/shrine.rb, line 879
def to_io
  io
end
to_json (*args)

Returns the data hash in the JSON format. Suitable for storing in a database column or passing to a background job.

[show source]
# File lib/shrine.rb, line 885
def to_json(*args)
  data.to_json(*args)
end
uploader ()

Returns an uploader object for the corresponding storage.

[show source]
# File lib/shrine.rb, line 909
def uploader
  shrine_class.new(storage_key)
end
url (**options)

Calls #url on the storage, forwarding any given URL options.

[show source]
# File lib/shrine.rb, line 857
def url(**options)
  storage.url(id, **options)
end