Skip to content

Commit

Permalink
feat(tabstractfileconvertor): allow to load files from string, url OR…
Browse files Browse the repository at this point in the history
… directly from memory
  • Loading branch information
Itee committed Aug 14, 2019
1 parent 253f474 commit 2675add
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions sources/core/converters/TAbstractFileConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as globalBuffer from 'buffer'
import fs from 'fs'
import {
isNull,
isString,
isUndefined
} from 'itee-validators'
import { Writable } from 'stream'
Expand Down Expand Up @@ -168,20 +169,58 @@ class TAbstractFileConverter {

const self = this
const dataBloc = this._queue.shift()
const data = dataBloc.file
const file = dataBloc.file
const parameters = dataBloc.parameters
const onSuccess = dataBloc.onSuccess
const onProgress = dataBloc.onProgress
const onError = dataBloc.onError

self._dumpFileInMemoryAs(
self._dumpType,
data,
parameters,
_onDumpSuccess,
_onProcessProgress,
_onProcessError
)
if ( isString( file ) ) {

self._dumpFileInMemoryAs(
self._dumpType,
file,
parameters,
_onDumpSuccess,
_onProcessProgress,
_onProcessError
)

} else {

const data = file.data

switch ( self._dumpType ) {

case TAbstractFileConverter.DumpType.ArrayBuffer: {

const bufferSize = data.length
const arrayBuffer = new ArrayBuffer( bufferSize )
const view = new Uint8Array( arrayBuffer )

for ( let i = 0 ; i < bufferSize ; ++i ) {
view[ i ] = buffer[ i ]
}

_onDumpSuccess( arrayBuffer )

}
break

case TAbstractFileConverter.DumpType.JSON:
_onDumpSuccess( JSON.parse( data.toString() ) )
break

case TAbstractFileConverter.DumpType.String:
_onDumpSuccess( data.toString() )
break

default:
throw new RangeError( `Invalid switch parameter: ${self._dumpType}` )

}

}

function _onDumpSuccess ( data ) {

Expand Down

0 comments on commit 2675add

Please sign in to comment.