Class: CloudFS::File

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

Overview

File class is aimed to provide native File object like interface to cloudfs files

Examples:

file = session.filesystem.root.upload(local_file_path)
file.seek(4, IO::SEEK_SET) #=> 4
file.tell #=> 4
file.read #=> " is some buffer till end of file"
file.rewind
file.read {|chunk| puts chunk} #=> "this is some buffer till end of file"
file.download(local_folder_path, filename: new_name_of_downloaded_file)

Direct Known Subclasses

Audio, Document, Photo, Video

Instance Attribute Summary collapse

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

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.

See Also:



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

#extensionString

Returns the extension.

Returns:

  • (String)

    the extension.



24
25
26
# File 'lib/cloudfs/file.rb', line 24

def extension
  @extension
end

#mimeString

Returns the mime type of file.

Returns:

  • (String)

    the mime type of file



27
28
29
# File 'lib/cloudfs/file.rb', line 27

def mime
  @mime
end

#sizeString (readonly)

Returns the size.

Returns:

  • (String)

    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

Note:

Internally uses chunked stream download, max size of in-memory chunk is 16KB.

Download this file to local directory

Parameters:

  • local_destination_path (String)

    path of local folder

  • filename (String) (defaults to: nil)

    name of downloaded file, default is name of this file

Yields:

  • (Integer)

    download progress.

Returns:

  • (true)

Raises:

REVIEW:

  • overwrites a file if it exists at local path



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_urlString

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

Note:

Pass block to stream chunks as soon as available, preferable for large reads.

Read from file

Parameters:

  • bytecount (Fixnum) (defaults to: nil)

    number of bytes to read from current access position, default reads upto end of file

Yields:

  • (String)

    chunk data as soon as available, chunksize size may vary each time

Returns:

  • (String)

    buffer, unless block is given

Raises:



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

#rewindObject

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

Parameters:

  • offset (Fixnum)

    offset in this file to seek to

  • whence (Fixnum) (defaults to: 0)

    defaults 0, If whence is 0 file offset shall be set to offset bytes If whence is 1, the file offset shall be set to its current location plus offset If whence is 2, the file offset shall be set to the size of the file plus offset

Returns:

  • (Fixnum)

    resulting offset

Raises:



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

#tellFixnum

Return current access position in this file

Returns:

  • (Fixnum)

    current position in file



169
170
171
# File 'lib/cloudfs/file.rb', line 169

def tell
  @offset
end