Skip to content

Commit 80fa3a7

Browse files
authored
fix(base64): Fix b64 url safe creation in filelink (#253)
1 parent 03f8e70 commit 80fa3a7

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/lib/filelink.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,14 +653,14 @@ export class Filelink {
653653

654654
if (this.b64) {
655655
if (this.transforms.length > 0) {
656-
transformsString = `b64/${b64(JSON.stringify(this.transforms))}`;
656+
transformsString = `b64/${b64(JSON.stringify(this.transforms), true)}`;
657657
}
658658

659659
if (Array.isArray(source)) {
660660
source = this.arrayToString(source);
661661
}
662662

663-
source = `b64://${b64(source)}`;
663+
source = `b64://${b64(source, true)}`;
664664
} else {
665665
if (Array.isArray(source)) {
666666
source = this.arrayToString(source);

src/lib/utils/index.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ describe('utils:index', () => {
107107
it('should return correct b65 value', () => {
108108
expect(b64('testtext')).toEqual('dGVzdHRleHQ=');
109109
});
110+
111+
it('should escape chars to make b64 url safe string - char "/"', () => {
112+
expect(b64('*0eijATh#"I$PR)s<uTa}{t>E"LC:L', true)).toEqual('KjBlaWpBVGgjIkkkUFIpczx1VGF9e3Q-RSJMQzpM');
113+
});
114+
115+
it('should escape chars to make b64 url safe string - char ""', () => {
116+
expect(b64('W{wpB@ckYD0O@&?!||9PS)7^+F*H8N', true)).toEqual('V3t3cEJAY2tZRDBPQCY_IXx8OVBTKTdeK0YqSDhO');
117+
});
110118
});
111119

112120
describe('sanitizeName', () => {

src/lib/utils/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,20 @@ export const getMimetype = (file: Uint8Array | Buffer): string => {
164164
* return based string
165165
* @param data
166166
*/
167-
export const b64 = (data: string): string => {
167+
export const b64 = (data: string, safeUrl: boolean = false): string => {
168+
let b64;
169+
168170
if (isNode()) {
169-
return Buffer.from(data).toString('base64');
171+
b64 = Buffer.from(data).toString('base64');
172+
} else {
173+
b64 = btoa(data);
174+
}
175+
176+
if (safeUrl) {
177+
return b64.replace(/\//g, '_').replace(/\+/g, '-');
170178
}
171179

172-
return btoa(data);
180+
return b64;
173181
};
174182

175183
/**

0 commit comments

Comments
 (0)