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.
[box] upload file and set Password/ Expiration
box-file-update-20171010-addonxml
(md5:06cf737a196ae433241ed0310ff3ae47)

    • B’: Folder ID stored in the selected Data Item at B
    • C’: (One) file stored in the selected Data Item at C
    • D’: Download expiration date stored in the selected Data Item at D
    • E’: Download password stored in the selected Data Item at E
    • A file is uploaded to the specified folder of Box, and the expiration date and the password for the download are set
    • Download URL will be stored in the data item selected at F
    • The communication logs will be stored in the data item selected at X
    • Only one file will be uploaded
    • It is recommended to set “Max number of files: 1” at the File type Data Item for C

    A. Set OAuth2 Config Name (at [OAuth 2.0 Setting])Single-line inputRequired
    B. Select STRING/SELECT for Folder ID on box ("0" for Root)Data selectString(single line)orSelect(radio/select/search)Required
    C. Select FILE DATA for uploadData selectFileRequired
    D. Select DATE DATA for file download expirationData selectDateRequired
    E. Select STRING DATA for download passwordData selectStringRequired
    F. Select STRING DATA for download URLData selectStringRequired
    X. Select STRING DATA for Access Log (update)Data selectString(multiple line)
    -variable namevariable label
    Aconf_OAuth2Set OAuth2 Config Name (at [OAuth 2.0 Setting])
    Bconf_DataIdBSelect STRING/SELECT for Folder ID on box ("0" for Root)
    Cconf_DataIdCSelect FILE DATA for upload
    Dconf_DataIdDSelect DATE DATA for file download expiration
    Econf_DataIdESelect STRING DATA for download password
    Fconf_DataIdFSelect STRING DATA for download URL
    Xconf_DataIdXSelect STRING DATA for Access Log (update)
    // (c) 2017, Questetra, Inc. (the MIT License)
    
    //// == 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);
    }