Class: GroupDocs::Storage::File

Inherits:
Api::Entity show all
Extended by:
Extensions::Lookup
Includes:
Api::Helpers::AccessMode
Defined in:
lib/groupdocs/storage/file.rb

Constant Summary collapse

DOCUMENT_TYPES =
{
  undefined: -1,
  cells:      0,
  words:      1,
  slides:     2,
  pdf:        3,
  html:       4,
  image:      5,
}

Constants included from Api::Helpers::AccessMode

Api::Helpers::AccessMode::MODES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Lookup

find!, find_all!

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)


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

def access
  @access
end

#created_onTime

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

Returns:

  • (Time)


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

def created_on
  @created_on
end

#file_typeObject



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

def file_type
  @file_type
end

#guidObject



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

def guid
  @guid
end

#idObject



72
73
74
# File 'lib/groupdocs/storage/file.rb', line 72

def id
  @id
end

#knownObject



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

def known
  @known
end

#modified_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 modified_on
  @modified_on
end

#nameObject



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

def name
  @name
end

#pathObject



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

def path
  @path
end

#sizeObject



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

def size
  @size
end

#thumbnailObject



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

def thumbnail
  @thumbnail
end

#typeSymbol

Returns document type in human-readable format.

Returns:

  • (Symbol)


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

def type
  @type
end

#urlObject



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

def url
  @url
end

#versionObject



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

def version
  @version
end

Class Method Details

.all!(path = '/', access = {}) ⇒ Array<GroupDocs::Storage::File>

Returns an array of all files on server starting with given path.

Parameters:

  • path (String) (defaults to: '/')

    Starting path to look for files

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

    Access credentials

Options Hash (access):

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

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/groupdocs/storage/file.rb', line 57

def self.all!(path = '/', access = {})
  files = Array.new
  folder = GroupDocs::Storage::Folder.new(path: path)
  folder.list!({}, access).each do |entity|
    if entity.is_a?(GroupDocs::Storage::Folder)
      files += all!("#{path}/#{entity.name}", access)
    else
      files << entity
    end
  end

  files
end

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

Uploads file to API server.

Examples:

GroupDocs::Storage::File.upload!('resume.pdf', '/folder/cv.pdf', description: 'My resume')

Parameters:

  • filepath (String)

    Path to file to be uploaded

  • upload_path (String) (defaults to: '/')

    Full path to directory to upload file to starting with “/”. You can also add filename and then uploaded file will use it.

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

    Access credentials

Options Hash (access):

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

Returns:

Raises:

  • (ArgumentError)

    If path does not start with /



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

def self.upload!(filepath, upload_path = '/', access = {})
  Api::Helpers::Path.verify_starts_with_root(upload_path)
  Api::Helpers::Path.append_file_name(upload_path, filepath)

  json = Api::Request.new do |request|
    request[:access] = access
    request[:method] = :POST
    request[:path] = "/storage/{{client_id}}/folders#{upload_path}"
    request[:request_body] = Object::File.new(filepath, 'rb')
  end.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:



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/groupdocs/storage/file.rb', line 247

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!

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

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

Moves file to given path.

Parameters:

  • path (String)

    Full path to directory to copy file to starting with “/”. You can also add filename and then copied file will use it.

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

    Access credentials

Options Hash (access):

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

Returns:



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/groupdocs/storage/file.rb', line 225

def copy!(path, access = {})
  Api::Helpers::Path.verify_starts_with_root(path)
  Api::Helpers::Path.append_file_name(path, 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)


266
267
268
269
270
271
272
# File 'lib/groupdocs/storage/file.rb', line 266

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/groupdocs/storage/file.rb', line 163

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, access = {}) ⇒ GroupDocs::Storage::File

Moves file to given path.

Parameters:

  • path (String)

    Full path to directory to move file to starting with “/”. You can also add filename and then moved file will use it.

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

    Access credentials

Options Hash (access):

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

Returns:



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/groupdocs/storage/file.rb', line 188

def move!(path, access = {})
  Api::Helpers::Path.verify_starts_with_root(path)
  Api::Helpers::Path.append_file_name(path, 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

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



211
212
213
# File 'lib/groupdocs/storage/file.rb', line 211

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

#to_documentGroupDocs::Document

Converts file to GroupDocs::Document.

Returns:



279
280
281
# File 'lib/groupdocs/storage/file.rb', line 279

def to_document
  Document.new(file: self)
end