Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 5522b35

Browse files
authored
fix: encode name portion when calling publicUrl function (#1828)
* fix: encode name portion when calling publicUrl function * linter fixes
1 parent a10b91e commit 5522b35

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/file.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3320,7 +3320,9 @@ class File extends ServiceObject<File> {
33203320
* ```
33213321
*/
33223322
publicUrl(): string {
3323-
return `${this.storage.apiEndpoint}/${this.bucket.name}/${this.name}`;
3323+
return `${this.storage.apiEndpoint}/${
3324+
this.bucket.name
3325+
}/${encodeURIComponent(this.name)}`;
33243326
}
33253327

33263328
move(

test/file.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,7 +3937,7 @@ describe('File', () => {
39373937
const file = new File(BUCKET, NAME);
39383938
assert.strictEqual(
39393939
file.publicUrl(),
3940-
`https://storage.googleapis.com/bucket-name/${NAME}`
3940+
`https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}`
39413941
);
39423942
done();
39433943
});
@@ -3947,7 +3947,7 @@ describe('File', () => {
39473947
const file = new File(BUCKET, NAME);
39483948
assert.strictEqual(
39493949
file.publicUrl(),
3950-
`https://storage.googleapis.com/bucket-name/${NAME}`
3950+
`https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}`
39513951
);
39523952
done();
39533953
});
@@ -3957,7 +3957,7 @@ describe('File', () => {
39573957
const file = new File(BUCKET, NAME);
39583958
assert.strictEqual(
39593959
file.publicUrl(),
3960-
`https://storage.googleapis.com/bucket-name/${NAME}`
3960+
`https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}`
39613961
);
39623962
done();
39633963
});
@@ -3967,7 +3967,17 @@ describe('File', () => {
39673967
const file = new File(BUCKET, NAME);
39683968
assert.strictEqual(
39693969
file.publicUrl(),
3970-
`https://storage.googleapis.com/bucket-name/${NAME}`
3970+
`https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}`
3971+
);
3972+
done();
3973+
});
3974+
3975+
it('with an ampersand in the name', done => {
3976+
const NAME = '&foo';
3977+
const file = new File(BUCKET, NAME);
3978+
assert.strictEqual(
3979+
file.publicUrl(),
3980+
`https://storage.googleapis.com/bucket-name/${encodeURIComponent(NAME)}`
39713981
);
39723982
done();
39733983
});

0 commit comments

Comments
 (0)