mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-08 15:15:02 -04:00
feat(base64-file-converter): infer mime type from base64 signature
This commit is contained in:
parent
f7f6f5d77f
commit
b82787a470
2 changed files with 68 additions and 9 deletions
32
src/composable/downloadBase64.test.ts
Normal file
32
src/composable/downloadBase64.test.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { getMimeTypeFromBase64 } from './downloadBase64';
|
||||
|
||||
describe('downloadBase64', () => {
|
||||
describe('getMimeTypeFromBase64', () => {
|
||||
it('when the base64 string has a data URI, it returns the mime type', () => {
|
||||
expect(getMimeTypeFromBase64({ base64String: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' })).to.deep.equal({ mimeType: 'image/png' });
|
||||
expect(getMimeTypeFromBase64({ base64String: 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' })).to.deep.equal({ mimeType: 'image/jpg' });
|
||||
});
|
||||
|
||||
it('when the base64 string has no data URI, it try to infer the mime type from the signature', () => {
|
||||
// https://en.wikipedia.org/wiki/List_of_file_signatures
|
||||
|
||||
// PNG
|
||||
expect(getMimeTypeFromBase64({ base64String: 'iVBORw0KGgoAAAANSUhEUgAAAAUA' })).to.deep.equal({ mimeType: 'image/png' });
|
||||
|
||||
// GIF
|
||||
expect(getMimeTypeFromBase64({ base64String: 'R0lGODdh' })).to.deep.equal({ mimeType: 'image/gif' });
|
||||
expect(getMimeTypeFromBase64({ base64String: 'R0lGODlh' })).to.deep.equal({ mimeType: 'image/gif' });
|
||||
|
||||
// JPG
|
||||
expect(getMimeTypeFromBase64({ base64String: '/9j/' })).to.deep.equal({ mimeType: 'image/jpg' });
|
||||
|
||||
// PDF
|
||||
expect(getMimeTypeFromBase64({ base64String: 'JVBERi0' })).to.deep.equal({ mimeType: 'application/pdf' });
|
||||
});
|
||||
|
||||
it('when the base64 string has no data URI and no signature, it returns an undefined mimeType', () => {
|
||||
expect(getMimeTypeFromBase64({ base64String: 'JVBERi' })).to.deep.equal({ mimeType: undefined });
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue