Box File Upload (Password / Expiration)
Uploads a file specifying the folder of Box, obtains the URL for publication and stores it in String type Data Item. Since you can set download password and download expiration date, it can be used for file sharing etc.
2017 © Questetra, Inc. (MIT License)
Configs
  • A: Set OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Select STRING/SELECT for Folder ID on box (“0” for Root) *
  • C: Select FILE DATA for upload *
  • D: Select DATE DATA for file download expiration *
  • E: Select STRING DATA for download password *
  • F: Select STRING DATA for download URL (update) *
  • X: Select STRING DATA for Access Log (update)
Script
//// == Config Retrieving / 工程コンフィグの参照 ==
var oauth2  = configs.get("conf_OAuth2") + "";
var dataIdB = configs.get("conf_DataIdB") + "";
var dataIdC = configs.get("conf_DataIdC") + "";
var dataIdD = configs.get("conf_DataIdD") + "";
var dataIdE = configs.get("conf_DataIdE") + "";
var dataIdF = configs.get("conf_DataIdF") + "";
var dataIdX = configs.get("conf_DataIdX") + "";

//// == Data Retrieving / ワークフローデータの参照 ==
var folderId = "";
if(engine.findDataByNumber(dataIdB) instanceof java.util.ArrayList ){
  folderId = engine.findDataByNumber(dataIdB).get(0).getValue() + "";
}else if( engine.findDataByNumber(dataIdB) instanceof java.lang.String ){
  folderId = engine.findDataByNumber(dataIdB) + "";
}
var files = engine.findDataByNumber(dataIdC);
var download_limit = engine.findDataByNumber(dataIdD) + "";
var download_password = engine.findDataByNumber(dataIdE) + "";

//// == File Upload ==
var accessLog = "";
var responseJson = "";
var token = httpClient.getOAuth2Token( oauth2 );

if (files !== null) {
  var file = files.get(0);
  var attributes = {
    parent : {id : folderId}
  };
  attributes["name"] = String(file.getName());

  var response = httpClient.begin()
    .bearer(token)
    .multipart('attributes', JSON.stringify(attributes))
    .multipart('file', file)
    .post('https://upload.box.com/api/2.0/files/content');
  accessLog += "---POST request--- " + response.getStatusCode() + "\n";
  accessLog += file.getName() + "\n";
  responseJson += response.getResponseAsString() + "\n";
}

// for Debug
accessLog += responseJson + "\n";

//// == ワークフローデータへの代入 / Data Updating ==
if( dataIdX !== "" ){
  engine.setDataByNumber(dataIdX,accessLog);
}

var update_info_obj = {};

update_info_obj.shared_link = {};
update_info_obj.shared_link.access = "open";
update_info_obj.shared_link.password = download_password;
update_info_obj.shared_link.unshared_at = download_limit;
update_info_obj.shared_link.permissions = {};
update_info_obj.shared_link.permissions.can_download = true;

var file_obj = JSON.parse(responseJson);
var file_id = file_obj.entries[0].id;

var response_update = httpClient.begin()
      .bearer(token)
      .body(JSON.stringify(update_info_obj), "application/json" )
      .put('https://api.box.com/2.0/files/' + file_id);

accessLog += "---PUT request--- "
    + response_update.getStatusCode() + "\n";
var updateResponseJson = response_update.getResponseAsString() + "\n";
var update_file_obj = JSON.parse(updateResponseJson);
engine.setDataByNumber(dataIdF, update_file_obj.shared_link.download_url);

// for Debug
accessLog += updateResponseJson + "\n";

if( dataIdX !== "" ){
  engine.setDataByNumber(dataIdX,accessLog);
}

Download

Capture

Notes

  • Only one file will be uploaded
  • It is recommended to set “Max number of files: 1” at the File type Data Item for C
%d bloggers like this: