- An XML file that contains the code below is available to download
- box-file-link-delete.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 fileId = decideFileId();
checkExistingSharedLink(oauth2, fileId);
deleteSharedLink(oauth2, fileId);
}
/**
* ファイルのIDをconfigから読み出して出力する。
* @return {String} fileId ファイルの ID
*/
function decideFileId() {
let fileId = "";
const fileIdDef = configs.getObject("conf_FileId");
if (fileIdDef === null) {
fileId = configs.get("conf_FileId");
} else {
fileId = engine.findData(fileIdDef);
}
if (fileId === "" || fileId === null) {
throw "File ID is blank";
}
return fileId;
}
/**
* ファイルに共有リンクが作成されているか調べ、無い場合はエラーにする
* @param {String} oauth OAuth2 設定
* @param {String} fileId ファイルの ID
*/
function checkExistingSharedLink(oauth2, fileId) {
const url = `https://api.box.com/2.0/files/${fileId}?fields=shared_link`;
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 information. status:${status}`;
}
const jsonRes = JSON.parse(responseTxt);
if (jsonRes.shared_link === null) {
throw `Failed to Delete Shared Link. \n The file(ID:${fileId}) doesn't have a Shared link.`;
}
}
/**
* Delete Shared Link to File on Box 共有リンク削除
* @param {String} oauth2 OAuth2 設定
* @param {String} fileId 共有リンクを削除したいファイルのID
*/
function deleteSharedLink(oauth2, fileId) {
const jsonBody = {};
jsonBody["shared_link"] = null;
const url = `https://api.box.com/2.0/files/${fileId}?fields=shared_link`;
const response = httpClient
.begin()
.authSetting(oauth2)
.body(JSON.stringify(jsonBody), "application/json; charset=UTF-8")
.put(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status !== 200) {
const error = `Failed to delete. status:${status}`;
engine.log(responseTxt);
throw error;
}
engine.log(`status: ${status} Shared Link was deleted, file ID: ${fileId}`);
}
Pingback: Box: Create Shared Link to File – Questetra Support