- An XML file that contains the code below is available to download
- box-folder-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(){
const folderId = retrieveFolderId();
const dateItem = configs.get("unsharedAt");
const passwordItem = configs.get("DownloadPass");
let unsharedDate = null;
if (dateItem !== "" && dateItem !== null){
unsharedDate = engine.findDataByNumber(dateItem);
}
let password = "";
if (passwordItem !== "" && passwordItem !== null){
password = engine.findDataByNumber(passwordItem);
}
// get OAuth2 Setting
const oauth2 = configs.get("OAuth2");
// フォルダが既に共有リンク作成されているか調べる
const existingUrl = getFolderInformation(oauth2, folderId);
//フォルダに既に共有リンクがある場合はエラーにする 共有リンクが無い場合は作成する
if (existingUrl !== null){
throw `Shared link(${existingUrl.url}) was already created.`;
}
const sharedlinkUrl = createSharedLink(oauth2, folderId, unsharedDate, password);
const UrlData = configs.getObject( "DownloadUrlItem" );
if (UrlData !== null) {
engine.setData(UrlData,sharedlinkUrl);
}
}
/**
* config からフォルダ ID を読み出す。空文字列の場合はエラー
* @return {String} folderId フォルダ ID
*/
function retrieveFolderId() {
const folderIdV1 = configs.get("FolderId");
if (folderIdV1 !== null && folderIdV1 !== "") {
return folderIdV1;
}
let folderId = configs.get("FolderId_V2"); // 固定値の場合
const folderIdDef = configs.getObject("FolderId_V2");
if (folderIdDef !== null) { // 文字型データ項目が選択されている場合
folderId = engine.findData(folderIdDef);
}
if (folderId === null || folderId === "") {
throw "Folder ID is blank";
}
return folderId;
}
/**
* Get Folder Information on Box フォルダが既に共有リンク作成されているか調べる
* @return {String} フォルダの共有リンクオブジェクト
*/
function getFolderInformation(oauth2, folderId) {
const url = `https://api.box.com/2.0/folders/${folderId}?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 folder information";
}
const jsonRes = JSON.parse(responseTxt);
return jsonRes.shared_link;
}
/**
* Create Shared Link to Folder on Box 共有リンク作成
* @return {String} フォルダの共有リンクURL
*/
function createSharedLink(oauth2, folderId, unsharedDate, password) {
let timezone = engine.getTimeZoneId();
if (timezone === "GMT"){
timezone += "+00:00";
}
let 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/folders/${folderId}?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";
}
const jsonRes = JSON.parse(responseTxt);
return jsonRes.shared_link.url;
}
Pingback: Box File Upload (Password / Expiration) – Questetra Support
Pingback: Box: Delete Shared Link of Folder – Questetra Support
Pingback: Utilising Box From Your Workflow – Questetra Support