Class: CloudFS::File
Overview
File class is aimed to provide native File object like interface to cloudfs files
Instance Attribute Summary collapse
-
#extension ⇒ String
The extension.
-
#mime ⇒ String
The mime type of file.
-
#size ⇒ String
readonly
The size.
Attributes inherited from Item
#blocklist_id, #blocklist_key, #date_content_last_modified, #date_created, #date_meta_last_modified, #id, #is_mirrored, #name, #parent_id, #type, #url, #version
Instance Method Summary collapse
-
#download(local_destination_path, filename: nil) {|Integer| ... } ⇒ true
Download this file to local directory.
-
#download_url ⇒ String
Get the download URL of the file.
-
#initialize(rest_adapter, parent: nil, parent_state: nil, in_trash: false, in_share: false, old_version: false, **properties) ⇒ File
constructor
A new instance of File.
-
#read(bytecount: nil) {|String| ... } ⇒ String
Read from file.
-
#rewind ⇒ Object
Reset position indicator.
-
#seek(offset, whence: 0) ⇒ Fixnum
Seek to a particular byte in this file.
-
#tell ⇒ Fixnum
Return current access position in this file.
Methods inherited from Item
#application_data, #application_data=, #change_attributes, #copy, #delete, #exists?, #in_share?, #in_trash?, #move, #old_version?, #path, #refresh, #restore, #save, #versions
Constructor Details
#initialize(rest_adapter, parent: nil, parent_state: nil, in_trash: false, in_share: false, old_version: false, **properties) ⇒ File
Returns a new instance of File.
47 48 49 50 51 52 53 54 |
# File 'lib/cloudfs/file.rb', line 47
def initialize(rest_adapter, parent: nil, parent_state: nil, in_trash: false,
in_share: false, old_version: false, ** properties)
fail RestAdapter::Errors::ArgumentError,
"Invalid item of type #{properties[:type]}" unless properties[:type] == 'file'
@offset = 0
super
end
|
Instance Attribute Details
#extension ⇒ String
Returns the extension.
24 25 26 |
# File 'lib/cloudfs/file.rb', line 24
def extension
@extension
end
|
#mime ⇒ String
Returns the mime type of file.
27 28 29 |
# File 'lib/cloudfs/file.rb', line 27
def mime
@mime
end
|
#size ⇒ String (readonly)
Returns the size.
21 22 23 |
# File 'lib/cloudfs/file.rb', line 21
def size
@size
end
|
Instance Method Details
#download(local_destination_path, filename: nil) {|Integer| ... } ⇒ true
Internally uses chunked stream download, max size of in-memory chunk is 16KB.
Download this file to local directory
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cloudfs/file.rb', line 70
def download(local_destination_path, filename: nil, &block)
fail RestAdapter::Errors::ArgumentError,
'local path is not a valid directory' unless ::File.directory?(local_destination_path)
FileSystemCommon.validate_item_state(self)
filename ||= @name
if local_destination_path[-1] == '/'
local_filepath = "#{local_destination_path}#{filename}"
else
local_filepath = "#{local_destination_path}/#{filename}"
end
::File.open(local_filepath, 'wb') do |file|
downloaded = 0
@rest_adapter.download(@url) do |buffer|
downloaded += buffer.size
file.write(buffer)
yield @size, downloaded if block_given?
end
end
true
end
|
#download_url ⇒ String
Get the download URL of the file.
97 98 99 100 |
# File 'lib/cloudfs/file.rb', line 97
def download_url
url = @rest_adapter.download_url(@url)
URI.extract(url).first.chomp(';')
end
|
#read(bytecount: nil) {|String| ... } ⇒ String
Pass block to stream chunks as soon as available, preferable for large reads.
Read from file
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cloudfs/file.rb', line 141
def read(bytecount: nil, &block)
fail RestAdapter::Errors::ArgumentError,
"Negative length given - #{bytecount}" if bytecount && bytecount < 0
FileSystemCommon.validate_item_state(self)
if bytecount == 0 || @offset >= @size
return yield '' if block_given?
return ''
end
# read till end of file if no bytecount is given
# or offset + bytecount > size of file
bytecount = @size - @offset if bytecount.nil? || (@offset + bytecount > @size)
if block_given?
read_to_proc(bytecount, &block)
else
read_to_buffer(bytecount)
end
end
|
#rewind ⇒ Object
Reset position indicator
163 164 165 |
# File 'lib/cloudfs/file.rb', line 163
def rewind
@offset = 0
end
|
#seek(offset, whence: 0) ⇒ Fixnum
Seek to a particular byte in this file
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/cloudfs/file.rb', line 183
def seek(offset, whence: 0)
case whence
when 0
@offset = offset if whence == 0
when 1
@offset += offset if whence == 1
when 2
@offset = @size + offset if whence == 2
else
fail RestAdapter::Errors::ArgumentError,
'Invalid value of whence, should be 0 or IO::SEEK_SET, 1 or IO::SEEK_CUR, 2 or IO::SEEK_END'
end
@offset
end
|
#tell ⇒ Fixnum
Return current access position in this file
169 170 171 |
# File 'lib/cloudfs/file.rb', line 169
def tell
@offset
end
|