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

fix!: allow leading slashes in file name #820

Merged
merged 10 commits into from
Oct 16, 2019
2 changes: 0 additions & 2 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,6 @@ class File extends ServiceObject<File> {
* const file = myBucket.file('my-file');
*/
constructor(bucket: Bucket, name: string, options: FileOptions = {}) {
name = name.replace(/^\/+/, '');
stephenplusplus marked this conversation as resolved.
Show resolved Hide resolved

const requestQueryObject: {generation?: number; userProject?: string} = {};

let generation: number;
Expand Down
21 changes: 16 additions & 5 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ describe('File', () => {
assert.strictEqual(file.storage, BUCKET.storage);
});

it('should strip a single leading slash', () => {
it('should not strip leading slashes', () => {
const file = new File(BUCKET, '/name');
assert.strictEqual(file.name, 'name');
assert.strictEqual(file.name, '/name');
});

it('should assign KMS key name', () => {
Expand Down Expand Up @@ -302,11 +302,11 @@ describe('File', () => {
});
});

it('should use stripped leading slash name in ServiceObject', () => {
it('should not strip leading slash name in ServiceObject', () => {
const file = new File(BUCKET, '/name');
const calledWith = file.calledWith_[0];

assert.strictEqual(calledWith.id, 'name');
assert.strictEqual(calledWith.id, encodeURIComponent('/name'));
});

it('should set a custom encryption key', done => {
Expand Down Expand Up @@ -614,13 +614,24 @@ describe('File', () => {
}

it('should allow a string', done => {
const newFileName = '/new-file-name.png';
const newFileName = 'new-file-name.png';
const newFile = new File(BUCKET, newFileName);
const expectedPath = `/rewriteTo/b/${file.bucket.name}/o/${newFile.name}`;
assertPathEquals(file, expectedPath, done);
file.copy(newFileName);
});

it('should allow a string with leading slash.', done => {
const newFileName = '/new-file-name.png';
const newFile = new File(BUCKET, newFileName);
// File uri encodes file name when calling this.request during copy
const expectedPath = `/rewriteTo/b/${
file.bucket.name
}/o/${encodeURIComponent(newFile.name)}`;
assertPathEquals(file, expectedPath, done);
file.copy(newFileName);
});

it('should allow a "gs://..." string', done => {
const newFileName = 'gs://other-bucket/new-file-name.png';
const expectedPath = `/rewriteTo/b/other-bucket/o/new-file-name.png`;
Expand Down