Class: Refile::File

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, id) ⇒ File

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of File.



10
11
12
13
# File 'lib/refile/file.rb', line 10

def initialize(backend, id)
  @backend = backend
  @id = id
end

Instance Attribute Details

#backendBackend (readonly)

Returns the backend the file is stored in.

Returns:

  • (Backend)

    the backend the file is stored in



4
5
6
# File 'lib/refile/file.rb', line 4

def backend
  @backend
end

#idString (readonly)

Returns the id of the file.

Returns:

  • (String)

    the id of the file



7
8
9
# File 'lib/refile/file.rb', line 7

def id
  @id
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the file object and release its file descriptor.



35
36
37
# File 'lib/refile/file.rb', line 35

def close
  io.close
end

#deletevoid

This method returns an undefined value.

Remove the file from the backend.



47
48
49
# File 'lib/refile/file.rb', line 47

def delete
  backend.delete(id)
end

#downloadTempfile

Downloads the file to a Tempfile on disk and returns this tempfile.

Examples:

file = backend.upload(StringIO.new("hello"))
tempfile = file.download
File.read(tempfile.path) # => "hello"

Returns:

  • (Tempfile)

    a tempfile with the file’s content



69
70
71
72
73
74
75
76
77
# 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?Boolean

Returns whether there is more data to read. Returns true if the end of the data has been reached.

Returns:

  • (Boolean)


28
29
30
# File 'lib/refile/file.rb', line 28

def eof?
  io.eof?
end

#exists?Boolean

Returns whether the file exists in the backend.

Returns:

  • (Boolean)

    whether the file exists in the backend



52
53
54
# File 'lib/refile/file.rb', line 52

def exists?
  backend.exists?(id)
end

#read(*args) ⇒ String

Reads from the file.

Returns:

  • (String)

    The contents of the read chunk

See Also:



20
21
22
# File 'lib/refile/file.rb', line 20

def read(*args)
  io.read(*args)
end

#rewindnil

Rewind to beginning of file.

Returns:

  • (nil)


82
83
84
# File 'lib/refile/file.rb', line 82

def rewind
  @io = nil
end

#sizeInteger

Returns the size of the file in bytes.

Returns:

  • (Integer)

    the size of the file in bytes



40
41
42
# File 'lib/refile/file.rb', line 40

def size
  backend.size(id)
end

#to_ioIO

Returns an IO object which contains the contents of the file.

Returns:

  • (IO)

    an IO object which contains the contents of the file



57
58
59
# File 'lib/refile/file.rb', line 57

def to_io
  io
end