Skip to content

storage: use standard URL instead of calling getMetadata - fixes #524 #525

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

Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 5 additions & 11 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,12 @@ File.prototype.createReadStream = function(options) {
var crc32c = validation === 'crc32c' || validation === 'all';
var md5 = validation === 'md5' || validation === 'all';

if (this.metadata.mediaLink) {
createAuthorizedReq(this.metadata.mediaLink);
} else {
this.getMetadata(function(err, metadata, resp) {
if (err) {
done(err, resp);
return;
}
var remoteFilePath = util.format('https://{b}.storage.googleapis.com/{o}', {
b: this.bucket.name,
o: encodeURIComponent(this.name)
});

createAuthorizedReq(metadata.mediaLink);
});
}
createAuthorizedReq(remoteFilePath);

return throughStream;

Expand Down
47 changes: 7 additions & 40 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,50 +274,25 @@ describe('File', function() {
});

describe('createReadStream', function() {
var metadata = { mediaLink: 'filelink' };

it('should confirm file exists before reading', function(done) {
file.getMetadata = function() {
done();
};
file.createReadStream();
});

it('should emit error if stat returns error', function(done) {
var error = new Error('Error.');
file.getMetadata = function(callback) {
setImmediate(function() {
callback(error);
});
};
file.createReadStream()
.once('error', function(err) {
assert.equal(err, error);
done();
});
});

it('should create an authorized request', function(done) {
var expectedPath = util.format('https://{b}.storage.googleapis.com/{o}', {
b: file.bucket.name,
o: encodeURIComponent(file.name)
});

file.bucket.storage.makeAuthorizedRequest_ = function(opts) {
assert.equal(opts.uri, metadata.mediaLink);
assert.equal(opts.uri, expectedPath);
done();
};

file.getMetadata = function(callback) {
callback(null, metadata);
};

file.createReadStream();
});

it('should emit an error from authorizing', function(done) {
var error = new Error('Error.');
file.bucket.storage.makeAuthorizedRequest_ = function(opts, callback) {
(callback.onAuthorized || callback)(error);
};
file.getMetadata = function(callback) {
setImmediate(function() {
callback(null, metadata);
(callback.onAuthorized || callback)(error);
});
};
file.createReadStream()
Expand Down Expand Up @@ -346,10 +321,6 @@ describe('File', function() {
};
nodeutil.inherits(request_Override, stream.Readable);

file.getMetadata = function(callback) {
callback(null, metadata);
};

file.bucket.storage.makeAuthorizedRequest_ = function(opts, callback) {
(callback.onAuthorized || callback)(null, fakeRequest);
};
Expand Down Expand Up @@ -480,7 +451,6 @@ describe('File', function() {
return duplexify();
};

file.metadata = metadata;
file.createReadStream({ start: startOffset });
});

Expand All @@ -495,7 +465,6 @@ describe('File', function() {
return duplexify();
};

file.metadata = metadata;
file.createReadStream({ end: endOffset });
});

Expand All @@ -512,7 +481,6 @@ describe('File', function() {
return duplexify();
};

file.metadata = metadata;
file.createReadStream({ start: startOffset, end: endOffset });
});

Expand All @@ -529,7 +497,6 @@ describe('File', function() {
return duplexify();
};

file.metadata = metadata;
file.createReadStream({ start: startOffset, end: endOffset });
});
});
Expand Down