Skip to content

Commit

Permalink
fix!: allow leading slashes in file name (#820)
Browse files Browse the repository at this point in the history
* fix!: allow leading slashes in filename

Match expected behavior for file name.  Leading slashes are maintained without options.  This is a breaking change.
  • Loading branch information
brianzinn authored and stephenplusplus committed Oct 16, 2019
1 parent 96a7fc2 commit 92e115d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/file.ts
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(/^\/+/, '');

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

let generation: number;
Expand Down
21 changes: 16 additions & 5 deletions test/file.ts
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

0 comments on commit 92e115d

Please sign in to comment.