2020-08-28 14:25:43 +03:00
|
|
|
export const readBlobAsText = (blob: Blob) => {
|
|
|
|
return new Promise<string>(resolve => {
|
|
|
|
const reader = new FileReader();
|
2020-08-31 00:50:25 +03:00
|
|
|
reader.addEventListener('loadend', (e) => {
|
2020-08-28 14:25:43 +03:00
|
|
|
// @ts-ignore
|
|
|
|
resolve(e.srcElement.result);
|
|
|
|
});
|
|
|
|
reader.readAsText(blob);
|
|
|
|
});
|
2020-10-29 19:11:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export function blobConstruct(blobParts: any, mimeType: string = ''): Blob {
|
|
|
|
let blob;
|
|
|
|
const safeMimeType = blobSafeMimeType(mimeType);
|
|
|
|
try {
|
|
|
|
blob = new Blob(blobParts, {type: safeMimeType});
|
|
|
|
} catch(e) {
|
|
|
|
// @ts-ignore
|
|
|
|
let bb = new BlobBuilder;
|
|
|
|
blobParts.forEach((blobPart: any) => {
|
|
|
|
bb.append(blobPart);
|
|
|
|
});
|
|
|
|
blob = bb.getBlob(safeMimeType);
|
|
|
|
}
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function blobSafeMimeType(mimeType: string) {
|
|
|
|
if([
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
'image/gif',
|
|
|
|
'image/webp',
|
|
|
|
'image/bmp',
|
|
|
|
'video/mp4',
|
|
|
|
'video/webm',
|
|
|
|
'video/quicktime',
|
|
|
|
'audio/ogg',
|
|
|
|
'audio/mpeg',
|
|
|
|
'audio/mp4',
|
|
|
|
'application/json'
|
|
|
|
].indexOf(mimeType) === -1) {
|
|
|
|
return 'application/octet-stream';
|
|
|
|
}
|
|
|
|
|
|
|
|
return mimeType;
|
|
|
|
}
|