Box: Download File
Downloads the specified file in Box. If you do not specify a file name, it will be saved with the file name in Box.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: OAuth2 Setting *
  • C2: File ID to download *
  • C3: File Name (named with the file name in Box if blank)#{EL}
  • C4: File type data item to add the downloaded file *
Script (click to open)

main();
function main(){

const oauth2 = configs.get("conf_OAuth2");

const fileIdDef = configs.getObject("conf_FileId");
let fileId = decideFileId(fileIdDef);

let fileName = configs.get( "conf_FileName" );
if(fileName === "" || fileName === null){
// Boxでのファイル名を取得する
fileName = getBoxFileName(oauth2, fileId);
}

const {contentType, content} = downloadBoxFile(oauth2, fileId);

const filesDef = configs.getObject("conf_Files");
saveFile(filesDef, fileName, contentType, content);

}

/**
* ダウンロードするファイルのIDをconfigから読み出して出力する
* @param {String} fileIdDef
* @return {String} fileId ファイルの ID
*/
function decideFileId(fileIdDef){
let fileId = "";
if(fileIdDef === null){
fileId = configs.get( "conf_FileId");
}else{
fileId = engine.findData(fileIdDef);
}
if(fileId === "" ||fileId === null) {
throw "fileId is blank";
}
return fileId;
}

/**
* Boxのファイル名を取得する
* @param {String} oauth2 認証設定
* @param {String} fileId ダウンロードするファイルのID
* @return {String} jsonRes.name Boxでのファイル名
*/
function getBoxFileName(oauth2, fileId){
const url = `https://api.box.com/2.0/files/${fileId}/`;
const response = httpClient.begin()
.authSetting(oauth2)
.get(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status !== 200) {
engine.log(`${responseTxt}`);
throw `Failed to download file. status: ${status}`;
}
let jsonRes;
try {
jsonRes = JSON.parse(responseTxt);
} catch(e) {
engine.log("failed to parse as json");
throw `Failed to download file. status: ${status}`;
}
return jsonRes.name;
}

/**
* box API にファイルダウンロードの GET リクエストを送信し、レスポンスを返す
* @param {String} oauth2 認証設定
* @param {String} folderId ダウンロードするファイルのID
* @return {HttpResponseWrapper} contentType
* @return {HttpResponseWrapper} content
*/
function downloadBoxFile(oauth2, fileId){

const url = `https://api.box.com/2.0/files/${fileId}/content/`;
const response = httpClient.begin()
.authSetting(oauth2)
.get(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();

if (status !== 200) {
engine.log(`${responseTxt}`);
throw `Failed to download file. status: ${status}`;
}
return {
contentType: response.getContentType(),
content: response.getResponse()
};
}

/**
* ダウンロードしたファイルを保存する
* @param {String} fileName 保存する際のファイル名
* @param {Qfile} file ファイル
* @param {HttpResponseWrapper} contentType
* @param {HttpResponseWrapper} content
*/
function saveFile(filesDef, fileName, contentType, content){

let files = engine.findData( filesDef );
if (files === null) {
files = new java.util.ArrayList();
}

const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
fileName, contentType, content
);

files.add( qfile );

// == ワークフローデータへ代入する
engine.setData( filesDef, files );

}

Notes

Capture


See also

Appendix

  • box-file-download.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on

1 thought on “Box: Download File”

  1. Pingback: Process Start Triggered by a File Upload to Box – Questetra Support

Comments are closed.

%d bloggers like this: