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 1 commit
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
26 changes: 24 additions & 2 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,18 @@ 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.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 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.
* responseDisposition: 'attachment; filename="filename.ext"'

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.

This comment was marked as spam.

* }, function(err, url) {});
*/
File.prototype.getSignedUrl = function(options, callback) {
Expand Down Expand Up @@ -1060,11 +1066,27 @@ 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.responseDisposition, 'string')) {
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
24 changes: 24 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,30 @@ describe('File', function() {
});
});

it('should add response content disposition parameter', 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 add response content type parameter', function(done) {
var type = 'application/json';
directoryFile.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + 5,
responseType: type

This comment was marked as spam.

}, function(err, signedUrl) {
assert(signedUrl.indexOf(encodeURIComponent(type)) > -1);
done();
});
});

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

Expand Down