- An XML file that contains the code below is available to download
- box-file-link-create.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() {
// get OAuth2 Setting
const oauth2 = configs.get("conf_OAuth2");
const fileId = decideFileId();
const unsharedDateDef = configs.getObject("conf_UnsharedAt");
const passwordDef = configs.getObject("conf_Password");
let unsharedDate = null;
if (unsharedDateDef !== null) {
unsharedDate = engine.findData(unsharedDateDef);
}
let password = "";
if (passwordDef !== null) {
password = engine.findData(passwordDef);
}
checkExistingSharedLink(oauth2, fileId);
const sharedlinkUrl = createSharedLink(oauth2, fileId, unsharedDate, password);
const urlDef = configs.getObject("conf_SharedLink");
if (urlDef !== null) {
engine.setData(urlDef, sharedlinkUrl);
}
}
/**
* ファイルの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 `Shared link(${jsonRes.shared_link.url}) was already created.`;
}
}
/**
* Create Shared Link to File on Box 共有リンク作成
* @param {String} oauth OAuth2 設定
* @param {String} fileId ファイルの ID
* @param {AddableTimestamp} unsharedDate 有効期限
* @param {String} password パスワード
* @return {String} ファイルの共有リンクURL
*/
function createSharedLink(oauth2, fileId, unsharedDate, password) {
let timezone = engine.getTimeZoneId();
if (timezone === "GMT") {
timezone += "+00:00";
}
const jsonBody = {};
jsonBody["shared_link"] = { "access": "open" };
if (unsharedDate !== null) {
jsonBody["shared_link"]["unshared_at"] = unsharedDate.toString() + timezone.slice(3);
}
if (password !== "" && password !== null) {
jsonBody["shared_link"]["password"] = password;
}
jsonBody["shared_link"]["permissions"] = { "can_download": true };
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) {
engine.log(responseTxt);
throw `Failed to create Shared Link. status:${status}`;
}
const jsonRes = JSON.parse(responseTxt);
return jsonRes.shared_link.url;
}
Pingback: Box: Delete Shared Link of File – Questetra Support
Pingback: Box: Add Collaboration – Questetra Support