- An XML file that contains the code below is available to download
- box-file-move.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(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2 = configs.get( "conf_OAuth2" ) ;
const fileId = decideFileId();
if(fileId === "" || fileId === null){
throw "No File ID";
}
const folderId = decideFolderId();
if(folderId === "" || folderId === null){
//空の場合はエラーとする when folder id isn't set, throw error
throw "No Folder ID";
}
moveFile(oauth2, folderId, fileId);
}
/**
* ファイルのIDをconfigから読み出して出力する。
* @return {String} ファイルの ID
*/
function decideFileId(){
const fileIdDef = configs.getObject("conf_FileId");
const fileId = engine.findData(fileIdDef);
return fileId;
}
/**
* フォルダのIDをconfigから読み出して出力する。
* @return {String} フォルダの ID
*/
function decideFolderId(){
let folderId = "";
const folderIdDef = configs.getObject("conf_FolderId");
if(folderIdDef === null){
folderId = configs.get( "conf_FolderId");
}else{
folderId = engine.findData(folderIdDef);
}
return folderId;
}
/**
* Box 上のファイルを指定フォルダに移動する。
*/
function moveFile(oauth2, folderId, fileId) {
const jsonReq = {};
jsonReq["parent"] = {"id": folderId };
const url = `https://api.box.com/2.0/files/${fileId}`;
const response = httpClient.begin()
.authSetting(oauth2)
// fields には id を指定(レスポンスを基本レプリゼンテーションに縮小するため)
.queryParam("fields", "id")
.body(JSON.stringify(jsonReq), "application/json; charset=UTF-8")
.put(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status >= 300) {
engine.log(responseTxt);
throw `Failed to move. status: ${status}`;
}
}
Pingback: Process Start Triggered by a File Upload to Box – Questetra Support