Class: SafeNet::NFS

Inherits:
Object
  • Object
show all
Defined in:
lib/safenet.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_obj) ⇒ NFS

Returns a new instance of NFS.



191
192
193
# File 'lib/safenet.rb', line 191

def initialize(client_obj)
  @client = client_obj
end

Instance Method Details

#create_directory(dir_path, options = {}) ⇒ Object

Create a public or private directory either in the application’s root

directory or in SAFE Drive.

Only authorised requests can create a directory.

Usage: my_client.nfs.create_directory(“/photos”) Adv.Usage: my_client.nfs.create_directory(“/photos”, meta: “some meta”, root_path: ‘drive’, is_private: true) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfs-create-directory



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/safenet.rb', line 207

def create_directory(dir_path, options = {})
  # Default values
  options[:root_path]      = 'app' if ! options.has_key?(:root_path)
  options[:is_private]     = true  if ! options.has_key?(:is_private)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{CGI.escape(dir_path)}"

  # Payload
  payload = {
    isPrivate: options[:is_private],
  }

  # Optional
  payload["metadata"] = Base64.strict_encode64(options[:meta]) if options.has_key?(:meta)

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'application/json'
  })
  req.body = payload.to_json
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#create_file(file_path, contents, options = {}) ⇒ Object

Create a file. Only authorised requests can invoke the API.

Usage: my_client.nfs.create_file(“/docs/hello.txt”, “Hello World!”) Adv.Usage: my_client.nfs.create_file(“/docs/hello.txt”, meta: “some meta”, root_path: “app”, content_type: “text/plain”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfsfile



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/safenet.rb', line 356

def create_file(file_path, contents, options = {})
  # Default values
  options[:root_path] = 'app' if ! options.has_key?(:root_path)
  contents ||= ""

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{CGI.escape(file_path)}"

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  headers = {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  }
  headers["Metadata"]       = Base64.strict_encode64(options[:meta]) if options.has_key?(:meta)
  headers["Content-Type"]   = options[:content_type] || 'text/plain'
  headers["Content-Length"] = contents.size.to_s
  req = Net::HTTP::Post.new(uri.path, headers)
  req.body = contents
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#create_private_directory(dir_path, options = {}) ⇒ Object

Alias



242
243
244
245
# File 'lib/safenet.rb', line 242

def create_private_directory(dir_path, options = {})
  options[:is_private] = true
  self.create_directory(dir_path, options)
end

#create_public_directory(dir_path, options = {}) ⇒ Object

Alias



236
237
238
239
# File 'lib/safenet.rb', line 236

def create_public_directory(dir_path, options = {})
  options[:is_private] = false
  self.create_directory(dir_path, options)
end

#delete_directory(dir_path, options = {}) ⇒ Object

Delete a directory. Only authorised requests can invoke this API.

Rename: my_client.nfs.delete_directory(“/photos”) Change meta: my_client.nfs.delete_directory(“/photos”, root_path: “app”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfs-delete-directory



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/safenet.rb', line 327

def delete_directory(dir_path, options = {})
  # Default values
  options[:root_path] = 'app' if ! options.has_key?(:root_path)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{CGI.escape(dir_path)}"

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#delete_file(file_path, options = {}) ⇒ Object

Delete a file. Only authorised requests can invoke the API.

Usage: my_client.nfs.delete_file(“/docs/hello.txt”) Adv.Usage: my_client.nfs.delete_file(“/docs/hello.txt”, root_path: “app”) Fail: “description”=>“FfiError::InvalidPath” Success: true

Reference: maidsafe.readme.io/docs/nfs-delete-file



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/safenet.rb', line 533

def delete_file(file_path, options = {})
  # Default values
  options[:root_path] = 'app' if ! options.has_key?(:root_path)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{CGI.escape(file_path)}"

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#get_directory(dir_path, options = {}) ⇒ Object

Fetch a directory. Only authorised requests can invoke this API.

Usage: my_client.nfs.get_directory(“/photos”, root_path: ‘drive’) Fail: “description”=>“FfiError::PathNotFound” Success: {“name”=> “my_dir”, …, …}

Reference: maidsafe.readme.io/docs/nfs-get-directory



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/safenet.rb', line 257

def get_directory(dir_path, options = {})
  # Default values
  options[:root_path] = 'app' if ! options.has_key?(:root_path)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{CGI.escape(dir_path)}"

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  })
  res = http.request(req)
  JSON.parse(res.body)
end

#get_file(file_path, options = {}) ⇒ Object

Read a file. The file can be streamed in chunks and also fetched as partial content

based on the range header specified in the request.

Only authorised requests can invoke the API.

Usage: my_client.nfs.get_file(“/docs/hello.txt”) Adv.Usage: my_client.nfs.get_file(“/docs/hello.txt”, range: “bytes 0-1000”, root_path: “app”) Fail: “description”=>“FfiError::InvalidPath” Success: “content-range”=>“bytes 0-4/4”, “accept-ranges”=>“bytes”, “content-length”=>“4”, “created-on”=>“2016-08-14T12:51:18.924Z”, “last-modified”=>“2016-08-14T12:51:18.935Z”, “content-type”=>“text/plain”, “date”=>“Sun, 14 Aug 2016 13:30:07 GMT”, “connection”=>“close”, “body”=>“Test”}

Reference: maidsafe.readme.io/docs/nfs-get-file



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/safenet.rb', line 426

def get_file(file_path, options = {})
  # Default values
  options[:offset]    = 0     if ! options.has_key?(:offset)
  options[:root_path] = 'app' if ! options.has_key?(:root_path)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{CGI.escape(file_path)}?"

  # Query params
  query = []
  query << "offset=#{options[:offset]}"
  query << "length=#{options[:length]}" if options.has_key?(:length) # length is optional
  url = "#{url}#{query.join('&')}"

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  headers = {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  }
  headers["Range"] = options[:range] if options.has_key?(:range)
  req = Net::HTTP::Get.new(uri.path, headers)
  res = http.request(req)
  res_headers = {}
  res.response.each_header {|k,v| res_headers[k] = v}
  res.code == "200" ? {"headers" => res_headers, "body" => res.body} : JSON.parse(res.body)
end

#get_file_meta(file_path, options = {}) ⇒ Object

Fetch the metadata of a file. Only authorised requests can invoke the API.

Usage: my_client.nfs.get_file_meta(“/docs/hello.txt”) Adv.Usage: my_client.nfs.get_file_meta(“/docs/hello.txt”, root_path: “app”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success:

Reference: maidsafe.readme.io/docs/nfs-get-file-metadata



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/safenet.rb', line 391

def get_file_meta(file_path, options = {})
  # Default values
  options[:root_path] = 'app' if ! options.has_key?(:root_path)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/#{options[:root_path]}/#{CGI.escape(file_path)}"

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  headers = {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
  }
  req = Net::HTTP::Head.new(uri.path, headers)
  res = http.request(req)
  res_headers = {}
  res.response.each_header {|k,v| res_headers[k] = v}
  res_headers["metadata"] = Base64.strict_decode64(res_headers["metadata"]) if res_headers.has_key?("metadata")
  res.code == "200" ? {"headers" => res_headers, "body" => res.body} : JSON.parse(res.body)
end

#move_file(src_root_path, src_path, dst_root_path, dst_path, action = 'move') ⇒ Object

Move or copy a file



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/safenet.rb', line 498

def move_file(src_root_path, src_path, dst_root_path, dst_path, action = 'move')
  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/movefile"

  # Payload
  payload = {}
  payload["srcRootPath"] = src_root_path # 'app' or 'drive'
  payload["srcPath"] = src_path
  payload["destRootPath"] = dst_root_path # 'app' or 'drive'
  payload["destPath"] = dst_path
  payload["action"] = action

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'application/json'
  })
  req.body = payload.to_json
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#rename_directory(dir_path, new_name, options = {}) ⇒ Object

Alias



311
312
313
314
# File 'lib/safenet.rb', line 311

def rename_directory(dir_path, new_name, options = {})
  options[:name] = new_name
  self.update_directory(dir_path, options)
end

#rename_file(file_path, new_name, options = {}) ⇒ Object

Alias



491
492
493
494
# File 'lib/safenet.rb', line 491

def rename_file(file_path, new_name, options = {})
  options[:name] = new_name
  self.update_file_meta(file_path)
end

#update_directory(dir_path, options = {}) ⇒ Object

Rename a directory and (optionally) update its metadata. Only authorised requests can invoke this API.

Rename: my_client.nfs.update_directory(“/photos”, name: “pics”) Change meta: my_client.nfs.update_directory(“/photos”, meta: “new meta”) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfs-update-directory



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/safenet.rb', line 286

def update_directory(dir_path, options = {})
  # Default values
  options[:root_path] = 'app' if ! options.has_key?(:root_path)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/directory/#{options[:root_path]}/#{CGI.escape(dir_path)}"

  # Optional payload
  payload = {}
  payload["name"] = CGI.escape(options[:name]) if options.has_key?(:name)
  payload["metadata"] = Base64.strict_encode64(options[:meta]) if options.has_key?(:meta)

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Put.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'application/json'
  })
  req.body = payload.to_json
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#update_file_meta(file_path, options = {}) ⇒ Object

Rename a file and (optionally) update its metadata. Only authorised requests can invoke the API.

Usage: my_client.nfs.update_file_metadata(“/docs/hello.txt”) Adv.Usage: my_client.nfs.get_file(“/docs/hello.txt”, range: “bytes 0-1000”, root_path: “app”) Fail: “description”=>“FfiError::InvalidPath” Success: “content-range”=>“bytes 0-4/4”, “accept-ranges”=>“bytes”, “content-length”=>“4”, “created-on”=>“2016-08-14T12:51:18.924Z”, “last-modified”=>“2016-08-14T12:51:18.935Z”, “content-type”=>“text/plain”, “date”=>“Sun, 14 Aug 2016 13:30:07 GMT”, “connection”=>“close”, “body”=>“Test”}

Reference: maidsafe.readme.io/docs/nfs-update-file-metadata



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/safenet.rb', line 466

def update_file_meta(file_path, options = {})
  # Default values
  options[:root_path] = 'app' if ! options.has_key?(:root_path)

  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/nfs/file/metadata/#{options[:root_path]}/#{CGI.escape(file_path)}"

  # Optional payload
  payload = {}
  payload["name"] = CGI.escape(options[:name]) if options.has_key?(:name)
  payload["metadata"] = Base64.strict_encode64(options[:meta]) if options.has_key?(:meta)

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Put.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'application/json'
  })
  req.body = payload.to_json
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end