class Refile::File

Attributes

backend[R]

@return [Backend] the backend the file is stored in

id[R]

@return [String] the id of the file

Public Class Methods

new(backend, id) click to toggle source

@api private

# File lib/refile/file.rb, line 10
def initialize(backend, id)
  @backend = backend
  @id = id
end

Public Instance Methods

as_json() click to toggle source

Prevent from exposing secure information unexpectedly

@return [Hash]

# File lib/refile/file.rb, line 89
def as_json
  {
    id: id,
    backend: backend.to_s
  }
end
close() click to toggle source

Close the file object and release its file descriptor.

@return [void]

# File lib/refile/file.rb, line 35
def close
  io.close
end
delete() click to toggle source

Remove the file from the backend.

@return [void]

# File lib/refile/file.rb, line 47
def delete
  backend.delete(id)
end
download() click to toggle source

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
eof?() click to toggle source

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
exists?() click to toggle source

@return [Boolean] whether the file exists in the backend

# File lib/refile/file.rb, line 52
def exists?
  backend.exists?(id)
end
read(*args) click to toggle source

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() click to toggle source

Rewind to beginning of file.

@return [nil]

# File lib/refile/file.rb, line 82
def rewind
  @io = nil
end
size() click to toggle source

@return [Integer] the size of the file in bytes

# File lib/refile/file.rb, line 40
def size
  backend.size(id)
end
to_io() click to toggle source

@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

io() click to toggle source
# File lib/refile/file.rb, line 98
def io
  @io ||= backend.open(id)
end