Skip to content

Commit dbc65b7

Browse files
committed
fix(getfile): fix get file for b64 encoded string
1 parent 30f0b27 commit dbc65b7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/lib/api/upload/file_tools.node.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Pr
100100

101101
if (isFileBase(input)) {
102102
const matches = input.match(base64Regexp);
103-
input = Buffer.from(matches[2], 'base64');
103+
console.log(matches, input);
104+
if (matches && matches.length === 2) {
105+
input = Buffer.from(matches[2], 'base64');
106+
} else {
107+
input = Buffer.from(input, 'base64');
108+
}
104109
}
105110

106111
if (isFileBuffer(input)) {

src/lib/api/upload/file_tools.spec.node.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jest.mock('fs');
2222

2323
const mockedTestFile = Buffer.from('text text');
2424
const base64Svg = 'PHN2ZyBoZWlnaHQ9IjEwMCIgd2lkdGg9IjEwMCI+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNDAiIHN0cm9rZT0iYmxhY2siIHN0cm9rZS13aWR0aD0iMyIgZmlsbD0icmVkIiAvPgogIFNvcnJ5LCB5b3VyIGJyb3dzZXIgZG9lcyBub3Qgc3VwcG9ydCBpbmxpbmUgU1ZHLiAgCjwvc3ZnPiA=';
25+
const base64SvgWithHeader = 'data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjEwMCIgd2lkdGg9IjEwMCI+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNDAiIHN0cm9rZT0iYmxhY2siIHN0cm9rZS13aWR0aD0iMyIgZmlsbD0icmVkIiAvPgogIFNvcnJ5LCB5b3VyIGJyb3dzZXIgZG9lcyBub3Qgc3VwcG9ydCBpbmxpbmUgU1ZHLiAgCjwvc3ZnPiA=';
2526

2627
describe('Api/Upload/FileTools', () => {
2728
describe('getFileNode', () => {
@@ -75,6 +76,15 @@ describe('Api/Upload/FileTools', () => {
7576
expect(file.mimetype).toEqual('image/svg+xml');
7677
});
7778

79+
it('Should handle base64 encoded string with Header', async () => {
80+
const file = await getFile({
81+
name: 'test.svg',
82+
file: base64SvgWithHeader,
83+
});
84+
85+
expect(file.mimetype).toEqual('image/svg+xml');
86+
});
87+
7888
it('Should detect text/plain mimetype', async () => {
7989
const file = await getFile({
8090
name: 'test.undefined',

0 commit comments

Comments
 (0)