diff --git a/src/file.ts b/src/file.ts index c49484d8f..064847c92 100644 --- a/src/file.ts +++ b/src/file.ts @@ -3320,7 +3320,9 @@ class File extends ServiceObject { * ``` */ publicUrl(): string { - return `${this.storage.apiEndpoint}/${this.bucket.name}/${this.name}`; + return `${this.storage.apiEndpoint}/${ + this.bucket.name + }/${encodeURIComponent(this.name)}`; } move( diff --git a/test/file.ts b/test/file.ts index ece8784df..2cdd6f7ac 100644 --- a/test/file.ts +++ b/test/file.ts @@ -3937,7 +3937,7 @@ describe('File', () => { const file = new File(BUCKET, NAME); assert.strictEqual( file.publicUrl(), - `https://storage.googleapis.com/bucket-name/${NAME}` + `https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}` ); done(); }); @@ -3947,7 +3947,7 @@ describe('File', () => { const file = new File(BUCKET, NAME); assert.strictEqual( file.publicUrl(), - `https://storage.googleapis.com/bucket-name/${NAME}` + `https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}` ); done(); }); @@ -3957,7 +3957,7 @@ describe('File', () => { const file = new File(BUCKET, NAME); assert.strictEqual( file.publicUrl(), - `https://storage.googleapis.com/bucket-name/${NAME}` + `https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}` ); done(); }); @@ -3967,7 +3967,17 @@ describe('File', () => { const file = new File(BUCKET, NAME); assert.strictEqual( file.publicUrl(), - `https://storage.googleapis.com/bucket-name/${NAME}` + `https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}` + ); + done(); + }); + + it('with an ampersand in the name', done => { + const NAME = '&foo'; + const file = new File(BUCKET, NAME); + assert.strictEqual( + file.publicUrl(), + `https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}` ); done(); });