new Folder()

This class allows you to interact with the folders in the file system of your site.

Creating a folder

A folder object can be instantiated in two ways.

You can specify the path of the folder:

var folder = new BCAPI.Models.FileSystem.Folder('/folder/path');

You can also specify the name of the folder, and the parent directory:

var folder = new BCAPI.Models.FileSystem.Folder({
    'parent': BCAPI.Models.FileSystem.Root,
    'name': 'my-folder'
});

The root directory / cannot be created like this. You can only get it with:

var root = BCAPI.Models.FileSystem.Root;
console.log(root.get("path")); 

At this moment the folder does not exist just yet, we need to call the create() function to actually write the folder to the disk. Here is a complete example:

var folder = new BCAPI.Models.FileSystem.Folder("something/test1/test2/my-folder");
folder.create().done(function () {
    console.log("The folder has been created !");
});

In the example above if the folders something, test1 and test2 does not exist they will all be created on the fly.

Get the folder's metadata

You use fetch to obtain the folder's details, including the files & folders that the folder contains:

var folder = new BCAPI.Models.FileSystem.Folder("/css");
folder.fetch().done(function () {
    console.log("Folder last update date is: " + folder.get("lastModified"));
    console.log("Printing the folder contents: ");
    var contents = folder.get("contents");
    for (var i = 0; i < contents.length; i++) {
        var entity = contents[i];
        var isFile = entity instanceof BCAPI.Models.FileSystem.File;
        //you can also use:
        //var isFile = entity.get("type") === "file";
        if (isFile) {
            console.log("File " + entity.get("name") + " updated at " + entity.get("lastModified"));
        } else {
            console.log("Folder " + entity.get("name"));
        }
    }
});

Rename the folder

Use save to rename a folder:

var folder = new BCAPI.Models.FileSystem.Folder("/my/folder");
folder.set("name", "new-folder");
folder.save().done(function () {
        console.log("The folder has been renamed");
        console.log("Path is now " + folder.get("path")); 
}).error(function (jqXHR) {
    console.log("Rename failed.");
    console.log("Error code: " + jqXHR.status);
    console.log("Error text: " + jqXHR.statusText);
    console.log("Response text: " + jqXHR.responseText);
});

Delete the folder

var folder = new BCAPI.Models.FileSystem.Folder("/my-folder");
folder.destroy().done(function () {
    console.log("Folder was deleted");
}).error(function (jqXHR) {
    console.log("Delete failed.");
    console.log("Error code: " + jqXHR.status);
    console.log("Error text: " + jqXHR.statusText);
    console.log("Response text: " + jqXHR.responseText);
});

Source:

Notes:

  • BCAPI.Models.FileSystem.Root is the root folder in your site's file structure. You can also create a file object by specifying the file's full path.
  • If you omit the / at the beginning of the file path, the system will add this.
  • When creating a folder, only creating an instance of the folder class doesn't actually create the folder on the server. If the folder doesn't exist yet, a call to create is required.

Methods

create() → {promise}

Creates the specified folder on the server.

Source:

Returns

A promised that will be resolved when the folder is created

Type: Promise

file(name, attributes, options) → {BCAPI.Models.FileSystem.File}

Creates a file object with the specified name and that has as parent this folder.

Parameters

Name Type Description
name string The name of the file
attributes object Properties of the file
options object Options for the file

Source:

Returns

A file that is a child of this folder.