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

storage: add response-content-* parameters to signed url #578

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
40 changes: 38 additions & 2 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,21 @@ 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 - The filename to prompt the user to
* save the file as when the signed url is accessed. This is ignored if
* options.responseDisposition is set.
* @param {string=} options.responseDisposition - The
* response-content-disposition parameter (http://goo.gl/yMWxQV) of the
* signed url.
* @param {string=} options.responseType - The response-content-type parameter
* of the signed url.
* @param {function=} callback - The callback function.
*
* @example
* file.getSignedUrl({
* action: 'read',
* expires: Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14) // 2 weeks.
* expires: Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14), // 2 weeks.
* promptSaveAs: 'filename.ext'
* }, function(err, url) {});
*/
File.prototype.getSignedUrl = function(options, callback) {
Expand Down Expand Up @@ -1060,11 +1069,38 @@ File.prototype.getSignedUrl = function(options, callback) {
].join('\n'));
var signature = sign.sign(credentials.private_key, 'base64');

var responseContentType = '';
if (util.is(options.responseType, 'string')) {
responseContentType =
'&response-content-type=' +
encodeURIComponent(options.responseType);
}

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.'

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

].join(' '));
}
responseContentDisposition =
'&response-content-disposition=' +
encodeURIComponent(options.responseDisposition);
}

callback(null, [
'https://storage.googleapis.com' + options.resource,
'?GoogleAccessId=' + credentials.client_email,
'&Expires=' + options.expires,
'&Signature=' + encodeURIComponent(signature)
'&Signature=' + encodeURIComponent(signature),
responseContentType,
responseContentDisposition
].join(''));
});
};
Expand Down
61 changes: 61 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,67 @@ describe('File', function() {
});
});

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: disposition
}, function(err, signedUrl) {
assert(signedUrl.indexOf(encodeURIComponent(disposition)) > -1);
assert(signedUrl.indexOf(encodeURIComponent(saveAs)) === -1);
assert.equal(console.warn, oldWarn);
done();
});
});
});

describe('expires', function() {
var nowInSeconds = Math.floor(Date.now() / 1000);

Expand Down