Box: Download File
This item downloads the specified file on Box. If you do not specify a file name, it will be saved with the file name on 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 *

Notes

  • File ID is contained in the URL: https://app.box.com/file/(File ID)
  • The refresh token for Box has an expiration date

Capture


See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • 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

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 get file name. status: ${status}`;
  }
  let jsonRes;
  try {
    jsonRes = JSON.parse(responseTxt);
  } catch(e) {
    engine.log("failed to parse as json");
    throw `Failed to get file name. status: ${status}`;
  }
return jsonRes.name;
}

/**
  * box API にファイルダウンロードの GET リクエストを送信し、レスポンスを返す
  * @param {String} oauth2 認証設定
  * @param {String} folderId  ダウンロードするファイルのID
  * @return {Object} response
  * @return {String} response.contentType
  * @return {ByteArrayWrapper} response.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 {ProcessDataDefinitionView} filesDef  保存先データ項目
  * @param {String} fileName  保存する際のファイル名
  * @param {String} contentType
  * @param {ByteArrayWrapper} 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 );

}

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: