import Debug from 'debug' ; import { Handle , HyperfileUrl } from 'hypermerge' ; import mime from 'mime-types' ; import path from 'path' ; import ContentTypes from '../../../ContentTypes' ; import FileContent from './FileContent' ; import FileInList from './FileInList' ; import * as Hyperfile from '../../../hyperfile' ; const log = Debug ( 'pushpin:filecontent' ) ; export interface FileDoc { title : string ; hyperfileUrl : HyperfileUrl ; extension : string ; mimeType : string ; } function createFromFile ( entry , handle , callback ) { const reader = new FileReader ( ) ; const { name = 'Unnamed File' } = entry ; reader . onload = ( ) => { const buffer = Buffer . from ( reader . result as ArrayBuffer ) ; const mimeType = mime . contentType ( entry . type ) || 'application/octet-stream' ; Hyperfile . writeBuffer ( buffer , mimeType ) . then ( ( hyperfileUrl ) => { handle . change ( ( doc ) => { const parsed = path . parse ( name ) ; doc . hyperfileUrl = hyperfileUrl ; doc . title = parsed . name ; doc . extension = parsed . ext . slice ( 1 ) ; doc . mimeType = mimeType ; } ) ; callback ( ) ; } ) . catch ( ( err ) => { log ( err ) ; } ) ; } ; reader . readAsArrayBuffer ( entry ) ; } ContentTypes . register ( { type : 'file' , name : 'File' , icon : 'file-o' , unlisted : true , contexts : { workspace : FileContent , board : FileContent , list : FileInList } , createFromFile } ) ;