Skip to content

Commit

Permalink
storage: add promptSaveAs option to getSignedUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed May 11, 2015
1 parent 91ae473 commit f36762a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
21 changes: 18 additions & 3 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,9 +1019,13 @@ File.prototype.getSignedPolicy = function(options, callback) {
* link will expire.
* @param {string=} options.extensionHeaders - If these headers are used, the
* server will check to make sure that the client provides matching values.
* @param {string=} options.promptSaveAs - If you provide this value the SaveAs
* prompt will be displayed when accessing the signed url and the file name
* will be set to the provided value. If options.responseDisposition is set
* this option is ignored.
* @param {string=} options.responseDisposition - If you provide this value,
* the response-content-disposition parameter of the signed url is set
* accordingly.
* the response-content-disposition parameter (http://goo.gl/yMWxQV) of the
* signed url is set accordingly.
* @param {string=} options.responseType - If you provide this value, the
* response-content-type parameter of the signed url is set accordingly.
* @param {function=} callback - The callback function.
Expand All @@ -1030,7 +1034,7 @@ File.prototype.getSignedPolicy = function(options, callback) {
* file.getSignedUrl({
* action: 'read',
* expires: Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14), // 2 weeks.
* responseDisposition: 'attachment; filename="filename.ext"'
* promptSaveAs: 'filename.ext'
* }, function(err, url) {});
*/
File.prototype.getSignedUrl = function(options, callback) {
Expand Down Expand Up @@ -1074,7 +1078,18 @@ File.prototype.getSignedUrl = function(options, callback) {
}

var responseContentDisposition = '';
if (util.is(options.promptSaveAs, 'string')) {
responseContentDisposition =
'&response-content-disposition=attachment; filename="' +
encodeURIComponent(options.promptSaveAs) + '"';
}
if (util.is(options.responseDisposition, 'string')) {
if (responseContentDisposition !== '') {
console.warn([
'getSignedUrl: the promptSaveAs option is ignored',
'when responseDisposition is provided.'
].join(' '));
}
responseContentDisposition =
'&response-content-disposition=' +
encodeURIComponent(options.responseDisposition);
Expand Down
62 changes: 49 additions & 13 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,27 +1350,63 @@ describe('File', function() {
});
});

it('should add response content disposition parameter', function(done) {
var disposition = 'attachment; filename="fname.ext"';
it('should add response-content-type parameter', function(done) {
var type = 'application/json';
directoryFile.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + 5,
responseDisposition: disposition
responseType: type
}, function(err, signedUrl) {
assert(signedUrl.indexOf(encodeURIComponent(disposition)) > -1);
assert(signedUrl.indexOf(encodeURIComponent(type)) > -1);
done();
});
});

it('should add response content type parameter', function(done) {
var type = 'application/json';
directoryFile.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + 5,
responseType: type
}, function(err, signedUrl) {
assert(signedUrl.indexOf(encodeURIComponent(type)) > -1);
done();
describe('promptSaveAs', function() {
it('should add response-content-disposition', function(done) {
var disposition = 'attachment; filename="fname.ext"';
directoryFile.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + 5,
promptSaveAs: 'fname.ext'
}, function(err, signedUrl) {
assert(signedUrl.indexOf(disposition) > -1);
done();
});
});
});

describe('responseDisposition', function() {
it('should add response-content-disposition', function(done) {
var disposition = 'attachment; filename="fname.ext"';
directoryFile.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + 5,
responseDisposition: disposition
}, function(err, signedUrl) {
assert(signedUrl.indexOf(encodeURIComponent(disposition)) > -1);
done();
});
});

it('should warn and ignore promptSaveAs if set', function(done) {
var disposition = 'attachment; filename="fname.ext"';
var saveAs = 'fname2.ext';
var oldWarn = console.warn;
console.warn = function(message) {
assert(message.indexOf('promptSaveAs') > -1);
console.warn = oldWarn;
};
directoryFile.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + 5,
promptSaveAs: saveAs,
responseDisposition: 'attachment; filename="fname.ext"'
}, function(err, signedUrl) {
assert(signedUrl.indexOf(encodeURIComponent(disposition)) > -1);
assert(signedUrl.indexOf(encodeURIComponent(saveAs)) === -1);
done();
});
});
});

Expand Down

0 comments on commit f36762a

Please sign in to comment.