Skip to content

Commit 2675add

Browse files
committed
feat(tabstractfileconvertor): allow to load files from string, url OR directly from memory
1 parent 253f474 commit 2675add

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

sources/core/converters/TAbstractFileConverter.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as globalBuffer from 'buffer'
1212
import fs from 'fs'
1313
import {
1414
isNull,
15+
isString,
1516
isUndefined
1617
} from 'itee-validators'
1718
import { Writable } from 'stream'
@@ -168,20 +169,58 @@ class TAbstractFileConverter {
168169

169170
const self = this
170171
const dataBloc = this._queue.shift()
171-
const data = dataBloc.file
172+
const file = dataBloc.file
172173
const parameters = dataBloc.parameters
173174
const onSuccess = dataBloc.onSuccess
174175
const onProgress = dataBloc.onProgress
175176
const onError = dataBloc.onError
176177

177-
self._dumpFileInMemoryAs(
178-
self._dumpType,
179-
data,
180-
parameters,
181-
_onDumpSuccess,
182-
_onProcessProgress,
183-
_onProcessError
184-
)
178+
if ( isString( file ) ) {
179+
180+
self._dumpFileInMemoryAs(
181+
self._dumpType,
182+
file,
183+
parameters,
184+
_onDumpSuccess,
185+
_onProcessProgress,
186+
_onProcessError
187+
)
188+
189+
} else {
190+
191+
const data = file.data
192+
193+
switch ( self._dumpType ) {
194+
195+
case TAbstractFileConverter.DumpType.ArrayBuffer: {
196+
197+
const bufferSize = data.length
198+
const arrayBuffer = new ArrayBuffer( bufferSize )
199+
const view = new Uint8Array( arrayBuffer )
200+
201+
for ( let i = 0 ; i < bufferSize ; ++i ) {
202+
view[ i ] = buffer[ i ]
203+
}
204+
205+
_onDumpSuccess( arrayBuffer )
206+
207+
}
208+
break
209+
210+
case TAbstractFileConverter.DumpType.JSON:
211+
_onDumpSuccess( JSON.parse( data.toString() ) )
212+
break
213+
214+
case TAbstractFileConverter.DumpType.String:
215+
_onDumpSuccess( data.toString() )
216+
break
217+
218+
default:
219+
throw new RangeError( `Invalid switch parameter: ${self._dumpType}` )
220+
221+
}
222+
223+
}
185224

186225
function _onDumpSuccess ( data ) {
187226

0 commit comments

Comments
 (0)