license import { BlobReader , BlobWriter , TextWriter , ZipReader } from '@zip.js/zip.js' ; import initZipSettings from '../utils/init-zip-settings' ; import { HtmlzContent } from './types' ; initZipSettings ( ) ; export default async function extract ( blob ) { const reader = new ZipReader ( new BlobReader ( blob ) ) ; const entries = await reader . getEntries ( ) ; const result = { 'index.html' : '' , 'metadata.opf' : '' , 'style.css' : '' , } ; if ( entries . length ) { await Promise . all ( entries . map ( async ( entry ) => { if ( entry . getData && ! entry . directory ) { let value ; switch ( entry . filename ) { case 'index.html' : case 'metadata.opf' : case 'style.css' : value = await entry . getData ( new TextWriter ( ) ) ; break ; default : { value = await entry . getData ( new BlobWriter ( getMimeTypeFromName ( entry . filename ) ) ) ; } } result [ entry . filename ] = value ; } } ) ) ; } await reader . close ( ) ; return result ; } function getMimeTypeFromName ( filename ) { const regexResult = /.*\.([^.]+)$/ . exec ( filename ) ; if ( regexResult ) { switch ( regexResult [ 1 ] . toLowerCase ( ) ) { case 'gif' : return 'image/gif' ; case 'png' : return 'image/png' ; case 'jpg' : case 'jpeg' : case 'jfif' : case 'jfi' : return 'image/jpeg' ; case 'bmp' : return 'image/bmp' ; case 'webp' : return 'image/webp' ; } } return undefined ; }