import * as franc from "franc-min" ; import * as iconv from "iconv-lite" ; import { epubFromBuffer , IEpub } from "~/vendor/epub-parser" ; import { isUtf8 } from "~/vendor/is-utf8" ; import jschardet = require ( "jschardet" ) ; import { fileSize , isText , readFile } from "~/util/FileUtils" ; export const isEpub = ( maybeEpub ) : boolean => maybeEpub && maybeEpub . hasOwnProperty ( "markedContent" ) ; export const detectLanguage = ( input ) : string | undefined => { const lang = franc ( input ) ; return lang !== "und" ? lang : undefined ; } ; export const getEpubOrPlainContent = async ( path ) : Promise < IEpub | string > => { let data ; try { data = await readFile ( path ) ; const epub = await epubFromBuffer ( data ! ) ; return epub || "" ; } catch ( e ) { const isDataText = data && ( await isText ( data , await fileSize ( path ) ) ) ; if ( data && isDataText ) { if ( ! isUtf8 ( data ) ) { return iconv . decode ( data , jschardet . detect ( data ) . encoding ) . toString ( ) ; } return data . toString ( ) ; } else { console . error ( "Error parsing file: " , e ) ; } } return "" ; } ; export const takeSample = ( rawTextContent , length ) => { return rawTextContent . substr ( 0 , length ) ; } ;