Class: Files::File

Inherits:
Object
  • Object
show all
Defined in:
lib/files.com/models/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ File

Returns a new instance of File.



188
189
190
191
192
193
194
195
# File 'lib/files.com/models/file.rb', line 188

def initialize(*args)
  @attributes = (args[0].is_a?(Hash) && args[0]) || {}
  @options = (args[1].is_a?(Hash) && args[1]) || {}
  @attributes[:path] = args[0] if args[0].is_a?(String)
  @mode = args[1] || 'r' if args[1].is_a?(String)
  @write_io = StringIO.new
  @bytes_written = 0
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/files.com/models/file.rb', line 5

def attributes
  @attributes
end

#lineno(*_args) ⇒ Object



409
410
411
# File 'lib/files.com/models/file.rb', line 409

def lineno(*_args)
  @lineno ||= 0
end

#modeObject (readonly)

Returns the value of attribute mode.



6
7
8
# File 'lib/files.com/models/file.rb', line 6

def mode
  @mode
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/files.com/models/file.rb', line 5

def options
  @options
end

#posObject



435
436
437
# File 'lib/files.com/models/file.rb', line 435

def pos
  @pos ||= 0
end

#syncObject



517
518
519
# File 'lib/files.com/models/file.rb', line 517

def sync
  @sync ||= false
end

Class Method Details

.binread(name, *args) ⇒ Object



8
9
10
# File 'lib/files.com/models/file.rb', line 8

def self.binread(name, *args)
  new(name).read(*args)
end

.binwrite(name, *args) ⇒ Object



12
13
14
# File 'lib/files.com/models/file.rb', line 12

def self.binwrite(name, *args)
  new(name).write(*args)
end

.chmod(*_args) ⇒ Object



16
17
18
# File 'lib/files.com/models/file.rb', line 16

def self.chmod(*_args)
  raise NotImplementedError
end

.chown(*_args) ⇒ Object



20
21
22
# File 'lib/files.com/models/file.rb', line 20

def self.chown(*_args)
  raise NotImplementedError
end

.client(options = {}) ⇒ Object



24
25
26
# File 'lib/files.com/models/file.rb', line 24

def self.client(options = {})
  options[:client] || ApiClient.active_client
end

.copy(old_path, new_path) ⇒ Object



28
29
30
# File 'lib/files.com/models/file.rb', line 28

def self.copy(old_path, new_path)
  FileAction.copy(old_path, destination: new_path)
end

.copy_stream(*_args) ⇒ Object



32
33
34
# File 'lib/files.com/models/file.rb', line 32

def self.copy_stream(*_args)
  raise NotImplementedError
end

.create(path, params = {}, options = {}) ⇒ Object

Upload file

Parameters:

action - string - The action to perform.  Can be `append`, `attachment`, `end`, `upload`, `put`, or may not exist
etags[etag] (required) - array - etag identifier.
etags[part] (required) - array - Part number.
length - integer - Length of file.
mkdir_parents - boolean - Create parent directories if they do not exist?
part - integer - Part if uploading a part.
parts - integer - How many parts to fetch?
provided_mtime - string - User provided modification time.
ref - string -
restart - integer - File byte offset to restart from.
size - integer - Size of file.
structure - string - If copying folder, copy just the structure?
with_rename - boolean - Allow file rename instead of overwrite?


948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'lib/files.com/models/file.rb', line 948

def self.create(path, params = {}, options = {})
  params ||= {}
  params[:path] = path
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: action must be an String") if params.dig(:action) and !params.dig(:action).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: length must be an Integer") if params.dig(:length) and !params.dig(:length).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: part must be an Integer") if params.dig(:part) and !params.dig(:part).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: parts must be an Integer") if params.dig(:parts) and !params.dig(:parts).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: provided_mtime must be an String") if params.dig(:provided_mtime) and !params.dig(:provided_mtime).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: ref must be an String") if params.dig(:ref) and !params.dig(:ref).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: restart must be an Integer") if params.dig(:restart) and !params.dig(:restart).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: size must be an Integer") if params.dig(:size) and !params.dig(:size).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: structure must be an String") if params.dig(:structure) and !params.dig(:structure).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, options = Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :post, params, options)
  File.new(response.data, options)
end

.delete(path, params = {}, options = {}) ⇒ Object

Parameters:

recursive - boolean - If true, will recursively delete folers.  Otherwise, will error on non-empty folders.  For legacy reasons, this parameter may also be provided as the HTTP header `Depth: Infinity`


984
985
986
987
988
989
990
991
992
# File 'lib/files.com/models/file.rb', line 984

def self.delete(path, params = {}, options = {})
  params ||= {}
  params[:path] = path
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, _options = Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :delete, params, options)
  response.data
end

.destroy(path, params = {}, options = {}) ⇒ Object



994
995
996
# File 'lib/files.com/models/file.rb', line 994

def self.destroy(path, params = {}, options = {})
  delete(path, params, options)
end

.directory?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/files.com/models/file.rb', line 36

def self.directory?(path, options = {})
  find(path, {}, options).type == "directory"
end

.download(path, params = {}, options = {}) ⇒ Object

Download file

Parameters:

action - string - Can be blank, `redirect` or `stat`.  If set to `stat`, we will return file information but without a download URL, and without logging a download.  If set to `redirect` we will serve a 302 redirect directly to the file.  This is used for integrations with Zapier, and is not recommended for most integrations.
id - integer - If provided, lookup the file by id instead of path.
with_previews - boolean - Include file preview information?
with_priority_color - boolean - Include file priority color information?


920
921
922
923
924
925
926
927
928
929
930
# File 'lib/files.com/models/file.rb', line 920

def self.download(path, params = {}, options = {})
  params ||= {}
  params[:path] = path
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: action must be an String") if params.dig(:action) and !params.dig(:action).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: id must be an Integer") if params.dig(:id) and !params.dig(:id).is_a?(Integer)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, options = Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :get, params, options)
  File.new(response.data, options)
end

.download_file(path, local_path = nil) ⇒ Object



40
41
42
43
# File 'lib/files.com/models/file.rb', line 40

def self.download_file(path, local_path = nil)
  local_path ||= File.basename(path)
  new(path).download_file(local_path)
end

.exist?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
# File 'lib/files.com/models/file.rb', line 45

def self.exist?(path, options = {})
  find(path, {}, options)
  true
rescue ApiError => e
  if e.code == 404
    false
  else
    raise e
  end
end

.exists?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/files.com/models/file.rb', line 56

def self.exists?(path, options = {})
  exist?(path, options)
end

.find(path, params = {}, options = {}) ⇒ Object



60
61
62
63
# File 'lib/files.com/models/file.rb', line 60

def self.find(path, params = {}, options = {})
  params[:action] = "stat"
  download(path, params, options)
end

.for_fd(*_args) ⇒ Object



65
66
67
# File 'lib/files.com/models/file.rb', line 65

def self.for_fd(*_args)
  raise NotImplementedError
end

.foreach(name, *args, &block) ⇒ Object



69
70
71
# File 'lib/files.com/models/file.rb', line 69

def self.foreach(name, *args, &block)
  new(name).each(*args, &block)
end

.get(path, params = {}, options = {}) ⇒ Object



73
74
75
# File 'lib/files.com/models/file.rb', line 73

def self.get(path, params = {}, options = {})
  find(path, params, options)
end

.identical?(path1, path2) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/files.com/models/file.rb', line 77

def self.identical?(path1, path2)
  new(path1).crc32 == new(path2).crc32
end

.lstat(path) ⇒ Object



81
82
83
# File 'lib/files.com/models/file.rb', line 81

def self.lstat(path)
  new(path).stat
end

.move(old_path, new_path) ⇒ Object



85
86
87
# File 'lib/files.com/models/file.rb', line 85

def self.move(old_path, new_path)
  FileAction.move(old_path, destination: new_path)
end

.mtime(path) ⇒ Object



89
90
91
# File 'lib/files.com/models/file.rb', line 89

def self.mtime(path)
  new(path).mtime
end

.open(path, mode = "r", &block) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/files.com/models/file.rb', line 93

def self.open(path, mode = "r", &block)
  file = new(path, mode)
  if block
    yield file
    file.close
  end
  file
end

.owned?(_path) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



102
103
104
# File 'lib/files.com/models/file.rb', line 102

def self.owned?(_path)
  raise NotImplementedError
end

.pipe(*_args) ⇒ Object



106
107
108
# File 'lib/files.com/models/file.rb', line 106

def self.pipe(*_args)
  raise NotImplementedError
end

.popen(*_args) ⇒ Object



110
111
112
# File 'lib/files.com/models/file.rb', line 110

def self.popen(*_args)
  raise NotImplementedError
end

.read(name, *args) ⇒ Object



114
115
116
# File 'lib/files.com/models/file.rb', line 114

def self.read(name, *args)
  new(name).read(*args)
end

.readable?(path) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/files.com/models/file.rb', line 118

def self.readable?(path)
  new(path).stat.permissions.include?("read")
end

.readlines(name, *args) ⇒ Object



122
123
124
# File 'lib/files.com/models/file.rb', line 122

def self.readlines(name, *args)
  new(name).readlines(*args)
end

.rename(old_path, new_path) ⇒ Object



126
127
128
# File 'lib/files.com/models/file.rb', line 126

def self.rename(old_path, new_path)
  FileAction.move(old_path, destination: new_path)
end

.select(*_args) ⇒ Object



130
131
132
# File 'lib/files.com/models/file.rb', line 130

def self.select(*_args)
  raise NotImplementedError
end

.stat(path) ⇒ Object



134
135
136
# File 'lib/files.com/models/file.rb', line 134

def self.stat(path)
  new(path).stat
end

.sysopen(*_args) ⇒ Object



138
139
140
# File 'lib/files.com/models/file.rb', line 138

def self.sysopen(*_args)
  raise NotImplementedError
end

.try_convert(*_args) ⇒ Object



142
143
144
# File 'lib/files.com/models/file.rb', line 142

def self.try_convert(*_args)
  raise NotImplementedError
end


146
147
148
# File 'lib/files.com/models/file.rb', line 146

def self.unlink(*paths)
  paths.map { |p| delete(p) }
end

.update(path, params = {}, options = {}) ⇒ Object

Parameters:

provided_mtime - string - Modified time of file.
priority_color - string - Priority/Bookmark color of file.


970
971
972
973
974
975
976
977
978
979
980
# File 'lib/files.com/models/file.rb', line 970

def self.update(path, params = {}, options = {})
  params ||= {}
  params[:path] = path
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: provided_mtime must be an String") if params.dig(:provided_mtime) and !params.dig(:provided_mtime).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: priority_color must be an String") if params.dig(:priority_color) and !params.dig(:priority_color).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, options = Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :patch, params, options)
  File.new(response.data, options)
end

.upload_chunks(io, path, options, upload = nil, etags = []) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/files.com/models/file.rb', line 150

def self.upload_chunks(io, path, options, upload = nil, etags = [])
  etags ||= []
  loop do
    upload = FileAction.begin_upload(path, { ref: upload&.ref, part: (upload&.part_number || 0) + 1 }, options)
    buf = io.read(upload.partsize) || ""
    method = upload.http_method.downcase.to_sym
    response = client(options).remote_request(method, upload.upload_uri, { "Content-Length": buf.length.to_s }, buf)
    etags << { etag: response.headers["ETag"], part: upload.part_number }
    return upload, etags if io.eof?
  end
end

.upload_file(path, destination = nil, options = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/files.com/models/file.rb', line 162

def self.upload_file(path, destination = nil, options = {})
  local_file = ::File.open(path, 'r')
  destination ||= File.basename(path)
  upload, etags = upload_chunks(local_file, destination, options)

  params = {
    action: "end",
    etags: etags,
    provided_mtime: local_file.mtime.to_s,
    ref: upload.ref,
    size: local_file.size
  }

  create(destination, params, options)
ensure
  local_file.close
end

.write(*_args) ⇒ Object



180
181
182
# File 'lib/files.com/models/file.rb', line 180

def self.write(*_args)
  raise NotImplementedError
end

.zero?(path) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/files.com/models/file.rb', line 184

def self.zero?(path)
  new(path).empty?
end

Instance Method Details

#actionObject

string - The action to perform. Can be ‘append`, `attachment`, `end`, `upload`, `put`, or may not exist



739
740
741
# File 'lib/files.com/models/file.rb', line 739

def action
  @attributes[:action]
end

#action=(value) ⇒ Object



743
744
745
# File 'lib/files.com/models/file.rb', line 743

def action=(value)
  @attributes[:action] = value
end

#advise(*_args) ⇒ Object



197
# File 'lib/files.com/models/file.rb', line 197

def advise(*_args); end

#atimeObject



199
200
201
# File 'lib/files.com/models/file.rb', line 199

def atime
  mtime
end

#autoclose=(*_args) ⇒ Object



203
# File 'lib/files.com/models/file.rb', line 203

def autoclose=(*_args); end

#autoclose?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


205
# File 'lib/files.com/models/file.rb', line 205

def autoclose?(*_args); end

#binmodeObject



207
208
209
# File 'lib/files.com/models/file.rb', line 207

def binmode
  binmode?
end

#binmode?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/files.com/models/file.rb', line 211

def binmode?
  true
end

#birthtimeObject



215
216
217
# File 'lib/files.com/models/file.rb', line 215

def birthtime
  raise NotImplementedError
end

#bytesObject



219
220
221
# File 'lib/files.com/models/file.rb', line 219

def bytes
  read_io.bytes
end

#charsObject



223
224
225
# File 'lib/files.com/models/file.rb', line 223

def chars
  read_io.chars
end

#chmod(*_args) ⇒ Object



227
228
229
# File 'lib/files.com/models/file.rb', line 227

def chmod(*_args)
  raise NotImplementedError
end

#chown(*_args) ⇒ Object



231
232
233
# File 'lib/files.com/models/file.rb', line 231

def chown(*_args)
  raise NotImplementedError
end

#clientObject



235
236
237
# File 'lib/files.com/models/file.rb', line 235

def client
  options[:client] || ApiClient.active_client
end

#closeObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/files.com/models/file.rb', line 239

def close
  flush

  if @upload
    end_options = {
      "action": "end",
      "etags": @etags,
      "provided_mtime": Time.now.to_s,
      "ref": @upload.ref,
      "size": @bytes_written
    }

    file = File.create(path, end_options, @options)
    @attributes = file.attributes
    @upload = nil
  end
  @write_io.close
end

#close_on_exec=(*args) ⇒ Object



262
263
264
# File 'lib/files.com/models/file.rb', line 262

def close_on_exec=(*args)
  @write_io.close_on_exec = *args
end

#close_on_exec?(*args) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/files.com/models/file.rb', line 258

def close_on_exec?(*args)
  @write_io.close_on_exec? *args
end

#close_read(*args) ⇒ Object



266
267
268
# File 'lib/files.com/models/file.rb', line 266

def close_read(*args)
  @write_io.close_read *args
end

#close_write(*args) ⇒ Object



270
271
272
# File 'lib/files.com/models/file.rb', line 270

def close_write(*args)
  @write_io.close_write *args
end

#closed?(*args) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/files.com/models/file.rb', line 274

def closed?(*args)
  @write_io.closed? *args
end

#codepoints(*args, &block) ⇒ Object



278
279
280
# File 'lib/files.com/models/file.rb', line 278

def codepoints(*args, &block)
  @write_io.codepoints *args, &block
end

#copy(destination) ⇒ Object



282
283
284
# File 'lib/files.com/models/file.rb', line 282

def copy(destination)
  File.copy(path, destination)
end

#crc32Object

string - File CRC32 checksum. This is sometimes delayed, so if you get a blank response, wait and try again.



649
650
651
# File 'lib/files.com/models/file.rb', line 649

def crc32
  @attributes[:crc32]
end

#crc32=(value) ⇒ Object



653
654
655
# File 'lib/files.com/models/file.rb', line 653

def crc32=(value)
  @attributes[:crc32] = value
end

#create(params = {}) ⇒ Object

Upload file

Parameters:

action - string - The action to perform.  Can be `append`, `attachment`, `end`, `upload`, `put`, or may not exist
etags[etag] (required) - array - etag identifier.
etags[part] (required) - array - Part number.
length - integer - Length of file.
mkdir_parents - boolean - Create parent directories if they do not exist?
part - integer - Part if uploading a part.
parts - integer - How many parts to fetch?
provided_mtime - string - User provided modification time.
ref - string -
restart - integer - File byte offset to restart from.
size - integer - Size of file.
structure - string - If copying folder, copy just the structure?
with_rename - boolean - Allow file rename instead of overwrite?


854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# File 'lib/files.com/models/file.rb', line 854

def create(params = {})
  params ||= {}
  params[:path] = @attributes[:path]
  raise MissingParameterError.new("Current object doesn't have a path") unless @attributes[:path]
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: action must be an String") if params.dig(:action) and !params.dig(:action).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: length must be an Integer") if params.dig(:length) and !params.dig(:length).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: part must be an Integer") if params.dig(:part) and !params.dig(:part).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: parts must be an Integer") if params.dig(:parts) and !params.dig(:parts).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: provided_mtime must be an String") if params.dig(:provided_mtime) and !params.dig(:provided_mtime).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: ref must be an String") if params.dig(:ref) and !params.dig(:ref).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: restart must be an Integer") if params.dig(:restart) and !params.dig(:restart).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: size must be an Integer") if params.dig(:size) and !params.dig(:size).is_a?(Integer)
  raise InvalidParameterError.new("Bad parameter: structure must be an String") if params.dig(:structure) and !params.dig(:structure).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :post, params, @options)
end

#ctime(*_args) ⇒ Object



286
287
288
# File 'lib/files.com/models/file.rb', line 286

def ctime(*_args)
  mtime
end

#delete(params = {}) ⇒ Object

Parameters:

recursive - boolean - If true, will recursively delete folers.  Otherwise, will error on non-empty folders.  For legacy reasons, this parameter may also be provided as the HTTP header `Depth: Infinity`


890
891
892
893
894
895
896
897
898
# File 'lib/files.com/models/file.rb', line 890

def delete(params = {})
  params ||= {}
  params[:path] = @attributes[:path]
  raise MissingParameterError.new("Current object doesn't have a path") unless @attributes[:path]
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :delete, params, @options)
end

#destroy(params = {}) ⇒ Object



900
901
902
# File 'lib/files.com/models/file.rb', line 900

def destroy(params = {})
  delete(params)
end

#display_nameObject

string - File/Folder display name



604
605
606
# File 'lib/files.com/models/file.rb', line 604

def display_name
  @attributes[:display_name]
end

#display_name=(value) ⇒ Object



608
609
610
# File 'lib/files.com/models/file.rb', line 608

def display_name=(value)
  @attributes[:display_name] = value
end

#download(params = {}) ⇒ Object

Download file

Parameters:

action - string - Can be blank, `redirect` or `stat`.  If set to `stat`, we will return file information but without a download URL, and without logging a download.  If set to `redirect` we will serve a 302 redirect directly to the file.  This is used for integrations with Zapier, and is not recommended for most integrations.
id - integer - If provided, lookup the file by id instead of path.
with_previews - boolean - Include file preview information?
with_priority_color - boolean - Include file priority color information?


826
827
828
829
830
831
832
833
834
835
836
# File 'lib/files.com/models/file.rb', line 826

def download(params = {})
  params ||= {}
  params[:path] = @attributes[:path]
  raise MissingParameterError.new("Current object doesn't have a path") unless @attributes[:path]
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: action must be an String") if params.dig(:action) and !params.dig(:action).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: id must be an Integer") if params.dig(:id) and !params.dig(:id).is_a?(Integer)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :get, params, @options)
end

#download_content(io) ⇒ Object



304
305
306
307
# File 'lib/files.com/models/file.rb', line 304

def download_content(io)
  response = client.remote_request(:get, download_uri_with_load)
  io.write(response.body)
end

#download_file(output_file) ⇒ Object



298
299
300
301
302
# File 'lib/files.com/models/file.rb', line 298

def download_file(output_file)
  ::File.open(output_file, 'wb') do |file|
    download_content(file)
  end
end

#download_uriObject

string - Link to download file. Provided only in response to a download request.



703
704
705
# File 'lib/files.com/models/file.rb', line 703

def download_uri
  @attributes[:download_uri]
end

#download_uri=(value) ⇒ Object



707
708
709
# File 'lib/files.com/models/file.rb', line 707

def download_uri=(value)
  @attributes[:download_uri] = value
end

#download_uri_with_loadObject



290
291
292
293
294
295
296
# File 'lib/files.com/models/file.rb', line 290

def download_uri_with_load
  return download_uri if download_uri

  file = File.download(path, {}, @options)
  @attributes = file.attributes
  download_uri
end

#each(*args, &block) ⇒ Object



309
310
311
# File 'lib/files.com/models/file.rb', line 309

def each(*args, &block)
  read_io.each *args, &block
end

#each_byte(*args, &block) ⇒ Object



313
314
315
# File 'lib/files.com/models/file.rb', line 313

def each_byte(*args, &block)
  read_io.each_byte *args, &block
end

#each_char(*args, &block) ⇒ Object



317
318
319
# File 'lib/files.com/models/file.rb', line 317

def each_char(*args, &block)
  read_io.each_char *args, &block
end

#each_codepoint(*args, &block) ⇒ Object



321
322
323
# File 'lib/files.com/models/file.rb', line 321

def each_codepoint(*args, &block)
  read_io.each_codepoint *args, &block
end

#each_line(*args, &block) ⇒ Object



325
326
327
# File 'lib/files.com/models/file.rb', line 325

def each_line(*args, &block)
  each(*args, &block)
end

#empty?Boolean

Returns:

  • (Boolean)


329
330
331
# File 'lib/files.com/models/file.rb', line 329

def empty?
  size == 0
end

#eofObject



333
334
335
# File 'lib/files.com/models/file.rb', line 333

def eof
  eof?
end

#eof?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'lib/files.com/models/file.rb', line 337

def eof?
  @write_io.eof?
end

#external_encoding(*args) ⇒ Object



341
342
343
# File 'lib/files.com/models/file.rb', line 341

def external_encoding(*args)
  internal_encoding *args
end

#fcntl(*_args) ⇒ Object



345
346
347
# File 'lib/files.com/models/file.rb', line 345

def fcntl(*_args)
  raise NotImplementedError
end

#fdatasync(*_args) ⇒ Object



349
350
351
# File 'lib/files.com/models/file.rb', line 349

def fdatasync(*_args)
  flush
end

#fileno(*_args) ⇒ Object



353
354
355
# File 'lib/files.com/models/file.rb', line 353

def fileno(*_args)
  id
end

#flock(*_args) ⇒ Object



357
358
359
# File 'lib/files.com/models/file.rb', line 357

def flock(*_args)
  raise NotImplementedError
end

#flush(*_args) ⇒ Object



361
362
363
364
365
366
367
368
369
370
# File 'lib/files.com/models/file.rb', line 361

def flush(*_args)
  if mode.include? "w"
    @write_io.rewind if @write_io.is_a?(StringIO)

    @bytes_written += @write_io.size
    @upload, @etags = File.upload_chunks(@write_io, path, options, @upload, @etags)
  elsif mode.include? "a"
    raise NotImplementedError
  end
end

#fsync(*args) ⇒ Object



372
373
374
# File 'lib/files.com/models/file.rb', line 372

def fsync(*args)
  flush *args
end

#getbyte(*args) ⇒ Object



376
377
378
# File 'lib/files.com/models/file.rb', line 376

def getbyte(*args)
  read_io.getbyte *args
end

#getc(*args) ⇒ Object



380
381
382
# File 'lib/files.com/models/file.rb', line 380

def getc(*args)
  read_io.getc *args
end

#gets(*args) ⇒ Object



384
385
386
# File 'lib/files.com/models/file.rb', line 384

def gets(*args)
  read_io.gets *args
end

#idObject

int64 - File/Folder ID



586
587
588
# File 'lib/files.com/models/file.rb', line 586

def id
  @attributes[:id]
end

#id=(value) ⇒ Object



590
591
592
# File 'lib/files.com/models/file.rb', line 590

def id=(value)
  @attributes[:id] = value
end

#internal_encoding(*_args) ⇒ Object



397
398
399
# File 'lib/files.com/models/file.rb', line 397

def internal_encoding(*_args)
  "".encoding
end

#ioctl(*_args) ⇒ Object



401
402
403
# File 'lib/files.com/models/file.rb', line 401

def ioctl(*_args)
  raise NotImplementedError
end

#isatty(*_args) ⇒ Object



405
406
407
# File 'lib/files.com/models/file.rb', line 405

def isatty(*_args)
  false
end

#lengthObject

int64 - Length of file.



748
749
750
# File 'lib/files.com/models/file.rb', line 748

def length
  @attributes[:length]
end

#length=(value) ⇒ Object



752
753
754
# File 'lib/files.com/models/file.rb', line 752

def length=(value)
  @attributes[:length] = value
end

#lines(*args, &block) ⇒ Object



415
416
417
# File 'lib/files.com/models/file.rb', line 415

def lines(*args, &block)
  each_line *args, &block
end

#lstat(*_args) ⇒ Object



419
420
421
# File 'lib/files.com/models/file.rb', line 419

def lstat(*_args)
  stats
end

#md5Object

string - File MD5 checksum. This is sometimes delayed, so if you get a blank response, wait and try again.



658
659
660
# File 'lib/files.com/models/file.rb', line 658

def md5
  @attributes[:md5]
end

#md5=(value) ⇒ Object



662
663
664
# File 'lib/files.com/models/file.rb', line 662

def md5=(value)
  @attributes[:md5] = value
end

#mime_typeObject

string - MIME Type. This is determined by the filename extension and is not stored separately internally.



667
668
669
# File 'lib/files.com/models/file.rb', line 667

def mime_type
  @attributes[:mime_type]
end

#mime_type=(value) ⇒ Object



671
672
673
# File 'lib/files.com/models/file.rb', line 671

def mime_type=(value)
  @attributes[:mime_type] = value
end

#mkdir_parentsObject

boolean - Create parent directories if they do not exist?



757
758
759
# File 'lib/files.com/models/file.rb', line 757

def mkdir_parents
  @attributes[:mkdir_parents]
end

#mkdir_parents=(value) ⇒ Object



761
762
763
# File 'lib/files.com/models/file.rb', line 761

def mkdir_parents=(value)
  @attributes[:mkdir_parents] = value
end

#move(destination) ⇒ Object



423
424
425
# File 'lib/files.com/models/file.rb', line 423

def move(destination)
  File.move(path, destination)
end

#mtimeObject

date-time - File last modified date/time, according to the server. This is the timestamp of the last Files.com operation of the file, regardless of what modified timestamp was sent.



631
632
633
# File 'lib/files.com/models/file.rb', line 631

def mtime
  @attributes[:mtime]
end

#mtime=(value) ⇒ Object



635
636
637
# File 'lib/files.com/models/file.rb', line 635

def mtime=(value)
  @attributes[:mtime] = value
end

#mv(destination) ⇒ Object



427
428
429
# File 'lib/files.com/models/file.rb', line 427

def mv(destination)
  File.move(path, destination)
end

#partObject

int64 - Part if uploading a part.



766
767
768
# File 'lib/files.com/models/file.rb', line 766

def part
  @attributes[:part]
end

#part=(value) ⇒ Object



770
771
772
# File 'lib/files.com/models/file.rb', line 770

def part=(value)
  @attributes[:part] = value
end

#partsObject

int64 - How many parts to fetch?



775
776
777
# File 'lib/files.com/models/file.rb', line 775

def parts
  @attributes[:parts]
end

#parts=(value) ⇒ Object



779
780
781
# File 'lib/files.com/models/file.rb', line 779

def parts=(value)
  @attributes[:parts] = value
end

#pathObject

string - File/Folder path This must be slash-delimited, but it must neither start nor end with a slash. Maximum of 5000 characters.



595
596
597
# File 'lib/files.com/models/file.rb', line 595

def path
  @attributes[:path]
end

#path=(value) ⇒ Object



599
600
601
# File 'lib/files.com/models/file.rb', line 599

def path=(value)
  @attributes[:path] = value
end

#permissionsObject

string - A short string representing the current user’s permissions. Can be ‘r`,`w`,`p`, or any combination



685
686
687
# File 'lib/files.com/models/file.rb', line 685

def permissions
  @attributes[:permissions]
end

#permissions=(value) ⇒ Object



689
690
691
# File 'lib/files.com/models/file.rb', line 689

def permissions=(value)
  @attributes[:permissions] = value
end

#pid(*_args) ⇒ Object



431
432
433
# File 'lib/files.com/models/file.rb', line 431

def pid(*_args)
  Process.pid
end

#pread(*args) ⇒ Object



441
442
443
# File 'lib/files.com/models/file.rb', line 441

def pread(*args)
  read_io.pread *args
end

#previewObject

File preview



730
731
732
# File 'lib/files.com/models/file.rb', line 730

def preview
  @attributes[:preview]
end

#preview=(value) ⇒ Object



734
735
736
# File 'lib/files.com/models/file.rb', line 734

def preview=(value)
  @attributes[:preview] = value
end

#preview_idObject

int64 - File preview ID



721
722
723
# File 'lib/files.com/models/file.rb', line 721

def preview_id
  @attributes[:preview_id]
end

#preview_id=(value) ⇒ Object



725
726
727
# File 'lib/files.com/models/file.rb', line 725

def preview_id=(value)
  @attributes[:preview_id] = value
end


445
446
447
# File 'lib/files.com/models/file.rb', line 445

def print(*args)
  @write_io.print *args
end

#printf(*args) ⇒ Object



449
450
451
# File 'lib/files.com/models/file.rb', line 449

def printf(*args)
  @write_io.printf *args
end

#priority_colorObject

string - Bookmark/priority color of file/folder



712
713
714
# File 'lib/files.com/models/file.rb', line 712

def priority_color
  @attributes[:priority_color]
end

#priority_color=(value) ⇒ Object



716
717
718
# File 'lib/files.com/models/file.rb', line 716

def priority_color=(value)
  @attributes[:priority_color] = value
end

#provided_mtimeObject

date-time - File last modified date/time, according to the client who set it. Files.com allows desktop, FTP, SFTP, and WebDAV clients to set modified at times. This allows Desktop<->Cloud syncing to preserve modified at times.



640
641
642
# File 'lib/files.com/models/file.rb', line 640

def provided_mtime
  @attributes[:provided_mtime]
end

#provided_mtime=(value) ⇒ Object



644
645
646
# File 'lib/files.com/models/file.rb', line 644

def provided_mtime=(value)
  @attributes[:provided_mtime] = value
end

#putc(*args) ⇒ Object



453
454
455
# File 'lib/files.com/models/file.rb', line 453

def putc(*args)
  @write_io.putc *args
end

#puts(*args) ⇒ Object



457
458
459
# File 'lib/files.com/models/file.rb', line 457

def puts(*args)
  @write_io.puts *args
end

#pwrite(*args) ⇒ Object



461
462
463
# File 'lib/files.com/models/file.rb', line 461

def pwrite(*args)
  @write_io.pwrite *args
end

#read(*args) ⇒ Object



465
466
467
# File 'lib/files.com/models/file.rb', line 465

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

#read_ioObject



388
389
390
391
392
393
394
395
# File 'lib/files.com/models/file.rb', line 388

def read_io
  @read_io ||= begin
    io = StringIO.new
    download_content(io)
    io.rewind
    io
  end
end

#read_nonblock(*args) ⇒ Object



469
470
471
# File 'lib/files.com/models/file.rb', line 469

def read_nonblock(*args)
  read_io.read_nonblock *args
end

#readbyte(*args) ⇒ Object



473
474
475
# File 'lib/files.com/models/file.rb', line 473

def readbyte(*args)
  read_io.readbyte *args
end

#readchar(*args) ⇒ Object



477
478
479
# File 'lib/files.com/models/file.rb', line 477

def readchar(*args)
  read_io.readchar *args
end

#readline(*args) ⇒ Object



481
482
483
# File 'lib/files.com/models/file.rb', line 481

def readline(*args)
  read_io.readline *args
end

#readlines(*args) ⇒ Object



485
486
487
# File 'lib/files.com/models/file.rb', line 485

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

#readpartial(*args) ⇒ Object



489
490
491
# File 'lib/files.com/models/file.rb', line 489

def readpartial(*args)
  read_io.readpartial *args
end

#refObject

string -



784
785
786
# File 'lib/files.com/models/file.rb', line 784

def ref
  @attributes[:ref]
end

#ref=(value) ⇒ Object



788
789
790
# File 'lib/files.com/models/file.rb', line 788

def ref=(value)
  @attributes[:ref] = value
end

#regionObject

string - Region location



676
677
678
# File 'lib/files.com/models/file.rb', line 676

def region
  @attributes[:region]
end

#region=(value) ⇒ Object



680
681
682
# File 'lib/files.com/models/file.rb', line 680

def region=(value)
  @attributes[:region] = value
end

#rename(destination) ⇒ Object



493
494
495
# File 'lib/files.com/models/file.rb', line 493

def rename(destination)
  File.rename(path, destination)
end

#reopen(*_args) ⇒ Object



497
498
499
# File 'lib/files.com/models/file.rb', line 497

def reopen(*_args)
  raise NotImplementedError
end

#restartObject

int64 - File byte offset to restart from.



793
794
795
# File 'lib/files.com/models/file.rb', line 793

def restart
  @attributes[:restart]
end

#restart=(value) ⇒ Object



797
798
799
# File 'lib/files.com/models/file.rb', line 797

def restart=(value)
  @attributes[:restart] = value
end

#rewindObject



501
502
503
# File 'lib/files.com/models/file.rb', line 501

def rewind
  @pos = 0
end

#saveObject



904
905
906
907
908
909
910
911
# File 'lib/files.com/models/file.rb', line 904

def save
  if @attributes[:path]
    update(@attributes)
  else
    new_obj = File.create(@attributes, @options)
    @attributes = new_obj.attributes
  end
end

#seek(pos) ⇒ Object



505
506
507
# File 'lib/files.com/models/file.rb', line 505

def seek(pos)
  @pos = pos
end

#set_encoding(*_args) ⇒ Object

rubocop:disable Naming/AccessorMethodName



509
510
511
# File 'lib/files.com/models/file.rb', line 509

def set_encoding(*_args) # rubocop:disable Naming/AccessorMethodName
  raise NotImplementedError
end

#sizeObject

int64 - File/Folder size



622
623
624
# File 'lib/files.com/models/file.rb', line 622

def size
  @attributes[:size]
end

#size=(value) ⇒ Object



626
627
628
# File 'lib/files.com/models/file.rb', line 626

def size=(value)
  @attributes[:size] = value
end

#stat(*_args) ⇒ Object



513
514
515
# File 'lib/files.com/models/file.rb', line 513

def stat(*_args)
  stats
end

#structureObject

string - If copying folder, copy just the structure?



802
803
804
# File 'lib/files.com/models/file.rb', line 802

def structure
  @attributes[:structure]
end

#structure=(value) ⇒ Object



806
807
808
# File 'lib/files.com/models/file.rb', line 806

def structure=(value)
  @attributes[:structure] = value
end

#subfolders_locked=(value) ⇒ Object



698
699
700
# File 'lib/files.com/models/file.rb', line 698

def subfolders_locked=(value)
  @attributes[:subfolders_locked?] = value
end

#subfolders_locked?Boolean

boolean - Are subfolders locked and unable to be modified?

Returns:

  • (Boolean)


694
695
696
# File 'lib/files.com/models/file.rb', line 694

def subfolders_locked?
  @attributes[:subfolders_locked?]
end

#sysread(*args) ⇒ Object



523
524
525
# File 'lib/files.com/models/file.rb', line 523

def sysread(*args)
  read *args
end

#sysseek(*args) ⇒ Object



527
528
529
# File 'lib/files.com/models/file.rb', line 527

def sysseek(*args)
  seek *args
end

#syswrite(*_args) ⇒ Object



531
532
533
# File 'lib/files.com/models/file.rb', line 531

def syswrite(*_args)
  raise NotImplementedError
end

#tellObject



535
536
537
# File 'lib/files.com/models/file.rb', line 535

def tell
  pos
end

#to_i(*_args) ⇒ Object



539
540
541
# File 'lib/files.com/models/file.rb', line 539

def to_i(*_args)
  fileno
end

#to_io(*_args) ⇒ Object



543
544
545
# File 'lib/files.com/models/file.rb', line 543

def to_io(*_args)
  @write_io
end

#to_path(*_args) ⇒ Object



547
548
549
# File 'lib/files.com/models/file.rb', line 547

def to_path(*_args)
  path
end

#truncate(*_args) ⇒ Object



551
552
553
# File 'lib/files.com/models/file.rb', line 551

def truncate(*_args)
  raise NotImplementedError
end

#tty?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


555
556
557
# File 'lib/files.com/models/file.rb', line 555

def tty?(*_args)
  false
end

#typeObject

string - Type: ‘directory` or `file`.



613
614
615
# File 'lib/files.com/models/file.rb', line 613

def type
  @attributes[:type]
end

#type=(value) ⇒ Object



617
618
619
# File 'lib/files.com/models/file.rb', line 617

def type=(value)
  @attributes[:type] = value
end

#ungetbyte(*_args) ⇒ Object



559
560
561
# File 'lib/files.com/models/file.rb', line 559

def ungetbyte(*_args)
  raise NotImplementedError
end

#ungetc(*_args) ⇒ Object



563
564
565
# File 'lib/files.com/models/file.rb', line 563

def ungetc(*_args)
  raise NotImplementedError
end

#update(params = {}) ⇒ Object

Parameters:

provided_mtime - string - Modified time of file.
priority_color - string - Priority/Bookmark color of file.


876
877
878
879
880
881
882
883
884
885
886
# File 'lib/files.com/models/file.rb', line 876

def update(params = {})
  params ||= {}
  params[:path] = @attributes[:path]
  raise MissingParameterError.new("Current object doesn't have a path") unless @attributes[:path]
  raise InvalidParameterError.new("Bad parameter: path must be an String") if params.dig(:path) and !params.dig(:path).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: provided_mtime must be an String") if params.dig(:provided_mtime) and !params.dig(:provided_mtime).is_a?(String)
  raise InvalidParameterError.new("Bad parameter: priority_color must be an String") if params.dig(:priority_color) and !params.dig(:priority_color).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  Api.send_request("/files/#{Addressable::URI.encode_component(params[:path])}", :patch, params, @options)
end

#upload_file(local_file) ⇒ Object



567
568
569
# File 'lib/files.com/models/file.rb', line 567

def upload_file(local_file)
  File.upload_file(local_file. path)
end

#with_renameObject

boolean - Allow file rename instead of overwrite?



811
812
813
# File 'lib/files.com/models/file.rb', line 811

def with_rename
  @attributes[:with_rename]
end

#with_rename=(value) ⇒ Object



815
816
817
# File 'lib/files.com/models/file.rb', line 815

def with_rename=(value)
  @attributes[:with_rename] = value
end

#write(*args) ⇒ Object



571
572
573
574
575
576
577
578
579
# File 'lib/files.com/models/file.rb', line 571

def write(*args)
  @mode ||= 'w'
  if args[0].respond_to?(:read)
    flush if @write_io.size > 0
    @write_io = args[0]
  else
    @write_io.write *args
  end
end

#write_nonblock(*args) ⇒ Object



581
582
583
# File 'lib/files.com/models/file.rb', line 581

def write_nonblock(*args)
  @write_io.write_nonblock *args
end