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 2 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
41 changes: 39 additions & 2 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,22 @@ 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,

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* 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

This comment was marked as spam.

* response-content-type parameter of the signed url is set accordingly.
* @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 +1070,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
60 changes: 60 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,66 @@ 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);
done();
});
});
});

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

Expand Down