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

Constructor Details

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

Instance Attribute Details

#accessSymbol

Converts access mode to human-readable format.

Returns:

  • (Symbol)


98
99
100
# File 'lib/groupdocs/storage/file.rb', line 98

def access
  @access
end

#created_onTime

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

Returns:

  • (Time)


84
85
86
# File 'lib/groupdocs/storage/file.rb', line 84

def created_on
  @created_on
end

#file_typeSymbol

Returns file type in human-readable format.

Returns:

  • (Symbol)


96
97
98
# File 'lib/groupdocs/storage/file.rb', line 96

def file_type
  @file_type
end

#guidObject



76
77
78
# File 'lib/groupdocs/storage/file.rb', line 76

def guid
  @guid
end

#idObject



74
75
76
# File 'lib/groupdocs/storage/file.rb', line 74

def id
  @id
end

#knownObject



80
81
82
# File 'lib/groupdocs/storage/file.rb', line 80

def known
  @known
end

#modified_onTime

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

Returns:

  • (Time)


86
87
88
# File 'lib/groupdocs/storage/file.rb', line 86

def modified_on
  @modified_on
end

#nameObject



90
91
92
# File 'lib/groupdocs/storage/file.rb', line 90

def name
  @name
end

#pathObject



100
101
102
# File 'lib/groupdocs/storage/file.rb', line 100

def path
  @path
end

#sizeObject



78
79
80
# File 'lib/groupdocs/storage/file.rb', line 78

def size
  @size
end

#thumbnailObject



82
83
84
# File 'lib/groupdocs/storage/file.rb', line 82

def thumbnail
  @thumbnail
end

#typeSymbol

Returns type in human-readable format.

Returns:

  • (Symbol)


94
95
96
# File 'lib/groupdocs/storage/file.rb', line 94

def type
  @type
end

#urlObject



88
89
90
# File 'lib/groupdocs/storage/file.rb', line 88

def url
  @url
end

#versionObject



92
93
94
# File 'lib/groupdocs/storage/file.rb', line 92

def version
  @version
end

Class Method Details

.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')

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

Options Hash (access):

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

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/groupdocs/storage/file.rb', line 35

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_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:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/groupdocs/storage/file.rb', line 61

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:



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

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:



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/groupdocs/storage/file.rb', line 238

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)


279
280
281
282
283
284
285
# File 'lib/groupdocs/storage/file.rb', line 279

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/groupdocs/storage/file.rb', line 174

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

  filepath = "#{path}/#{name}"
  Object::File.open(filepath, 'w') 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:



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/groupdocs/storage/file.rb', line 200

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)


294
295
296
297
298
299
300
# File 'lib/groupdocs/storage/file.rb', line 294

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:



223
224
225
# File 'lib/groupdocs/storage/file.rb', line 223

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

#to_documentGroupDocs::Document

Converts file to GroupDocs::Document.

Returns:



307
308
309
# File 'lib/groupdocs/storage/file.rb', line 307

def to_document
  Document.new(file: self)
end