@@ -12,39 +12,29 @@ exports = module.exports
12
12
const CHUNK_SIZE = 262144
13
13
14
14
// Use a layout + chunkers to convert a directory (or file) to the layout format
15
- exports . import = ( dagService , options , callback ) => {
15
+ exports . import = ( target , dagService , options , callback ) => {
16
16
if ( typeof options === 'function' ) { callback = options ; options = { } }
17
- if ( ! dagService ) { return callback ( new Error ( 'no dag service provided' ) ) }
17
+
18
+ if ( ! target ) { return callback ( new Error ( 'must specify target' ) ) }
19
+ if ( ! dagService ) { return callback ( new Error ( 'must specify dag service' ) ) }
18
20
19
21
// options.recursive : follow dirs
20
22
// options.chunkers : obj with chunkers to each type of data, { default: dumb-chunker }
21
- // options.path : import a file hierarchy from a path
22
- // options.stream : import a stream
23
- // options.buffer : import a buffer
24
23
25
- // TODO: make first param be 'target' and check type to decide how to import
26
- // path
27
- // stream
28
- // buffer
24
+ options = options || { }
29
25
30
- if ( options . buffer ) {
31
- if ( ! Buffer . isBuffer ( options . buffer ) ) {
32
- return callback ( new Error ( 'buffer importer must take a buffer' ) )
33
- }
34
- bufferImporter ( options . buffer , callback )
35
- } else if ( options . stream ) {
36
- if ( ! ( typeof options . stream . on === 'function' ) ) {
37
- return callback ( new Error ( 'stream importer must take a readable stream' ) )
38
- }
26
+ if ( Buffer . isBuffer ( target ) ) {
27
+ bufferImporter ( target , callback )
28
+ } else if ( typeof target . on === 'function' ) {
39
29
// TODO Create Stream Importer
40
30
// streamImporter(options.stream, callback)
41
31
return callback ( new Error ( 'stream importer has not been built yet' ) )
42
- } else if ( options . path ) {
43
- const stats = fs . statSync ( options . path )
32
+ } else if ( typeof target === 'string' ) {
33
+ const stats = fs . statSync ( target )
44
34
if ( stats . isFile ( ) ) {
45
- fileImporter ( options . path , callback )
35
+ fileImporter ( target , callback )
46
36
} else if ( stats . isDirectory ( ) && options . recursive ) {
47
- dirImporter ( options . path , callback )
37
+ dirImporter ( target , callback )
48
38
} else {
49
39
return callback ( new Error ( 'recursive must be true to add a directory' ) )
50
40
}
0 commit comments