Class: GroupDocs::Storage::File

Inherits:
Api::Entity show all
Includes:
Api::Helpers::AccessMode, Api::Helpers::Path
Defined in:
lib/groupdocs/storage/file.rb

Constant Summary collapse

DOCUMENT_TYPES =
%w(Undefined Cells Words Slides Pdf Html Image)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Api::Helpers::Path

included

Methods inherited from Api::Entity

#initialize, #inspect, #to_hash

Methods included from Api::Helpers::Accessor

#alias_accessor

Constructor Details

This class inherits a constructor from GroupDocs::Api::Entity

Instance Attribute Details

#accessSymbol

Converts access mode to human-readable format.

Returns:

  • (Symbol)


182
183
184
# File 'lib/groupdocs/storage/file.rb', line 182

def access
  @access
end

#created_onTime

Converts timestamp which is return by API server to Time object.

Returns:

  • (Time)


168
169
170
# File 'lib/groupdocs/storage/file.rb', line 168

def created_on
  @created_on
end

#file_typeSymbol

Returns file type in human-readable format.

Returns:

  • (Symbol)


180
181
182
# File 'lib/groupdocs/storage/file.rb', line 180

def file_type
  @file_type
end

#guidObject



160
161
162
# File 'lib/groupdocs/storage/file.rb', line 160

def guid
  @guid
end

#idObject



158
159
160
# File 'lib/groupdocs/storage/file.rb', line 158

def id
  @id
end

#knownObject



164
165
166
# File 'lib/groupdocs/storage/file.rb', line 164

def known
  @known
end

#local_pathObject



186
187
188
# File 'lib/groupdocs/storage/file.rb', line 186

def local_path
  @local_path
end

#modified_onTime

Converts timestamp which is return by API server to Time object.

Returns:

  • (Time)


170
171
172
# File 'lib/groupdocs/storage/file.rb', line 170

def modified_on
  @modified_on
end

#nameObject



174
175
176
# File 'lib/groupdocs/storage/file.rb', line 174

def name
  @name
end

#pathObject



184
185
186
# File 'lib/groupdocs/storage/file.rb', line 184

def path
  @path
end

#sizeObject



162
163
164
# File 'lib/groupdocs/storage/file.rb', line 162

def size
  @size
end

#thumbnailObject



166
167
168
# File 'lib/groupdocs/storage/file.rb', line 166

def thumbnail
  @thumbnail
end

#typeSymbol

Returns type in human-readable format.

Returns:

  • (Symbol)


178
179
180
# File 'lib/groupdocs/storage/file.rb', line 178

def type
  @type
end

#urlObject



172
173
174
# File 'lib/groupdocs/storage/file.rb', line 172

def url
  @url
end

#versionObject



176
177
178
# File 'lib/groupdocs/storage/file.rb', line 176

def version
  @version
end

Class Method Details

.decompress!(filepath, options = {}, access = {}) ⇒ GroupDocs::Storage::File

Uploads and unzip as file.

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})
  • access (Hash) (defaults to: {})

    Access credentials

  • description (Hash)

    a customizable set of options

  • archiveType (Hash)

    a customizable set of options

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/groupdocs/storage/file.rb', line 138

def self.decompress!(filepath, options = {}, access = {})
  options[:path] ||= ''
  options[:name] ||= Object::File.basename(filepath)
  path = prepare_path("#{options[:path]}/#{options[:name]}")

  api = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :POST
    request[:path] = "/storage/{{client_id}}/decompress/#{path}/"
    request[:request_body] = Object::File.new(filepath, 'rb')
  end
  api.add_params(:archiveType => options[:archiveType]) if options[:archiveType]
  json = api.execute!

  json[:files].map do |file|
    File.new(file)
  end
end

.get_shared_file!(path, name, user_email, file_path) ⇒ Object

Get shared file.

Parameters:

  • path (String)

    Path save new file which receive in response

  • name (String)

    Name new file

  • file_path (String)
  • user_email (String)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/groupdocs/storage/file.rb', line 64

def self.get_shared_file!(path, name, user_email, file_path)
  response = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :GET
    request[:path] = "/storage/shared/#{user_email}/#{file_path}"
  end.execute!

  filepath = "#{path}/#{name}"
  Object::File.open(filepath, 'wb') do |file|
    file.write(response)
  end

  filepath
end

.upload!(filepath, options = {}, access = {}) ⇒ GroupDocs::Storage::File

Uploads file to API server.

Examples:

Upload file to root directory

GroupDocs::Storage::File.upload!('resume.pdf')

Upload file to specific directory

GroupDocs::Storage::File.upload!('resume.pdf', path: 'folder1')

Upload and rename file

GroupDocs::Storage::File.upload!('resume.pdf', name: 'cv.pdf')

Upload file with description

GroupDocs::Storage::File.upload!('resume.pdf', description: 'Resume')

Upload file with callback URL

GroupDocs::Storage::File.upload!('resume.pdf', callbackUrl: 'http://google.com')

Parameters:

  • filepath (String)

    Path to file to be uploaded

  • options (Hash) (defaults to: {})
  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (options):

  • path (String)

    Folder path to upload to

  • name (String)

    Name of file to be renamed

  • :description (String)

    File description

  • :callbackUrl (String)

    will be called after file is uploaded

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/groupdocs/storage/file.rb', line 39

def self.upload!(filepath, options = {}, access = {})
  options[:path] ||= ''
  options[:name] ||= Object::File.basename(filepath)
  path = prepare_path("#{options[:path]}/#{options[:name]}")
  api = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :POST
    request[:path] = "/storage/{{client_id}}/folders/#{path}"
    request[:request_body] = Object::File.new(filepath, 'rb')
  end
  api.add_params(:description => options[:description]) if options[:description]
  json = api.execute!

  Storage::File.new(json)
end

.upload_google!(options = {}, access = {}) ⇒ GroupDocs::Storage::File

Updated in release 2.2.0

Uploads google page as file.

Parameters:

  • options (Hash) (defaults to: {})
  • access (Hash) (defaults to: {})

    Access credentials

  • url (Hash)

    a customizable set of options

  • description (Hash)

    a customizable set of options

  • accessToken (Hash)

    a customizable set of options

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/groupdocs/storage/file.rb', line 113

def self.upload_google!(options = {}, access = {})
  api = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :POST
    request[:path] = "/storage/{{client_id}}/google/files/"
  end

  api.add_params(options)
  json = api.execute!

  Storage::File.new(json)
end

.upload_web!(url, access = {}) ⇒ GroupDocs::Storage::File

Uploads web page as file.

Parameters:

  • url (String)
  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/groupdocs/storage/file.rb', line 88

def self.upload_web!(url, access = {})
  api = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :POST
    request[:path] = '/storage/{{client_id}}/urls'
  end
  api.add_params(:url => url)
  json = api.execute!

  Storage::File.new(json)
end

Instance Method Details

#compress!(access = {}) ⇒ GroupDocs::Storage::File

Compresses file on server to given archive type.

Parameters:

  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



346
347
348
349
350
351
352
353
354
355
356
# File 'lib/groupdocs/storage/file.rb', line 346

def compress!(access = {})
  json = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :POST
    request[:path] = "/storage/{{client_id}}/files/#{id}/archive/zip"
  end.execute!

  # add filename for further file operations
  json[:name] = "#{name}.zip"
  Storage::File.new(json)
end

#copy!(path, options = {}, access = {}) ⇒ GroupDocs::Storage::File

Moves file to given path.

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})
  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (options):

  • name (String)

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/groupdocs/storage/file.rb', line 324

def copy!(path, options = {}, access = {})
  options[:name] ||= name
  path = prepare_path("#{path}/#{options[:name]}")

  json = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :PUT
    request[:headers] = { :'Groupdocs-Copy' => id }
    request[:path] = "/storage/{{client_id}}/files/#{path}"
  end.execute!

  Storage::File.new(json[:dst_file])
end

#delete!(access = {}) ⇒ Object

Deletes file from server.

Parameters:

  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)


365
366
367
368
369
370
371
# File 'lib/groupdocs/storage/file.rb', line 365

def delete!(access = {})
  Api::Request.new do |request|
    request[:access] = access
    request[:method] = :DELETE
    request[:path] = "/storage/{{client_id}}/files/#{guid}"
  end.execute!
end

#download!(path, access = {}) ⇒ String

Downloads file to given path.

Parameters:

  • path (String)

    Directory to download file to

  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:

  • (String)

    Path to downloaded file



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/groupdocs/storage/file.rb', line 260

def download!(path, access = {})
  response = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :DOWNLOAD
    request[:path] = "/storage/{{client_id}}/files/#{guid}"
  end.execute!

  filepath = "#{path}/#{name}"
  Object::File.open(filepath, 'wb') do |file|
    file.write(response)
  end

  filepath
end

#move!(path, options = {}, access = {}) ⇒ GroupDocs::Storage::File

Moves file to given path.

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})
  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (options):

  • name (String)

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/groupdocs/storage/file.rb', line 286

def move!(path, options = {}, access = {})
  options[:name] ||= name
  path = prepare_path("#{path}/#{options[:name]}")

  json = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :PUT
    request[:headers] = { :'Groupdocs-Move' => id }
    request[:path] = "/storage/{{client_id}}/files/#{path}"
  end.execute!

  Storage::File.new(json[:dst_file])
end

#move_to_trash!(access = {}) ⇒ Object

Moves file to trash on server.

Parameters:

  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)


380
381
382
383
384
385
386
# File 'lib/groupdocs/storage/file.rb', line 380

def move_to_trash!(access = {})
  Api::Request.new do |request|
    request[:access] = access
    request[:method] = :PUT
    request[:path] = "/storage/{{client_id}}/trash/#{path}/#{name}"
  end.execute!
end

#rename!(name, access = {}) ⇒ GroupDocs::Storage::File

Renames file to new one.

Parameters:

  • name (String)

    New file name

  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)

Returns:



309
310
311
# File 'lib/groupdocs/storage/file.rb', line 309

def rename!(name, access = {})
  move!(path, { :name => name }, access)
end

#restore_to_trash!(path, access = {}) ⇒ Object

Restore file from trash on server.

Parameters:

  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)


395
396
397
398
399
400
401
# File 'lib/groupdocs/storage/file.rb', line 395

def restore_to_trash!(path, access = {})
  Api::Request.new do |request|
    request[:access] = access
    request[:method] = :DELETE
    request[:path] = "/storage/{{client_id}}/trash/#{path}"
  end.execute!
end

#to_documentGroupDocs::Document

Converts file to GroupDocs::Document.

Returns:



408
409
410
# File 'lib/groupdocs/storage/file.rb', line 408

def to_document
  Document.new(:file => self)
end

#upload_cancel!(path, access = {}) ⇒ Object

Added in release 1.5.8

Cancel file upload

Parameters:

  • path (String)

    File path (name)

  • access (Hash) (defaults to: {})

    Access credentials

Options Hash (access):

  • :client_id (String)
  • :private_key (String)


422
423
424
425
426
427
428
# File 'lib/groupdocs/storage/file.rb', line 422

def upload_cancel!(path, access = {})
  Api::Request.new do |request|
    request[:access] = access
    request[:method] = :GET
    request[:path] = "/storage/{{client_id}}/cancelUpload/#{id}/#{path}"
  end.execute!
end