Box: Download File
Download the specified file in Box. If you do not specify a file name, it will be saved with the file name in Box.
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 );

}

Download

2021-02-09 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/bpmn-icons/service-task-box-file-download/
The Addon-import feature is available with Professional or Enterprise edition.

Notes

  1. File ID is contained in the URL: https://app.box.com/file/(File ID)
  2. The refresh token for Box has the expiration. Use regularly to ensure that it does not exceed the expiration. (Box: Token & URL Expiration)

Capture

See also

%d bloggers like this: