This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from ipfs/files-api-flat
Working and passing
- Loading branch information
Showing
7 changed files
with
109 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,104 @@ | ||
'use strict' | ||
|
||
const File = require('vinyl') | ||
const vinylfs = require('vinyl-fs-browser') | ||
const vmps = require('vinyl-multipart-stream') | ||
const stream = require('stream') | ||
const Merge = require('merge-stream') | ||
const isNode = require('detect-node') | ||
const Multipart = require('multipart-stream') | ||
const flatmap = require('flatmap') | ||
|
||
exports = module.exports = getFilesStream | ||
function headers (file) { | ||
const name = file.path || '' | ||
const header = { | ||
'Content-Disposition': `file; filename="${name}"` | ||
} | ||
|
||
function getFilesStream (files, opts) { | ||
if (!files) return null | ||
if (file.dir) { | ||
header['Content-Type'] = 'application/x-directory' | ||
} else { | ||
header['Content-Type'] = 'application/octet-stream' | ||
} | ||
|
||
// merge all inputs into one stream | ||
const adder = new Merge() | ||
return header | ||
} | ||
|
||
// single stream for pushing directly | ||
const single = new stream.PassThrough({objectMode: true}) | ||
adder.add(single) | ||
function strip (name, base) { | ||
const smallBase = base | ||
.split('/') | ||
.slice(0, -1) | ||
.join('/') + '/' | ||
return name.replace(smallBase, '') | ||
} | ||
|
||
for (let i = 0; i < files.length; i++) { | ||
const file = files[i] | ||
function loadPaths (opts, file) { | ||
const path = require('path') | ||
const fs = require('fs') | ||
const glob = require('glob') | ||
|
||
if (typeof (file) === 'string') { | ||
const srcOpts = { | ||
buffer: false, | ||
stripBOM: false, | ||
followSymlinks: opts.followSymlinks != null ? opts.followSymlinks : true | ||
} | ||
const followSymlinks = opts.followSymlinks != null ? opts.followSymlinks : true | ||
|
||
// add the file or dir itself | ||
adder.add(vinylfs.src(file, srcOpts)) | ||
file = path.resolve(file) | ||
const stats = fs.statSync(file) | ||
|
||
// if recursive, glob the contents | ||
if (opts.recursive) { | ||
adder.add(vinylfs.src(file + '/**/*', srcOpts)) | ||
} | ||
} else { | ||
// try to create a single vinyl file, and push it. | ||
// throws if cannot use the file. | ||
single.push(vinylFile(file)) | ||
} | ||
if (stats.isDirectory() && !opts.recursive) { | ||
throw new Error('Can only add directories using --recursive') | ||
} | ||
|
||
single.end() | ||
return adder.pipe(vmps.flat()) | ||
} | ||
if (stats.isDirectory() && opts.recursive) { | ||
const mg = new glob.sync.GlobSync(`${file}/**/*`, { | ||
follow: followSymlinks | ||
}) | ||
|
||
// vinylFile tries to cast a file object to a vinyl file. | ||
// it's agressive. If it _cannot_ be converted to a file, | ||
// it returns null. | ||
function vinylFile (file) { | ||
if (file instanceof File) { | ||
return file // it's a vinyl file. | ||
return mg.found.map(name => { | ||
if (mg.cache[name] === 'FILE') { | ||
return { | ||
path: strip(name, file), | ||
dir: false, | ||
content: fs.createReadStream(name) | ||
} | ||
} else { | ||
return { | ||
path: strip(name, file), | ||
dir: true | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// let's try to make a vinyl file? | ||
const f = {cwd: '/', base: '/', path: ''} | ||
if (file.contents && file.path) { | ||
// set the cwd + base, if there. | ||
f.path = file.path | ||
f.cwd = file.cwd || f.cwd | ||
f.base = file.base || f.base | ||
f.contents = file.contents | ||
} else { | ||
// ok maybe we just have contents? | ||
f.contents = file | ||
return { | ||
path: file, | ||
content: fs.createReadStream(file) | ||
} | ||
|
||
// ensure the contents are safe to pass. | ||
// throws if vinyl cannot use the contents | ||
f.contents = vinylContentsSafe(f.contents) | ||
return new File(f) | ||
} | ||
|
||
function vinylContentsSafe (c) { | ||
if (Buffer.isBuffer(c)) return c | ||
if (typeof (c) === 'string') return c | ||
if (c instanceof stream.Stream) return c | ||
if (typeof (c.pipe) === 'function') { | ||
// hey, looks like a stream. but vinyl won't detect it. | ||
// pipe it to a PassThrough, and use that | ||
const s = new stream.PassThrough() | ||
return c.pipe(s) | ||
} | ||
function getFilesStream (files, opts) { | ||
if (!files) return null | ||
|
||
const mp = new Multipart() | ||
|
||
flatmap(files, file => { | ||
if (typeof file === 'string') { | ||
if (!isNode) { | ||
throw new Error('Can not add paths in node') | ||
} | ||
|
||
return loadPaths(opts, file) | ||
} | ||
|
||
if (file.path && (file.content || file.dir)) { | ||
return file | ||
} | ||
|
||
throw new Error('vinyl will not accept: ' + c) | ||
return { | ||
path: '', | ||
dir: false, | ||
content: file | ||
} | ||
}).forEach(file => { | ||
mp.addPart({ | ||
headers: headers(file), | ||
body: file.content | ||
}) | ||
}) | ||
|
||
return mp | ||
} | ||
|
||
exports = module.exports = getFilesStream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters