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 732
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 869
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 863
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 819
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 847
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.

[show source]
# File lib/shrine.rb, line 795
def download(*args)
  if storage.respond_to?(:download)
    storage.download(id, *args)
  else
    tempfile = Tempfile.new(["shrine", ".#{extension}"], binmode: true)
    open(*args) { |io| IO.copy_stream(io, tempfile.path) }
    tempfile.tap(&:open)
  end
end
eof? ()

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

[show source]
# File lib/shrine.rb, line 813
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 836
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 762
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 877
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 741
def id
  @data.fetch("id")
end
metadata ()

A hash of file metadata that was extracted during upload.

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

The MIME type of the uploaded file.

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

Opens an IO object of the uploaded file for reading and yields it to the block, closing it after the block finishes. For opening without a block to_io can be used.

uploaded_file.open do |io|
  puts io.read # prints the content of the file
end
[show source]
# File lib/shrine.rb, line 785
def open(*args)
  @io = storage.open(id, *args)
  yield @io
ensure
  @io.close if @io
  @io = nil
end
original_filename ()

The filename that was extracted from the uploaded file.

[show source]
# File lib/shrine.rb, line 756
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 807
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 841
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 825
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 892
def shrine_class
  self.class.shrine_class
end
size ()

The filesize of the uploaded file.

[show source]
# File lib/shrine.rb, line 768
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 887
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 746
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 852
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 858
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 882
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 830
def url(**options)
  storage.url(id, **options)
end