Skip to content

Commit

Permalink
fix(getfile): fix get file for b64 encoded string
Browse files Browse the repository at this point in the history
  • Loading branch information
pcholuj committed Mar 31, 2021
1 parent 30f0b27 commit dbc65b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/api/upload/file_tools.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Pr

if (isFileBase(input)) {
const matches = input.match(base64Regexp);
input = Buffer.from(matches[2], 'base64');
console.log(matches, input);
if (matches && matches.length === 2) {
input = Buffer.from(matches[2], 'base64');
} else {
input = Buffer.from(input, 'base64');
}
}

if (isFileBuffer(input)) {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/api/upload/file_tools.spec.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jest.mock('fs');

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

describe('Api/Upload/FileTools', () => {
describe('getFileNode', () => {
Expand Down Expand Up @@ -75,6 +76,15 @@ describe('Api/Upload/FileTools', () => {
expect(file.mimetype).toEqual('image/svg+xml');
});

it('Should handle base64 encoded string with Header', async () => {
const file = await getFile({
name: 'test.svg',
file: base64SvgWithHeader,
});

expect(file.mimetype).toEqual('image/svg+xml');
});

it('Should detect text/plain mimetype', async () => {
const file = await getFile({
name: 'test.undefined',
Expand Down

0 comments on commit dbc65b7

Please sign in to comment.