
To use Dropbox functionality in BrightServer scripts, a verified Dropbox account must be used. This account must contain an application, whose token is used in order to access the account via BrightServer scripts.
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token) ;
ScriptSession.logInfo("Getting account details...") ;
ScriptSession.logInfo("Account Id = " + dropbox.getId()) ;
ScriptSession.logInfo("Account Name = " + dropbox.getName()) ;
ScriptSession.logInfo("Account Surname = " + dropbox.getSurname()) ;
ScriptSession.logInfo("Account Display Name = " + dropbox.getDisplayName()) ;
ScriptSession.logInfo("Account Disabled = " + dropbox.isDisabled()) ;
ScriptSession.logInfo("Account Type = " + dropbox.getAccountType()) ;
ScriptSession.logInfo("Account Email = " + dropbox.getEmail()) ;
}
catch (error)
{
ScriptSession.logError("Error accessing Dropbox: " + error.toString());
}
Note:
Dropbox access token should be used in place of "<YOUR-ACCESS-TOKEN>". For more information on generating this token, please refer to Generating Dropbox Access Token section in the Appendix. An incorrect token will throw a server exception.
BrightServer will retrieve information for the Dropbox account and display it in the server log under INFO.
function run()
{
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token);
ScriptSession.logInfo("Listing files...") ;
listDirectory(dropbox, "", " ");
}
catch (error)
{
ScriptSession.logError("Error accessing Dropbox: " + error.toString());
}
}
function listDirectory(db, path, indent)
{
var files = db.getFileList(path);
for (var i = 0; i < files.length; i++)
{
var metadata = db.getMetadata(files[i]) ;
if (metadata.isFile())
{
ScriptSession.logInfo(indent + "File : " + files[i]) ;
}
else
{
ScriptSession.logInfo(indent + "Folder : " + files[i]) ;
listDirectory(db, files[i], indent + " ");
}
}
}
Note:
Token is used as demonstrated in Example 1.
The above code lists out the entire file/directory structure for a dropbox directory, via recursive 'listDirectory' function.
DropboxMetadata of the file is inspected, checking the isFile() method. If it is a file, it is written to output as a file. If a directory, it is written to output as a directory, and then also traversed using the same function.
As "" is specified as the initial path, all files and directories for the root directory will be listed.
Listing is done via BrightServer output in INFO.
Indenting is passed to the recursive function for readability purposes. The deeper the file or directory, the more indented it will appear.
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token);
var exampleFile = "/exampleFile.txt";
var metadata = db.getMetadata(exampleFile);
ScriptSession.logInfo("Id: " + metadata.getId());
ScriptSession.logInfo("Name: " + metadata.getName());
ScriptSession.logInfo("Is File: " + metadata.isFile());
ScriptSession.logInfo("Is Folder: " + metadata.isFolder());
ScriptSession.logInfo("Client Modified: " + metadata.getClientModifiedDate());
ScriptSession.logInfo("Server Modified: " + metadata.getServerModifiedDate());
ScriptSession.logInfo("File Size: " + metadata.getFileSize());
ScriptSession.logInfo("Revision: " + metadata.getRevision());
}
catch (error)
{
ScriptSession.logError("Error accessing file data on Dropbox: " + error.toString());
}
Note:
Token is used as demonstrated in Example 1.
The above code will retrieve the metadata for the 'exampleFile.txt' file in the root folder of the dropbox specified. It will then proceed to retrieve the file's metadata and output the fields to BrightServer's INFO log.
If the file does not exist, an exception will be thrown
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token);
ScriptSession.logInfo("Save File Locally:");
var localFile = "C:/Temp/dropboxUpdated.txt";
var date = Calendar.getInstance().getTime();
var blob = new Blob(date.toString().getBytes());
blob.writeToFile(localFile);
ScriptSession.logInfo("Write File to Dropbox:");
var destFile = "/lastUpdate.txt";
db.uploadFile(localFile, destFile, true);
}
catch (error)
{
ScriptSession.logError("Error uploading file to Dropbox: " + error.toString());
}
Note:
Token is used as demonstrated in Example 1.
The above code will create a text file at 'C:/Temp/dropboxUpdated.txt' location with the current date/time.
The file created will then be written to dropbox using the uploadFile() method. As the 'bOverwrite' flag is true, if the file already exists on dropbox, it will be overwritten.
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token);
ScriptSession.logInfo("Download File from Dropbox:");
var srcFile = "/lastUpdate.txt";
var destFile= "C:/Temp/dropboxUpdated.txt";
db.downloadFile(srcFile, destFile);
}
catch (error)
{
ScriptSession.logError("Error downloading file from Dropbox: " + error.toString()) ;
}
Note:
Token is used as demonstrated in Example 1.
The above code will download the 'lastUpdate.txt' file from the root directory of Dropbox to 'C:/Temp/dropboxUpdated.txt'.
Full path of Dropbox location must be provided.
If the file specified by destFile already exists on the local machine, it will be overwritten.
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token);
ScriptSession.logInfo("Delete file from Dropbox:");
var deletePath = "/fileToDelete.txt";
db.deleteFile(deletePath );
}
catch (error)
{
ScriptSession.logError("Error deleting file from Dropbox: " + error.toString()) ;
}
Note:
Token is used as demonstrated in Example 1.
The above code will attempt to delete the file at 'fileToDelete.txt' from the root directory of DropBox.
Full path of Dropbox location must be provided.
Due to Dropbox implementation, depending on the Dropbox account type, deleting may not be supported, causing an exception to be thrown.
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token);
ScriptSession.logInfo("Delete file from Dropbox:");
var originalFolder = "/OriginalFolder";
var copyDestination = "/DestinationFolder/CopyDestination";
db.copy(originalFolder, copyDestination, false);
}
catch (error)
{
ScriptSession.logError("Error copying folder: " + error.toString()) ;
}
Note:
Token is used as demonstrated in Example 1.
The code above takes a folder on dropbox and moves it to a sub-directory in the root folder. This may also be performed for files.
If the file does not exist, an exception will be thrown. If the folder does not exist, it will be created as part of the copy.
The full path must be provided for both source and destination.
bAutoRename flag is set to false, which will cause an exception if a file with the same name already exists. Only enable this flag if data is to be logged and does not require to be re-accessed in the script.
var token = "<YOUR-ACCESS-TOKEN>";
try
{
var dropbox = ScriptSession.createDropboxObject(token);
ScriptSession.logInfo("Move file to directory in Dropbox:");
var fileName = "/fileName.txt";
var folderName = "/testFolder";
db.move(fileName, folderName+fileName, false);
}
catch (error)
{
ScriptSession.logError("Error moving file: " + error.toString()) ;
}
Note:
Token is used as demonstrated in Example 1.
The code above takes a file on dropbox and moves it to a sub-folder in the root folder. This may also be performed for moving folders.
If the file does not exist, an exception will be thrown. If the folder does not exist, it will be created as part of the move.
The full path must be provided for both source and destination.
bAutoRename flag is set to false, which will cause an exception if a file with the same name already exists. Only enable this flag if data is to be logged and does not require to be re-accessed in the script.