Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added download_folder method #404

Merged
merged 5 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib-es5/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,20 @@ function download_zip_url() {
}));
}

/**
* Creates and downloads a URL that when invokes creates an archive of a folder
*/
function download_folder(folder_path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

options.resource_type = options.resource_type || "all";
options.prefixes = folder_path;
var cloudinary_params = exports.sign_request(exports.archive_params(merge(options, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How this works? the signature include the mode='download' but the options sent to the api does not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its part of the cloudinary_params sent in the hashToQuery(cloudinary_params);

Its the same concept needed in download_archive_url-

function download_archive_url(options = {}) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still don't understand how this works

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? both the signature and the api call include the mode=download param.

The sign_request method returns the signed params with the mode param included (after signing).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What am I missing?
There are options which are sent to the API and when building the signature, we merge options with mode=download

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion - all params are sent on the query string and the options sent to the api_url are for general config and not params to the specific api call

mode: "download"
})), options);
return exports.api_url("generate_archive", options) + "?" + hashToQuery(cloudinary_params);
}

/**
* Render the key/value pair as an HTML tag attribute
* @private
Expand Down Expand Up @@ -1302,7 +1316,8 @@ function archive_params() {
timestamp: options.timestamp || exports.timestamp(),
transformations: utils.build_eager(options.transformations),
type: options.type,
use_original_filename: exports.as_safe_bool(options.use_original_filename)
use_original_filename: exports.as_safe_bool(options.use_original_filename),
folder_path: options.folder_path
};
}

Expand Down Expand Up @@ -1507,6 +1522,7 @@ exports.present = present;
exports.only = pickOnlyExistingValues; // for backwards compatibility
exports.pickOnlyExistingValues = pickOnlyExistingValues;
exports.jsonArrayParam = jsonArrayParam;
exports.download_folder = download_folder;
// was exported before, so kept for backwards compatibility
exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;
exports.DEFAULT_VIDEO_SOURCE_TYPES = DEFAULT_VIDEO_SOURCE_TYPES;
Expand Down
14 changes: 14 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,18 @@ function download_zip_url(options = {}) {
}));
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't we drop the need of lib-es5?
@patrick-tolosa

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not merged yet

* Creates and downloads a URL that when invokes creates an archive of a folder
*/
function download_folder(folder_path, options = {}) {
options.resource_type = options.resource_type || "all";
options.prefixes = folder_path;
strausr marked this conversation as resolved.
Show resolved Hide resolved
let cloudinary_params = exports.sign_request(exports.archive_params(merge(options, {
mode: "download",
})), options);
return exports.api_url("generate_archive", options) + "?" + hashToQuery(cloudinary_params);
}

/**
* Render the key/value pair as an HTML tag attribute
* @private
Expand Down Expand Up @@ -1198,6 +1210,7 @@ function archive_params(options = {}) {
transformations: utils.build_eager(options.transformations),
type: options.type,
use_original_filename: exports.as_safe_bool(options.use_original_filename),
folder_path: options.folder_path,
};
}

Expand Down Expand Up @@ -1382,6 +1395,7 @@ exports.present = present;
exports.only = pickOnlyExistingValues; // for backwards compatibility
exports.pickOnlyExistingValues = pickOnlyExistingValues;
exports.jsonArrayParam = jsonArrayParam;
exports.download_folder = download_folder;
// was exported before, so kept for backwards compatibility
exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;
exports.DEFAULT_VIDEO_SOURCE_TYPES = DEFAULT_VIDEO_SOURCE_TYPES;
Expand Down
26 changes: 26 additions & 0 deletions test/archivespec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,30 @@ describe("archive", function () {
});
});
});
describe('download_folder', function(){
it('should return url with resource_type image', function(){
let download_folder_url = utils.download_folder('samples/', {resource_type: 'image'});
expect(download_folder_url).to.contain('image');
});
asisayag2 marked this conversation as resolved.
Show resolved Hide resolved
it('should return valid url', function(){
let download_folder_url = utils.download_folder('folder/');
expect(download_folder_url).not.to.be.empty();
expect(download_folder_url).to.contain('generate_archive');
});

it('should flatten folder', function(){
let download_folder_url = utils.download_folder('folder/', {flatten_folders: true});
expect(download_folder_url).to.contain('flatten_folders');
});

it('should expire_at folder', function(){
let download_folder_url = utils.download_folder('folder/', {expires_at: Date.now() / 1000 + 60});
expect(download_folder_url).to.contain('expires_at');
});

it('should use original file_name of folder', function(){
let download_folder_url = utils.download_folder('folder/', {use_original_filename: true});
expect(download_folder_url).to.contain('use_original_filename');
});
});
});