From 92e115dca81604909fc34e983abcf47409d3f417 Mon Sep 17 00:00:00 2001 From: Brian Zinn Date: Wed, 16 Oct 2019 13:34:43 -0700 Subject: [PATCH] fix!: allow leading slashes in file name (#820) * fix!: allow leading slashes in filename Match expected behavior for file name. Leading slashes are maintained without options. This is a breaking change. --- src/file.ts | 2 -- test/file.ts | 21 ++++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/file.ts b/src/file.ts index f07a28f84..7d85ce9e1 100644 --- a/src/file.ts +++ b/src/file.ts @@ -499,8 +499,6 @@ class File extends ServiceObject { * 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; diff --git a/test/file.ts b/test/file.ts index 5764fb35b..94f91f7ad 100644 --- a/test/file.ts +++ b/test/file.ts @@ -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', () => { @@ -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 => { @@ -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`;