-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Files Quest #156
Changes from all commits
2bb65f6
e768675
4e5df90
f5d063d
318fa2d
b66665e
72e193c
51d7bdb
8c07406
b68f980
7b62937
65ca444
40df5f3
74c3a83
1948e6c
14eaeeb
3a708ad
a5a3b80
430d296
247671f
12f5e1a
804147c
a00df6c
dbd8e61
01e3b6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const IPFS = require('../../../core') | ||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:version') | ||
log.error = debug('cli:version:error') | ||
const bs58 = require('bs58') | ||
const Readable = require('stream').Readable | ||
const fs = require('fs') | ||
const async = require('async') | ||
const pathj = require('path') | ||
const glob = require('glob') | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Add a file to IPFS using the UnixFS data format', | ||
|
@@ -19,15 +24,120 @@ module.exports = Command.extend({ | |
}, | ||
|
||
run: (recursive, path) => { | ||
var node = new IPFS() | ||
path = process.cwd() + '/' + path | ||
node.files.add(path, { | ||
recursive: recursive | ||
}, (err, stats) => { | ||
let s | ||
let rs | ||
|
||
if (!path) { | ||
throw new Error('Error: Argument \'path\' is required') | ||
} | ||
|
||
s = fs.statSync(path) | ||
|
||
if (s.isDirectory() && recursive === false) { | ||
throw new Error('Error: ' + process.cwd() + ' is a directory, use the \'-r\' flag to specify directories') | ||
} | ||
if (path === '.' && recursive === true) { | ||
path = process.cwd() | ||
s = fs.statSync(process.cwd()) | ||
} else if (path === '.' && recursive === false) { | ||
s = fs.statSync(process.cwd()) | ||
if (s.isDirectory()) { | ||
throw new Error('Error: ' + process.cwd() + ' is a directory, use the \'-r\' flag to specify directories') | ||
} | ||
} | ||
|
||
glob(pathj.join(path, '/**/*'), (err, res) => { | ||
if (err) { | ||
return console.log(err) | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does the |
||
} | ||
console.log('added', bs58.encode(stats.Hash).toString(), stats.Name) | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does the |
||
} | ||
var files = [] | ||
if (utils.isDaemonOn()) { | ||
if (res.length !== 0) { | ||
const index = path.lastIndexOf('/') | ||
async.eachLimit(res, 10, (element, callback) => { | ||
rs = new Readable() | ||
const addPath = element.substring(index + 1, element.length) | ||
if (fs.statSync(element).isDirectory()) { | ||
callback() | ||
} else { | ||
const buffered = fs.readFileSync(element) | ||
rs.push(buffered) | ||
rs.push(null) | ||
const filePair = {path: addPath, content: rs} | ||
files.push(filePair) | ||
callback() | ||
} | ||
}, (err) => { | ||
if (err) { | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for wrapping the |
||
} | ||
ipfs.add(files, (err, res) => { | ||
if (err) { | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for wrapping the |
||
} | ||
res.forEach((goRes) => { | ||
console.log('added', goRes.Hash, goRes.Name) | ||
}) | ||
}) | ||
}) | ||
} else { | ||
rs = new Readable() | ||
const buffered = fs.readFileSync(path) | ||
rs.push(buffered) | ||
rs.push(null) | ||
path = path.substring(path.lastIndexOf('/') + 1, path.length) | ||
const filePair = {path: path, content: rs} | ||
files.push(filePair) | ||
ipfs.add(files, (err, res) => { | ||
if (err) { | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for wrapping the |
||
} | ||
console.log('added', res[0].Hash, res[0].Name) | ||
}) | ||
} | ||
return | ||
} | ||
const i = ipfs.files.add() | ||
i.on('data', (file) => { | ||
console.log('added', bs58.encode(file.multihash).toString(), file.path) | ||
}) | ||
if (res.length !== 0) { | ||
const index = path.lastIndexOf('/') | ||
async.eachLimit(res, 10, (element, callback) => { | ||
rs = new Readable() | ||
const addPath = element.substring(index + 1, element.length) | ||
if (fs.statSync(element).isDirectory()) { | ||
callback() | ||
} else { | ||
const buffered = fs.readFileSync(element) | ||
rs.push(buffered) | ||
rs.push(null) | ||
const filePair = {path: addPath, stream: rs} | ||
i.write(filePair) | ||
callback() | ||
} | ||
}, (err) => { | ||
if (err) { | ||
throw new Error(err) | ||
} | ||
i.end() | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for |
||
}) | ||
} else { | ||
rs = new Readable() | ||
const buffered = fs.readFileSync(path) | ||
path = path.substring(path.lastIndexOf('/') + 1, path.length) | ||
rs.push(buffered) | ||
rs.push(null) | ||
const filePair = {path: path, stream: rs} | ||
i.write(filePair) | ||
i.end() | ||
} | ||
}) | ||
}) | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const debug = require('debug') | ||
const utils = require('../../utils') | ||
const log = debug('cli:files') | ||
log.error = debug('cli:files:error') | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Download IPFS objects', | ||
|
||
options: {}, | ||
|
||
run: (path, options) => { | ||
if (!path) { | ||
throw new Error("Argument 'path' is required") | ||
} | ||
if (!options) { | ||
options = {} | ||
} | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
if (utils.isDaemonOn()) { | ||
ipfs.cat(path, (err, res) => { | ||
if (err) { | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for wrapping the |
||
} | ||
console.log(res.toString()) | ||
}) | ||
return | ||
} | ||
|
||
ipfs.files.cat(path, (err, res) => { | ||
if (err) { | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for wrapping the |
||
} | ||
if (res) { | ||
res.on('file', (data) => { | ||
data.stream.pipe(process.stdout) | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const debug = require('debug') | ||
const utils = require('../../utils') | ||
const log = debug('cli:files') | ||
log.error = debug('cli:files:error') | ||
var fs = require('fs') | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Download IPFS objects', | ||
|
||
options: {}, | ||
|
||
run: (path, options) => { | ||
let dir | ||
let filepath | ||
let ws | ||
|
||
if (!path) { | ||
throw new Error("Argument 'path' is required") | ||
} | ||
if (!options) { | ||
options = {} | ||
dir = process.cwd() | ||
} else { | ||
if (options.slice(-1) !== '/') { | ||
options += '/' | ||
} | ||
dir = options | ||
} | ||
|
||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
ipfs.files.get(path, (err, data) => { | ||
if (err) { | ||
throw new Error(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for wrapping the |
||
} | ||
data.on('file', (data) => { | ||
if (data.path.lastIndexOf('/') === -1) { | ||
filepath = data.path | ||
if (data.dir === false) { | ||
ws = fs.createWriteStream(dir + data.path) | ||
data.stream.pipe(ws) | ||
} else { | ||
try { | ||
fs.mkdirSync(dir + filepath) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
} | ||
} else { | ||
filepath = data.path.substring(0, data.path.lastIndexOf('/') + 1) | ||
try { | ||
fs.mkdirSync(dir + filepath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe |
||
} catch (err) { | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why can this |
||
ws = fs.createWriteStream(dir + data.path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe |
||
// data.stream.on('end', () => { | ||
// console.log('finished writing file to disk') | ||
// }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this commented out? |
||
data.stream.pipe(ws) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,12 @@ const DAGService = mDAG.DAGService | |
const peerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
const multiaddr = require('multiaddr') | ||
const importer = require('ipfs-data-importing').import | ||
const Importer = require('ipfs-unixfs-engine').importer | ||
const Exporter = require('ipfs-unixfs-engine').exporter | ||
const libp2p = require('libp2p-ipfs') | ||
const init = require('./init') | ||
const IPFSRepo = require('ipfs-repo') | ||
const UnixFS = require('ipfs-unixfs') | ||
|
||
exports = module.exports = IPFS | ||
|
||
|
@@ -392,10 +394,52 @@ function IPFS (repo) { | |
} | ||
|
||
this.files = { | ||
add: (path, options, callback) => { | ||
options.path = path | ||
options.dagService = dagS | ||
importer(options, callback) | ||
add: (arr, callback) => { | ||
if (typeof arr === 'function') { | ||
callback = arr | ||
arr = undefined | ||
} | ||
if (callback === undefined) { | ||
callback = function noop () {} | ||
} | ||
if (arr === undefined) { | ||
return new Importer(dagS) | ||
} | ||
|
||
const i = new Importer(dagS) | ||
const res = [] | ||
|
||
i.on('data', (info) => { | ||
res.push(info) | ||
}) | ||
|
||
i.on('end', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
callback(null, res) | ||
}) | ||
|
||
arr.forEach((tuple) => { | ||
i.write(tuple) | ||
}) | ||
|
||
i.end() | ||
}, | ||
cat: (hash, callback) => { | ||
dagS.get(hash, (err, fetchedNode) => { | ||
if (err) { | ||
return callback(err, null) | ||
} | ||
const data = UnixFS.unmarshal(fetchedNode.data) | ||
if (data.type === 'directory') { | ||
callback('This dag node is a directory', null) | ||
} else { | ||
const exportEvent = Exporter(hash, dagS) | ||
callback(null, exportEvent) | ||
} | ||
}) | ||
}, | ||
get: (hash, callback) => { | ||
var exportFile = Exporter(hash, dagS) | ||
callback(null, exportFile) | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use backticks for these error messages, like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I think the
Error:
in the front is not needed, in go-ipfs that's just the log level