class Refile::File
Attributes
@return [Backend] the backend the file is stored in
@return [String] the id of the file
Public Class Methods
@api private
# File lib/refile/file.rb, line 10 def initialize(backend, id) @backend = backend @id = id end
Public Instance Methods
Close the file object and release its file descriptor.
@return [void]
# File lib/refile/file.rb, line 35 def close io.close end
Remove the file from the backend.
@return [void]
# File lib/refile/file.rb, line 47 def delete backend.delete(id) end
Downloads the file to a Tempfile on disk and returns this tempfile.
@example
file = backend.upload(StringIO.new("hello")) tempfile = file.download File.read(tempfile.path) # => "hello"
@return [Tempfile] a tempfile with the file's content
# File lib/refile/file.rb, line 69 def download return io if io.is_a?(Tempfile) Tempfile.new(id, binmode: true).tap do |tempfile| IO.copy_stream(io, tempfile) tempfile.rewind tempfile.fsync end end
Returns whether there is more data to read. Returns true if the end of the data has been reached.
@return [Boolean]
# File lib/refile/file.rb, line 28 def eof? io.eof? end
@return [Boolean] whether the file exists in the backend
# File lib/refile/file.rb, line 52 def exists? backend.exists?(id) end
Reads from the file.
@see www.ruby-doc.org/core-2.2.0/IO.html#method-i-read
@return [String] The contents of the read chunk
# File lib/refile/file.rb, line 20 def read(*args) io.read(*args) end
Rewind to beginning of file.
@return [nil]
# File lib/refile/file.rb, line 82 def rewind @io = nil end
@return [Integer] the size of the file in bytes
# File lib/refile/file.rb, line 40 def size backend.size(id) end
@return [IO] an IO object which contains the contents of the file
# File lib/refile/file.rb, line 57 def to_io io end
Private Instance Methods
# File lib/refile/file.rb, line 88 def io @io ||= backend.open(id) end