Skip to content

Commit f36762a

Browse files
committed
storage: add promptSaveAs option to getSignedUrl
1 parent 91ae473 commit f36762a

2 files changed

Lines changed: 67 additions & 16 deletions

File tree

lib/storage/file.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,13 @@ File.prototype.getSignedPolicy = function(options, callback) {
10191019
* link will expire.
10201020
* @param {string=} options.extensionHeaders - If these headers are used, the
10211021
* server will check to make sure that the client provides matching values.
1022+
* @param {string=} options.promptSaveAs - If you provide this value the SaveAs
1023+
* prompt will be displayed when accessing the signed url and the file name
1024+
* will be set to the provided value. If options.responseDisposition is set
1025+
* this option is ignored.
10221026
* @param {string=} options.responseDisposition - If you provide this value,
1023-
* the response-content-disposition parameter of the signed url is set
1024-
* accordingly.
1027+
* the response-content-disposition parameter (http://goo.gl/yMWxQV) of the
1028+
* signed url is set accordingly.
10251029
* @param {string=} options.responseType - If you provide this value, the
10261030
* response-content-type parameter of the signed url is set accordingly.
10271031
* @param {function=} callback - The callback function.
@@ -1030,7 +1034,7 @@ File.prototype.getSignedPolicy = function(options, callback) {
10301034
* file.getSignedUrl({
10311035
* action: 'read',
10321036
* expires: Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14), // 2 weeks.
1033-
* responseDisposition: 'attachment; filename="filename.ext"'
1037+
* promptSaveAs: 'filename.ext'
10341038
* }, function(err, url) {});
10351039
*/
10361040
File.prototype.getSignedUrl = function(options, callback) {
@@ -1074,7 +1078,18 @@ File.prototype.getSignedUrl = function(options, callback) {
10741078
}
10751079

10761080
var responseContentDisposition = '';
1081+
if (util.is(options.promptSaveAs, 'string')) {
1082+
responseContentDisposition =
1083+
'&response-content-disposition=attachment; filename="' +
1084+
encodeURIComponent(options.promptSaveAs) + '"';
1085+
}
10771086
if (util.is(options.responseDisposition, 'string')) {
1087+
if (responseContentDisposition !== '') {
1088+
console.warn([
1089+
'getSignedUrl: the promptSaveAs option is ignored',
1090+
'when responseDisposition is provided.'
1091+
].join(' '));
1092+
}
10781093
responseContentDisposition =
10791094
'&response-content-disposition=' +
10801095
encodeURIComponent(options.responseDisposition);

test/storage/file.js

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,27 +1350,63 @@ describe('File', function() {
13501350
});
13511351
});
13521352

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

1365-
it('should add response content type parameter', function(done) {
1366-
var type = 'application/json';
1367-
directoryFile.getSignedUrl({
1368-
action: 'read',
1369-
expires: Math.round(Date.now() / 1000) + 5,
1370-
responseType: type
1371-
}, function(err, signedUrl) {
1372-
assert(signedUrl.indexOf(encodeURIComponent(type)) > -1);
1373-
done();
1365+
describe('promptSaveAs', function() {
1366+
it('should add response-content-disposition', function(done) {
1367+
var disposition = 'attachment; filename="fname.ext"';
1368+
directoryFile.getSignedUrl({
1369+
action: 'read',
1370+
expires: Math.round(Date.now() / 1000) + 5,
1371+
promptSaveAs: 'fname.ext'
1372+
}, function(err, signedUrl) {
1373+
assert(signedUrl.indexOf(disposition) > -1);
1374+
done();
1375+
});
1376+
});
1377+
});
1378+
1379+
describe('responseDisposition', function() {
1380+
it('should add response-content-disposition', function(done) {
1381+
var disposition = 'attachment; filename="fname.ext"';
1382+
directoryFile.getSignedUrl({
1383+
action: 'read',
1384+
expires: Math.round(Date.now() / 1000) + 5,
1385+
responseDisposition: disposition
1386+
}, function(err, signedUrl) {
1387+
assert(signedUrl.indexOf(encodeURIComponent(disposition)) > -1);
1388+
done();
1389+
});
1390+
});
1391+
1392+
it('should warn and ignore promptSaveAs if set', function(done) {
1393+
var disposition = 'attachment; filename="fname.ext"';
1394+
var saveAs = 'fname2.ext';
1395+
var oldWarn = console.warn;
1396+
console.warn = function(message) {
1397+
assert(message.indexOf('promptSaveAs') > -1);
1398+
console.warn = oldWarn;
1399+
};
1400+
directoryFile.getSignedUrl({
1401+
action: 'read',
1402+
expires: Math.round(Date.now() / 1000) + 5,
1403+
promptSaveAs: saveAs,
1404+
responseDisposition: 'attachment; filename="fname.ext"'
1405+
}, function(err, signedUrl) {
1406+
assert(signedUrl.indexOf(encodeURIComponent(disposition)) > -1);
1407+
assert(signedUrl.indexOf(encodeURIComponent(saveAs)) === -1);
1408+
done();
1409+
});
13741410
});
13751411
});
13761412

0 commit comments

Comments
 (0)