Class: Refile::File
- Inherits:
-
Object
- Object
- Refile::File
- Defined in:
- lib/refile/file.rb
Instance Attribute Summary collapse
-
#backend ⇒ Backend
readonly
The backend the file is stored in.
-
#id ⇒ String
readonly
The id of the file.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Prevent from exposing secure information unexpectedly.
-
#close ⇒ void
Close the file object and release its file descriptor.
-
#delete ⇒ void
Remove the file from the backend.
-
#download ⇒ Tempfile
Downloads the file to a Tempfile on disk and returns this tempfile.
-
#eof? ⇒ Boolean
Returns whether there is more data to read.
-
#exists? ⇒ Boolean
Whether the file exists in the backend.
-
#initialize(backend, id) ⇒ File
constructor
private
A new instance of File.
-
#read(*args) ⇒ String
Reads from the file.
-
#rewind ⇒ nil
Rewind to beginning of file.
-
#size ⇒ Integer
The size of the file in bytes.
-
#to_io ⇒ IO
An IO object which contains the contents of the file.
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
#backend ⇒ Backend (readonly)
Returns the backend the file is stored in.
4 5 6 |
# File 'lib/refile/file.rb', line 4 def backend @backend end |
#id ⇒ String (readonly)
Returns the id of the file.
7 8 9 |
# File 'lib/refile/file.rb', line 7 def id @id end |
Instance Method Details
#as_json ⇒ Hash
Prevent from exposing secure information unexpectedly
89 90 91 92 93 94 |
# File 'lib/refile/file.rb', line 89 def as_json { id: id, backend: backend.to_s } end |
#close ⇒ void
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 |
#delete ⇒ void
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 |
#download ⇒ Tempfile
Downloads the file to a Tempfile on disk and returns this tempfile.
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.
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.
52 53 54 |
# File 'lib/refile/file.rb', line 52 def exists? backend.exists?(id) end |
#read(*args) ⇒ String
Reads from the file.
20 21 22 |
# File 'lib/refile/file.rb', line 20 def read(*args) io.read(*args) end |
#rewind ⇒ nil
Rewind to beginning of file.
82 83 84 |
# File 'lib/refile/file.rb', line 82 def rewind @io = nil end |
#size ⇒ Integer
Returns the size of the file in bytes.
40 41 42 |
# File 'lib/refile/file.rb', line 40 def size backend.size(id) end |
#to_io ⇒ IO
Returns 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 |