Class: CloudFS::Folder

Inherits:
Container show all
Defined in:
lib/cloudfs/folder.rb

Overview

Represents a folder in the user's filesystem that can contain files and other folders.

Examples:

folder = session.filesystem.root.create_folder(name_of_folder)
folder.name = "newname"
folder.save
file = folder.upload(local_file_path)
folder.list		#=> Array<File, Folder>

Instance Attribute Summary

Attributes inherited from Item

#blocklist_id, #blocklist_key, #date_content_last_modified, #date_created, #date_meta_last_modified, #id, #is_mirrored, #name, #parent_id, #type, #url, #version

Instance Method Summary collapse

Methods inherited from Container

#initialize, #list

Methods inherited from Item

#application_data, #application_data=, #change_attributes, #copy, #delete, #exists?, #in_share?, #in_trash?, #initialize, #move, #old_version?, #path, #refresh, #restore, #save, #versions

Constructor Details

This class inherits a constructor from CloudFS::Container

Instance Method Details

#create_folder(name, exists: 'FAIL') ⇒ Folder

Create folder under this container

Parameters:

  • name (String)

    name of folder to be created

  • exists (String) (defaults to: 'FAIL')

    ('FAIL', 'OVERWRITE', 'RENAME') action to take if the item already exists

Returns:

Raises:



75
76
77
78
79
80
81
82
# File 'lib/cloudfs/folder.rb', line 75

def create_folder(name, exists: 'FAIL')
  FileSystemCommon.validate_item_state(self)
  fail RestAdapter::Errors::ArgumentError,
       'Invalid argument, must pass name' if RestAdapter::Utils.is_blank?(name)

  properties = @rest_adapter.create_folder(name, path: @url, exists: exists)
  FileSystemCommon.create_item_from_hash(@rest_adapter, parent: @url, ** properties)
end

#upload(file_system_path, name: nil, exists: 'FAIL', upload_io: false) ⇒ File

Upload file to this folder

Examples:

Upload local file path
	file = folder.upload(local_file_path, name: "testfile.txt")
Upload ::File instance
file = ::File.open(local_file_path, "r") do |fp|
		folder.upload(fp, name: "testfile.txt", upload_io: true)
end
Upload string
	file = folder.upload("This is upload string",
		name: 'testfile.txt', upload_io: true)
Upload stream
	io = StringIO.new
	io.write("this is test stringio")
	file = folder.upload(io, name: 'testfile.txt', upload_io: true)
	io.close

Parameters:

  • file_system_path (String, #read&#pos&#pos=)

    local file path, in-memory string, an io object for example StringIO, File, Tempfile.

  • name (String) (defaults to: nil)

    default: nil, name of uploaded file, must be set if file_system_path does not respond to #path unless file_system_path is local file path

  • exists (String) (defaults to: 'FAIL')

    ('FAIL', 'OVERWRITE', 'RENAME') action to take in case of a conflict with an existing folder.

  • upload_io (Boolean) (defaults to: false)

    default: false, if set to false, file_system_path is considered to be a local file path

Returns:

  • (File)

    instance reference to uploaded file

Raises:

  • (Client::Errors::SessionNotLinked, Client::Errors::ServiceError, Client::Errors::InvalidItemError, Client::Errors::OperationNotAllowedError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloudfs/folder.rb', line 49

def upload(file_system_path, name: nil, exists: 'FAIL', upload_io: false)
  FileSystemCommon.validate_item_state(self)
  fail RestAdapter::Errors::ArgumentError,
       'Invalid input, expected file system path.' if RestAdapter::Utils.is_blank?(file_system_path)

  if upload_io == false
    response = ::File.open(file_system_path, 'r') do |file|
      @rest_adapter.upload(@url, file, name: name, exists: exists)
    end
  else
    response = @rest_adapter.upload(@url, file_system_path, name: name, exists: exists)
  end
  FileSystemCommon.create_item_from_hash(@rest_adapter,
                                         parent: @url, ** response)
end