From 85bdc455c40fc55bd450e827be7c6523b7c19eaa Mon Sep 17 00:00:00 2001 From: Raya Straus Date: Mon, 29 Jun 2020 14:20:33 +0300 Subject: [PATCH 1/7] added download backedup asset function --- lib-es5/api_client/call_api.js | 4 +--- lib-es5/utils/consts.js | 8 ++++++-- lib-es5/utils/index.js | 35 +++++++++++++++++++++++++++++++--- lib/api_client/call_api.js | 4 +--- lib/utils/consts.js | 5 ++++- lib/utils/index.js | 30 ++++++++++++++++++++++++++--- test/archivespec.js | 8 ++++++++ 7 files changed, 79 insertions(+), 15 deletions(-) diff --git a/lib-es5/api_client/call_api.js b/lib-es5/api_client/call_api.js index 9ad70065..d813a6f9 100644 --- a/lib-es5/api_client/call_api.js +++ b/lib-es5/api_client/call_api.js @@ -11,9 +11,7 @@ var ensurePresenceOf = utils.ensurePresenceOf; function call_api(method, uri, params, callback, options) { ensurePresenceOf({ method, uri }); - var cloudinary = ensureOption(options, "upload_prefix", "https://api.cloudinary.com"); - var cloud_name = ensureOption(options, "cloud_name"); - var api_url = [cloudinary, "v1_1", cloud_name].concat(uri).join("/"); + var api_url = utils.base_api_url(uri); var auth = { key: ensureOption(options, "api_key"), secret: ensureOption(options, "api_secret") diff --git a/lib-es5/utils/consts.js b/lib-es5/utils/consts.js index f56ffaa1..167c205e 100644 --- a/lib-es5/utils/consts.js +++ b/lib-es5/utils/consts.js @@ -57,7 +57,8 @@ var PREDEFINED_VARS = { "width": "w" }; -var TRANSFORMATION_PARAMS = ['angle', 'aspect_ratio', 'audio_codec', 'audio_frequency', 'background', 'bit_rate', 'border', 'color', 'color_space', 'crop', 'default_image', 'delay', 'density', 'dpr', 'duration', 'effect', 'end_offset', 'fetch_format', 'flags', 'fps', 'gravity', 'height', 'if', 'keyframe_interval', 'offset', 'opacity', 'overlay', 'page', 'prefix', 'quality', 'radius', 'raw_transformation', 'responsive_width', 'size', 'start_offset', 'streaming_profile', 'transformation', 'underlay', 'variables', 'video_codec', 'video_sampling', 'width', 'x', 'y', 'zoom']; +var TRANSFORMATION_PARAMS = ['angle', 'aspect_ratio', 'audio_codec', 'audio_frequency', 'background', 'bit_rate', 'border', 'color', 'color_space', 'crop', 'default_image', 'delay', 'density', 'dpr', 'duration', 'effect', 'end_offset', 'fetch_format', 'flags', 'fps', 'gravity', 'height', 'if', 'keyframe_interval', 'offset', 'opacity', 'overlay', 'page', 'prefix', 'quality', 'radius', 'raw_transformation', 'responsive_width', 'size', 'start_offset', 'streaming_profile', 'transformation', 'underlay', 'variables', 'video_codec', 'video_sampling', 'width', 'x', 'y', 'zoom' // + any key that starts with '$' +]; var LAYER_KEYWORD_PARAMS = { font_weight: "normal", @@ -67,6 +68,8 @@ var LAYER_KEYWORD_PARAMS = { stroke: "none" }; +var UPLOAD_PREFIX = "https://api.cloudinary.com"; + module.exports = { DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION, DEFAULT_POSTER_OPTIONS, @@ -75,5 +78,6 @@ module.exports = { PREDEFINED_VARS, LAYER_KEYWORD_PARAMS, TRANSFORMATION_PARAMS, - SIMPLE_PARAMS + SIMPLE_PARAMS, + UPLOAD_PREFIX }; \ No newline at end of file diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index 2c0ddd59..75f01cfe 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -56,6 +56,7 @@ var ensureOption = require('./ensureOption').defaults(config()); var entries = require('./entries'); var isRemoteUrl = require('./isRemoteUrl'); var getSDKVersionID = require('./encoding/sdkVersionID/getSDKVersionID'); +var UPLOAD_PREFIX = require('./consts'); exports = module.exports; var utils = module.exports; @@ -916,14 +917,21 @@ function unsigned_url_prefix(source, cloud_name, private_cdn, cdn_subdomain, sec return prefix; } +function base_api_url() { + var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; + var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + + var cloudinary = ensureOption(options, "upload_prefix", UPLOAD_PREFIX); + var cloud_name = ensureOption(options, "cloud_name"); + return [cloudinary, "v1_1", cloud_name].concat(path).join("/"); +} + function api_url() { var action = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'upload'; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - var cloudinary = ensureOption(options, "upload_prefix", "https://api.cloudinary.com"); - var cloud_name = ensureOption(options, "cloud_name"); var resource_type = options.resource_type || "image"; - return [cloudinary, "v1_1", cloud_name, resource_type, action].join("/"); + return base_api_url([resource_type, action]); } function random_public_id() { @@ -1060,6 +1068,24 @@ function zip_download_url(tag) { return exports.api_url("download_tag.zip", options) + "?" + hashToQuery(params); } +/** + * The returned url should allow downloading the backedup asset + * @param asset_id + * @param version_id + * @param options + * @returns {string } + */ +function download_backedup_asset(asset_id, version_id) { + var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; + + var params = exports.sign_request({ + timestamp: options.timestamp || exports.timestamp(), + asset_id: asset_id, + version_id: version_id + }, options); + return exports.base_api_url(['download_backup'], options) + "?" + hashToQuery(params); +} + /** * Returns a URL that when invokes creates an archive and returns it. * @param {object} options @@ -1523,6 +1549,9 @@ exports.only = pickOnlyExistingValues; // for backwards compatibility exports.pickOnlyExistingValues = pickOnlyExistingValues; exports.jsonArrayParam = jsonArrayParam; exports.download_folder = download_folder; +exports.base_api_url = base_api_url; +exports.download_backedup_asset = download_backedup_asset; + // was exported before, so kept for backwards compatibility exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS; exports.DEFAULT_VIDEO_SOURCE_TYPES = DEFAULT_VIDEO_SOURCE_TYPES; diff --git a/lib/api_client/call_api.js b/lib/api_client/call_api.js index 372df6eb..249cab99 100644 --- a/lib/api_client/call_api.js +++ b/lib/api_client/call_api.js @@ -8,9 +8,7 @@ const { ensurePresenceOf } = utils; function call_api(method, uri, params, callback, options) { ensurePresenceOf({ method, uri }); - const cloudinary = ensureOption(options, "upload_prefix", "https://api.cloudinary.com"); - const cloud_name = ensureOption(options, "cloud_name"); - const api_url = [cloudinary, "v1_1", cloud_name].concat(uri).join("/"); + const api_url = utils.base_api_url(uri); const auth = { key: ensureOption(options, "api_key"), secret: ensureOption(options, "api_secret") diff --git a/lib/utils/consts.js b/lib/utils/consts.js index 38a57a71..d2eb686f 100644 --- a/lib/utils/consts.js +++ b/lib/utils/consts.js @@ -129,6 +129,8 @@ const LAYER_KEYWORD_PARAMS = { stroke: "none" }; +const UPLOAD_PREFIX = "https://api.cloudinary.com"; + module.exports = { DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION, DEFAULT_POSTER_OPTIONS, @@ -137,5 +139,6 @@ module.exports = { PREDEFINED_VARS, LAYER_KEYWORD_PARAMS, TRANSFORMATION_PARAMS, - SIMPLE_PARAMS + SIMPLE_PARAMS, + UPLOAD_PREFIX }; diff --git a/lib/utils/index.js b/lib/utils/index.js index 453b756f..6049a006 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -45,6 +45,7 @@ const ensureOption = require('./ensureOption').defaults(config()); const entries = require('./entries'); const isRemoteUrl = require('./isRemoteUrl'); const getSDKVersionID = require('./encoding/sdkVersionID/getSDKVersionID'); +const UPLOAD_PREFIX = require('./consts'); exports = module.exports; const utils = module.exports; @@ -857,11 +858,15 @@ function unsigned_url_prefix( return prefix; } -function api_url(action = 'upload', options = {}) { - let cloudinary = ensureOption(options, "upload_prefix", "https://api.cloudinary.com"); +function base_api_url(path= [], options = {}) { + let cloudinary = ensureOption(options, "upload_prefix", UPLOAD_PREFIX); let cloud_name = ensureOption(options, "cloud_name"); + return [cloudinary, "v1_1", cloud_name].concat(path).join("/"); +} + +function api_url(action = 'upload', options = {}) { let resource_type = options.resource_type || "image"; - return [cloudinary, "v1_1", cloud_name, resource_type, action].join("/"); + return base_api_url([resource_type, action]); } function random_public_id() { @@ -972,6 +977,22 @@ function zip_download_url(tag, options = {}) { return exports.api_url("download_tag.zip", options) + "?" + hashToQuery(params); } +/** + * The returned url should allow downloading the backedup asset + * @param asset_id + * @param version_id + * @param options + * @returns {string } + */ +function download_backedup_asset(asset_id, version_id, options = {}) { + let params = exports.sign_request({ + timestamp: options.timestamp || exports.timestamp(), + asset_id: asset_id, + version_id: version_id + }, options); + return exports.base_api_url(['download_backup'], options) + "?" + hashToQuery(params); +} + /** * Returns a URL that when invokes creates an archive and returns it. * @param {object} options @@ -1396,6 +1417,9 @@ exports.only = pickOnlyExistingValues; // for backwards compatibility exports.pickOnlyExistingValues = pickOnlyExistingValues; exports.jsonArrayParam = jsonArrayParam; exports.download_folder = download_folder; +exports.base_api_url = base_api_url; +exports.download_backedup_asset = download_backedup_asset; + // was exported before, so kept for backwards compatibility exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS; exports.DEFAULT_VIDEO_SOURCE_TYPES = DEFAULT_VIDEO_SOURCE_TYPES; diff --git a/test/archivespec.js b/test/archivespec.js index 6c97f8e6..eae9200c 100644 --- a/test/archivespec.js +++ b/test/archivespec.js @@ -213,4 +213,12 @@ describe("archive", function () { expect(download_folder_url).to.contain('use_original_filename'); }); }); + + describe('download_backedup_asset', function(){ + it('should return url with asset and version id', function(){ + let download_backedup_asset_url = utils.download_backedup_asset('b71b23d9c89a81a254b88a91a9dad8cd', '0e493356d8a40b856c4863c026891a4e'); + expect(download_backedup_asset_url).to.contain('asset_id'); + expect(download_backedup_asset_url).to.contain('version_id'); + }); + }); }); From 9d969c0a666eb0cb062ac6b668acfa8f37e6e5ad Mon Sep 17 00:00:00 2001 From: Raya Straus Date: Mon, 29 Jun 2020 15:10:33 +0300 Subject: [PATCH 2/7] update constant location --- lib-es5/utils/index.js | 4 ++-- lib/utils/index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index 75f01cfe..b285d6b0 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -56,7 +56,6 @@ var ensureOption = require('./ensureOption').defaults(config()); var entries = require('./entries'); var isRemoteUrl = require('./isRemoteUrl'); var getSDKVersionID = require('./encoding/sdkVersionID/getSDKVersionID'); -var UPLOAD_PREFIX = require('./consts'); exports = module.exports; var utils = module.exports; @@ -95,7 +94,8 @@ var _require2 = require('./consts'), PREDEFINED_VARS = _require2.PREDEFINED_VARS, LAYER_KEYWORD_PARAMS = _require2.LAYER_KEYWORD_PARAMS, TRANSFORMATION_PARAMS = _require2.TRANSFORMATION_PARAMS, - SIMPLE_PARAMS = _require2.SIMPLE_PARAMS; + SIMPLE_PARAMS = _require2.SIMPLE_PARAMS, + UPLOAD_PREFIX = _require2.UPLOAD_PREFIX; function textStyle(layer) { var keywords = []; diff --git a/lib/utils/index.js b/lib/utils/index.js index 6049a006..7054a1c0 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -45,7 +45,6 @@ const ensureOption = require('./ensureOption').defaults(config()); const entries = require('./entries'); const isRemoteUrl = require('./isRemoteUrl'); const getSDKVersionID = require('./encoding/sdkVersionID/getSDKVersionID'); -const UPLOAD_PREFIX = require('./consts'); exports = module.exports; const utils = module.exports; @@ -84,7 +83,8 @@ const { PREDEFINED_VARS, LAYER_KEYWORD_PARAMS, TRANSFORMATION_PARAMS, - SIMPLE_PARAMS + SIMPLE_PARAMS, + UPLOAD_PREFIX } = require('./consts'); function textStyle(layer) { From af79c59bc0d703d91248aad477d6a2af1f84a801 Mon Sep 17 00:00:00 2001 From: Raya Straus Date: Mon, 29 Jun 2020 15:55:48 +0300 Subject: [PATCH 3/7] updated docstring --- lib-es5/utils/index.js | 4 +++- lib/utils/index.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index b285d6b0..76430348 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -1069,7 +1069,9 @@ function zip_download_url(tag) { } /** - * The returned url should allow downloading the backedup asset + * The returned url should allow downloading the backedup asset based on the + * version and asset id + * asset and version id are returned with resource(, { versions: true }) * @param asset_id * @param version_id * @param options diff --git a/lib/utils/index.js b/lib/utils/index.js index 7054a1c0..c3a73e20 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -978,7 +978,9 @@ function zip_download_url(tag, options = {}) { } /** - * The returned url should allow downloading the backedup asset + * The returned url should allow downloading the backedup asset based on the + * version and asset id + * asset and version id are returned with resource(, { versions: true }) * @param asset_id * @param version_id * @param options From 8f743e573cb8db3160b4ceddfe8d052eddc15b89 Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Tue, 30 Jun 2020 10:16:40 +0300 Subject: [PATCH 4/7] Add console to createTestCloud script --- tools/createTestCloud.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/createTestCloud.js b/tools/createTestCloud.js index 4b8b0e8c..0b2a820e 100644 --- a/tools/createTestCloud.js +++ b/tools/createTestCloud.js @@ -44,7 +44,8 @@ function setup() { console.log('Successfully created a temporary cloud'); console.log('Cloudname: ', cloudName); console.log('Sample image uploaded to: ', res.url); - }).catch(() => { + }).catch((e) => { + console.log('FATAL - Could not create sample asset', e); throw 'FATAL - Could not create sample asset'; }); }); From 407d0a4f60a39e2bab892efb40bceebf9bf604dc Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Tue, 30 Jun 2020 10:35:37 +0300 Subject: [PATCH 5/7] Add more consoles --- tools/createTestCloud.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/createTestCloud.js b/tools/createTestCloud.js index 0b2a820e..b52ec98f 100644 --- a/tools/createTestCloud.js +++ b/tools/createTestCloud.js @@ -46,6 +46,7 @@ function setup() { console.log('Sample image uploaded to: ', res.url); }).catch((e) => { console.log('FATAL - Could not create sample asset', e); + console.log(cloudData); throw 'FATAL - Could not create sample asset'; }); }); From 7d782b245956cfc93a6bb15d26189ad8ed4be330 Mon Sep 17 00:00:00 2001 From: Raya Straus Date: Tue, 30 Jun 2020 18:30:21 +0300 Subject: [PATCH 6/7] added options --- lib-es5/api_client/call_api.js | 2 +- lib-es5/utils/index.js | 2 +- lib/api_client/call_api.js | 2 +- lib/utils/index.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib-es5/api_client/call_api.js b/lib-es5/api_client/call_api.js index d813a6f9..01c0ba12 100644 --- a/lib-es5/api_client/call_api.js +++ b/lib-es5/api_client/call_api.js @@ -11,7 +11,7 @@ var ensurePresenceOf = utils.ensurePresenceOf; function call_api(method, uri, params, callback, options) { ensurePresenceOf({ method, uri }); - var api_url = utils.base_api_url(uri); + var api_url = utils.base_api_url(uri, options); var auth = { key: ensureOption(options, "api_key"), secret: ensureOption(options, "api_secret") diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index 76430348..3366a75d 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -931,7 +931,7 @@ function api_url() { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var resource_type = options.resource_type || "image"; - return base_api_url([resource_type, action]); + return base_api_url([resource_type, action], options); } function random_public_id() { diff --git a/lib/api_client/call_api.js b/lib/api_client/call_api.js index 249cab99..99baac07 100644 --- a/lib/api_client/call_api.js +++ b/lib/api_client/call_api.js @@ -8,7 +8,7 @@ const { ensurePresenceOf } = utils; function call_api(method, uri, params, callback, options) { ensurePresenceOf({ method, uri }); - const api_url = utils.base_api_url(uri); + const api_url = utils.base_api_url(uri, options); const auth = { key: ensureOption(options, "api_key"), secret: ensureOption(options, "api_secret") diff --git a/lib/utils/index.js b/lib/utils/index.js index c3a73e20..b41190ba 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -866,7 +866,7 @@ function base_api_url(path= [], options = {}) { function api_url(action = 'upload', options = {}) { let resource_type = options.resource_type || "image"; - return base_api_url([resource_type, action]); + return base_api_url([resource_type, action], options); } function random_public_id() { From dccc726167d15372cb682029ccc87a2c7074971a Mon Sep 17 00:00:00 2001 From: Raya Straus Date: Tue, 30 Jun 2020 18:36:35 +0300 Subject: [PATCH 7/7] removed script changes --- tools/createTestCloud.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/createTestCloud.js b/tools/createTestCloud.js index b52ec98f..4b8b0e8c 100644 --- a/tools/createTestCloud.js +++ b/tools/createTestCloud.js @@ -44,9 +44,7 @@ function setup() { console.log('Successfully created a temporary cloud'); console.log('Cloudname: ', cloudName); console.log('Sample image uploaded to: ', res.url); - }).catch((e) => { - console.log('FATAL - Could not create sample asset', e); - console.log(cloudData); + }).catch(() => { throw 'FATAL - Could not create sample asset'; }); });