- 下記のスクリプトを記述した XML ファイルをダウンロードできます
main();
function main(){
const oauth2 = configs.get('conf_OAuth2');
const fileId = decideFileId();
applyWatermark(oauth2, fileId);
}
/**
* ファイル ID を config から読み出して出力する
* @return {String} fileId ファイル ID
*/
function decideFileId(){
const fileId = engine.findData(configs.getObject('conf_FileId'));
if (fileId === '' || fileId === null) {
throw 'File ID is blank.';
}
return fileId;
}
/**
* 電子すかしを適用
* @param {String} oauth OAuth2 設定
* @param {String} fileId ファイル ID
*/
function applyWatermark(oauth2, fileId) {
const jsonBody = {};
jsonBody['watermark'] = {'imprint': 'default'};
const url = `https://api.box.com/2.0/files/${encodeURIComponent(fileId)}/watermark`;
const response = httpClient.begin()
.authSetting(oauth2)
.body(JSON.stringify(jsonBody), 'application/json; charset=UTF-8')
.put(url);
const status = response.getStatusCode();
if (status !== 200 && status !== 201) { // 200: 更新, 201: 新規適用
engine.log(response.getResponseAsString());
throw `Failed to apply watermark. status:${status}`;
}
}