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.



195
196
197
198
199
200
201
202
203
# File 'lib/files.com/models/file.rb', line 195

def initialize(*args)
  @attributes = (args[0].is_a?(Hash) && args[0]) || {}
  @options = (args[1].is_a?(Hash) && args[1])
  @options ||= (args[2].is_a?(Hash) && args[2]) || {}
  @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



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

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



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

def pos
  @pos ||= 0
end

#syncObject



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

def sync
  @sync ||= false
end

Class Method Details

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

Begin file upload

Parameters:

mkdir_parents - boolean - Create parent directories if they do not exist?
part - int64 - Part if uploading a part.
parts - int64 - How many parts to fetch?
ref - string -
restart - int64 - File byte offset to restart from.
size - int64 - Total bytes of file being uploaded (include bytes being retained if appending/restarting).
with_rename - boolean - Allow file rename instead of overwrite?


1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/files.com/models/file.rb', line 1085

def self.begin_upload(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: 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: 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 MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, options = Api.send_request("/file_actions/begin_upload/#{params[:path]}", :post, params, options)
  response.data.map do |entity_data|
    FileUploadPart.new(entity_data, options)
  end
end

.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(path, params = {}, options = {}) ⇒ Object

Copy file/folder

Parameters:

destination (required) - string - Copy destination path.
structure - boolean - Copy structure only?


1047
1048
1049
# File 'lib/files.com/models/file.rb', line 1047

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

Parameters:

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


972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/files.com/models/file.rb', line 972

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/#{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.


1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/files.com/models/file.rb', line 1008

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/#{params[:path]}", :delete, params, options)
  response.data
end

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



1018
1019
1020
# File 'lib/files.com/models/file.rb', line 1018

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.
preview_size - string - Request a preview size.  Can be `small` (default), `large`, `xlarge`, or `pdf`.
with_previews - boolean - Include file preview information?
with_priority_color - boolean - Include file priority color information?


945
946
947
948
949
950
951
952
953
954
955
# File 'lib/files.com/models/file.rb', line 945

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: preview_size must be an String") if params.dig(:preview_size) and !params.dig(:preview_size).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  response, options = Api.send_request("/files/#{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 Error => 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

Parameters:

path (required) - string - Path to operate on.
preview_size - string - Request a preview size.  Can be `small` (default), `large`, `xlarge`, or `pdf`.
with_previews - boolean - Include file preview information?
with_priority_color - boolean - Include file priority color information?


1027
1028
1029
1030
# File 'lib/files.com/models/file.rb', line 1027

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

.from_path(path) ⇒ Object



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

def self.from_path(path)
  File.find(path)
end

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



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

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

.identical?(path1, path2) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.lstat(path) ⇒ Object



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

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

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

Move file/folder

Parameters:

destination (required) - string - Move destination path.


1063
1064
1065
# File 'lib/files.com/models/file.rb', line 1063

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

.mtime(path) ⇒ Object



93
94
95
# File 'lib/files.com/models/file.rb', line 93

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

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



97
98
99
100
101
102
103
104
# File 'lib/files.com/models/file.rb', line 97

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

.owned?(_path) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



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

def self.owned?(_path)
  raise NotImplementedError
end

.pipe(*_args) ⇒ Object



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

def self.pipe(*_args)
  raise NotImplementedError
end

.popen(*_args) ⇒ Object



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

def self.popen(*_args)
  raise NotImplementedError
end

.read(name, *args) ⇒ Object



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

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

.readable?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.readlines(name, *args) ⇒ Object



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

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

.rename(old_path, new_path) ⇒ Object



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

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

.select(*_args) ⇒ Object



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

def self.select(*_args)
  raise NotImplementedError
end

.stat(path) ⇒ Object



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

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

.sysopen(*_args) ⇒ Object



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

def self.sysopen(*_args)
  raise NotImplementedError
end

.try_convert(*_args) ⇒ Object



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

def self.try_convert(*_args)
  raise NotImplementedError
end


150
151
152
# File 'lib/files.com/models/file.rb', line 150

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.


994
995
996
997
998
999
1000
1001
1002
1003
1004
# File 'lib/files.com/models/file.rb', line 994

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/#{params[:path]}", :patch, params, options)
  File.new(response.data, options)
end

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/files.com/models/file.rb', line 154

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

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/files.com/models/file.rb', line 169

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



187
188
189
# File 'lib/files.com/models/file.rb', line 187

def self.write(*_args)
  raise NotImplementedError
end

.zero?(path) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/files.com/models/file.rb', line 191

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



745
746
747
# File 'lib/files.com/models/file.rb', line 745

def action
  @attributes[:action]
end

#action=(value) ⇒ Object



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

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

#advise(*_args) ⇒ Object



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

def advise(*_args); end

#atimeObject



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

def atime
  mtime
end

#autoclose=(*_args) ⇒ Object



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

def autoclose=(*_args); end

#autoclose?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


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

def autoclose?(*_args); end

#begin_upload(params = {}) ⇒ Object

Begin file upload

Parameters:

mkdir_parents - boolean - Create parent directories if they do not exist?
part - int64 - Part if uploading a part.
parts - int64 - How many parts to fetch?
ref - string -
restart - int64 - File byte offset to restart from.
size - int64 - Total bytes of file being uploaded (include bytes being retained if appending/restarting).
with_rename - boolean - Allow file rename instead of overwrite?


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

def begin_upload(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: 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: 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 MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

  Api.send_request("/file_actions/begin_upload/#{@attributes[:path]}", :post, params, @options)
end

#binmodeObject



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

def binmode
  binmode?
end

#binmode?Boolean

Returns:

  • (Boolean)


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

def binmode?
  true
end

#birthtimeObject



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

def birthtime
  raise NotImplementedError
end

#bytesObject



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

def bytes
  read_io.bytes
end

#charsObject



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

def chars
  read_io.chars
end

#chmod(*_args) ⇒ Object



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

def chmod(*_args)
  raise NotImplementedError
end

#chown(*_args) ⇒ Object



239
240
241
# File 'lib/files.com/models/file.rb', line 239

def chown(*_args)
  raise NotImplementedError
end

#clientObject



243
244
245
# File 'lib/files.com/models/file.rb', line 243

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

#closeObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/files.com/models/file.rb', line 247

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



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

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

#close_on_exec?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#close_read(*args) ⇒ Object



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

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

#close_write(*args) ⇒ Object



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

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

#closed?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#codepoints(*args, &block) ⇒ Object



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

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

#copy(params = {}) ⇒ Object

Copy file/folder

Parameters:

destination (required) - string - Copy destination path.
structure - boolean - Copy structure only?


880
881
882
# File 'lib/files.com/models/file.rb', line 880

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.



655
656
657
# File 'lib/files.com/models/file.rb', line 655

def crc32
  @attributes[:crc32]
end

#crc32=(value) ⇒ Object



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

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

#created_atObject

date-time - File created date/time



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

def created_at
  @attributes[:created_at]
end

#ctime(*_args) ⇒ Object



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

def ctime(*_args)
  mtime
end

#delete(params = {}) ⇒ Object

Parameters:

recursive - boolean - If true, will recursively delete folers.  Otherwise, will error on non-empty folders.


861
862
863
864
865
866
867
868
869
# File 'lib/files.com/models/file.rb', line 861

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/#{@attributes[:path]}", :delete, params, @options)
end

#destroy(params = {}) ⇒ Object



871
872
873
# File 'lib/files.com/models/file.rb', line 871

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

#display_nameObject

string - File/Folder display name



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

def display_name
  @attributes[:display_name]
end

#display_name=(value) ⇒ Object



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

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.
preview_size - string - Request a preview size.  Can be `small` (default), `large`, `xlarge`, or `pdf`.
with_previews - boolean - Include file preview information?
with_priority_color - boolean - Include file priority color information?


832
833
834
835
836
837
838
839
840
841
842
# File 'lib/files.com/models/file.rb', line 832

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: preview_size must be an String") if params.dig(:preview_size) and !params.dig(:preview_size).is_a?(String)
  raise MissingParameterError.new("Parameter missing: path") unless params.dig(:path)

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

#download_content(io, range: []) ⇒ Object



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

def download_content(io, range: [])
  Files::ApiClient.download_client.stream_download(download_uri_with_load, io, range)
end

#download_file(output_file) ⇒ Object



306
307
308
309
310
# File 'lib/files.com/models/file.rb', line 306

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.



709
710
711
# File 'lib/files.com/models/file.rb', line 709

def download_uri
  @attributes[:download_uri]
end

#download_uri=(value) ⇒ Object



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

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

#download_uri_with_loadObject



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

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



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

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

#each_byte(*args, &block) ⇒ Object



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

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

#each_char(*args, &block) ⇒ Object



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

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

#each_codepoint(*args, &block) ⇒ Object



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

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

#each_line(*args, &block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  size == 0
end

#eofObject



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

def eof
  eof?
end

#eof?Boolean

Returns:

  • (Boolean)


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

def eof?
  @write_io.eof?
end

#external_encoding(*args) ⇒ Object



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

def external_encoding(*args)
  internal_encoding *args
end

#fcntl(*_args) ⇒ Object



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

def fcntl(*_args)
  raise NotImplementedError
end

#fdatasync(*_args) ⇒ Object



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

def fdatasync(*_args)
  flush
end

#fileno(*_args) ⇒ Object



360
361
362
# File 'lib/files.com/models/file.rb', line 360

def fileno(*_args)
  id
end

#flock(*_args) ⇒ Object



364
365
366
# File 'lib/files.com/models/file.rb', line 364

def flock(*_args)
  raise NotImplementedError
end

#flush(*_args) ⇒ Object



368
369
370
371
372
373
374
375
376
377
# File 'lib/files.com/models/file.rb', line 368

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

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

#fsync(*args) ⇒ Object



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

def fsync(*args)
  flush *args
end

#getbyte(*args) ⇒ Object



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

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

#getc(*args) ⇒ Object



387
388
389
# File 'lib/files.com/models/file.rb', line 387

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

#gets(*args) ⇒ Object



391
392
393
# File 'lib/files.com/models/file.rb', line 391

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

#internal_encoding(*_args) ⇒ Object



407
408
409
# File 'lib/files.com/models/file.rb', line 407

def internal_encoding(*_args)
  "".encoding
end

#ioctl(*_args) ⇒ Object



411
412
413
# File 'lib/files.com/models/file.rb', line 411

def ioctl(*_args)
  raise NotImplementedError
end

#isatty(*_args) ⇒ Object



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

def isatty(*_args)
  false
end

#lengthObject

int64 - Length of file.



754
755
756
# File 'lib/files.com/models/file.rb', line 754

def length
  @attributes[:length]
end

#length=(value) ⇒ Object



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

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

#lines(*args, &block) ⇒ Object



425
426
427
# File 'lib/files.com/models/file.rb', line 425

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

#lstat(*_args) ⇒ Object



429
430
431
# File 'lib/files.com/models/file.rb', line 429

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.



664
665
666
# File 'lib/files.com/models/file.rb', line 664

def md5
  @attributes[:md5]
end

#md5=(value) ⇒ Object



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

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.



673
674
675
# File 'lib/files.com/models/file.rb', line 673

def mime_type
  @attributes[:mime_type]
end

#mime_type=(value) ⇒ Object



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

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

#mkdir_parentsObject

boolean - Create parent directories if they do not exist?



763
764
765
# File 'lib/files.com/models/file.rb', line 763

def mkdir_parents
  @attributes[:mkdir_parents]
end

#mkdir_parents=(value) ⇒ Object



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

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

#move(params = {}) ⇒ Object

Move file/folder

Parameters:

destination (required) - string - Move destination path.


896
897
898
# File 'lib/files.com/models/file.rb', line 896

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.



637
638
639
# File 'lib/files.com/models/file.rb', line 637

def mtime
  @attributes[:mtime]
end

#mtime=(value) ⇒ Object



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

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

#mv(destination) ⇒ Object



437
438
439
# File 'lib/files.com/models/file.rb', line 437

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

#partObject

int64 - Part if uploading a part.



772
773
774
# File 'lib/files.com/models/file.rb', line 772

def part
  @attributes[:part]
end

#part=(value) ⇒ Object



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

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

#partsObject

int64 - How many parts to fetch?



781
782
783
# File 'lib/files.com/models/file.rb', line 781

def parts
  @attributes[:parts]
end

#parts=(value) ⇒ Object



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

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.



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

def path
  @attributes[:path]
end

#path=(value) ⇒ Object



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

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

#permissionsObject

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



691
692
693
# File 'lib/files.com/models/file.rb', line 691

def permissions
  @attributes[:permissions]
end

#permissions=(value) ⇒ Object



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

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

#pid(*_args) ⇒ Object



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

def pid(*_args)
  Process.pid
end

#pread(*args) ⇒ Object



451
452
453
# File 'lib/files.com/models/file.rb', line 451

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

#previewObject

Preview - File preview



736
737
738
# File 'lib/files.com/models/file.rb', line 736

def preview
  @attributes[:preview]
end

#preview=(value) ⇒ Object



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

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

#preview_idObject

int64 - File preview ID



727
728
729
# File 'lib/files.com/models/file.rb', line 727

def preview_id
  @attributes[:preview_id]
end

#preview_id=(value) ⇒ Object



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

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


455
456
457
# File 'lib/files.com/models/file.rb', line 455

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

#printf(*args) ⇒ Object



459
460
461
# File 'lib/files.com/models/file.rb', line 459

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

#priority_colorObject

string - Bookmark/priority color of file/folder



718
719
720
# File 'lib/files.com/models/file.rb', line 718

def priority_color
  @attributes[:priority_color]
end

#priority_color=(value) ⇒ Object



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

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.



646
647
648
# File 'lib/files.com/models/file.rb', line 646

def provided_mtime
  @attributes[:provided_mtime]
end

#provided_mtime=(value) ⇒ Object



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

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

#putc(*args) ⇒ Object



463
464
465
# File 'lib/files.com/models/file.rb', line 463

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

#puts(*args) ⇒ Object



467
468
469
# File 'lib/files.com/models/file.rb', line 467

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

#pwrite(*args) ⇒ Object



471
472
473
# File 'lib/files.com/models/file.rb', line 471

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

#read(*args) ⇒ Object



475
476
477
# File 'lib/files.com/models/file.rb', line 475

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

#read_io(**options) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
# File 'lib/files.com/models/file.rb', line 395

def read_io(**options)
  @read_io ||= begin
                 r, w = SizableIO.pipe
                 Thread.new do
                   download_content(w, **options)
                 ensure
                   w.close
                 end
                 r.wait!(5)
               end
end

#read_nonblock(*args) ⇒ Object



479
480
481
# File 'lib/files.com/models/file.rb', line 479

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

#readbyte(*args) ⇒ Object



483
484
485
# File 'lib/files.com/models/file.rb', line 483

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

#readchar(*args) ⇒ Object



487
488
489
# File 'lib/files.com/models/file.rb', line 487

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

#readline(*args) ⇒ Object



491
492
493
# File 'lib/files.com/models/file.rb', line 491

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

#readlines(*args) ⇒ Object



495
496
497
# File 'lib/files.com/models/file.rb', line 495

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

#readpartial(*args) ⇒ Object



499
500
501
# File 'lib/files.com/models/file.rb', line 499

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

#refObject

string -



790
791
792
# File 'lib/files.com/models/file.rb', line 790

def ref
  @attributes[:ref]
end

#ref=(value) ⇒ Object



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

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

#regionObject

string - Region location



682
683
684
# File 'lib/files.com/models/file.rb', line 682

def region
  @attributes[:region]
end

#region=(value) ⇒ Object



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

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

#rename(destination) ⇒ Object



503
504
505
# File 'lib/files.com/models/file.rb', line 503

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

#reopen(*_args) ⇒ Object



507
508
509
# File 'lib/files.com/models/file.rb', line 507

def reopen(*_args)
  raise NotImplementedError
end

#restartObject

int64 - File byte offset to restart from.



799
800
801
# File 'lib/files.com/models/file.rb', line 799

def restart
  @attributes[:restart]
end

#restart=(value) ⇒ Object



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

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

#rewindObject



511
512
513
# File 'lib/files.com/models/file.rb', line 511

def rewind
  @pos = 0
end

#saveObject



933
934
935
936
# File 'lib/files.com/models/file.rb', line 933

def save
  new_obj = File.create(path, @attributes, @options)
  @attributes = new_obj.attributes
end

#seek(pos) ⇒ Object



515
516
517
# File 'lib/files.com/models/file.rb', line 515

def seek(pos)
  @pos = pos
end

#set_encoding(*_args) ⇒ Object

rubocop:disable Naming/AccessorMethodName



519
520
521
# File 'lib/files.com/models/file.rb', line 519

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

#sizeObject

int64 - File/Folder size



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

def size
  @attributes[:size]
end

#size=(value) ⇒ Object



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

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

#stat(*_args) ⇒ Object



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

def stat(*_args)
  stats
end

#structureObject

string - If copying folder, copy just the structure?



808
809
810
# File 'lib/files.com/models/file.rb', line 808

def structure
  @attributes[:structure]
end

#structure=(value) ⇒ Object



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

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

#subfolders_locked=(value) ⇒ Object



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

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

#subfolders_locked?Boolean

boolean - Are subfolders locked and unable to be modified?

Returns:

  • (Boolean)


700
701
702
# File 'lib/files.com/models/file.rb', line 700

def subfolders_locked?
  @attributes[:subfolders_locked?]
end

#sysread(*args) ⇒ Object



533
534
535
# File 'lib/files.com/models/file.rb', line 533

def sysread(*args)
  read *args
end

#sysseek(*args) ⇒ Object



537
538
539
# File 'lib/files.com/models/file.rb', line 537

def sysseek(*args)
  seek *args
end

#syswrite(*_args) ⇒ Object



541
542
543
# File 'lib/files.com/models/file.rb', line 541

def syswrite(*_args)
  raise NotImplementedError
end

#tellObject



545
546
547
# File 'lib/files.com/models/file.rb', line 545

def tell
  pos
end

#to_i(*_args) ⇒ Object



549
550
551
# File 'lib/files.com/models/file.rb', line 549

def to_i(*_args)
  fileno
end

#to_io(*_args) ⇒ Object



553
554
555
# File 'lib/files.com/models/file.rb', line 553

def to_io(*_args)
  @write_io
end

#to_path(*_args) ⇒ Object



557
558
559
# File 'lib/files.com/models/file.rb', line 557

def to_path(*_args)
  path
end

#truncate(*_args) ⇒ Object



561
562
563
# File 'lib/files.com/models/file.rb', line 561

def truncate(*_args)
  raise NotImplementedError
end

#tty?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


565
566
567
# File 'lib/files.com/models/file.rb', line 565

def tty?(*_args)
  false
end

#typeObject

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



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

def type
  @attributes[:type]
end

#type=(value) ⇒ Object



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

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

#ungetbyte(*_args) ⇒ Object



569
570
571
# File 'lib/files.com/models/file.rb', line 569

def ungetbyte(*_args)
  raise NotImplementedError
end

#ungetc(*_args) ⇒ Object



573
574
575
# File 'lib/files.com/models/file.rb', line 573

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.


847
848
849
850
851
852
853
854
855
856
857
# File 'lib/files.com/models/file.rb', line 847

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/#{@attributes[:path]}", :patch, params, @options)
end

#upload_file(local_file) ⇒ Object



577
578
579
# File 'lib/files.com/models/file.rb', line 577

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

#with_renameObject

boolean - Allow file rename instead of overwrite?



817
818
819
# File 'lib/files.com/models/file.rb', line 817

def with_rename
  @attributes[:with_rename]
end

#with_rename=(value) ⇒ Object



821
822
823
# File 'lib/files.com/models/file.rb', line 821

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

#write(*args) ⇒ Object



581
582
583
584
585
586
587
588
589
# File 'lib/files.com/models/file.rb', line 581

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

#write_nonblock(*args) ⇒ Object



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

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