-
Notifications
You must be signed in to change notification settings - Fork 1.2k
files add, cat, get core + cli #197
Changes from all commits
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,45 @@ | ||
'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 fs = require('fs') | ||
const async = require('async') | ||
const path = require('path') | ||
const glob = require('glob') | ||
|
||
function checkPath (inPath, recursive) { | ||
// This function is to check for the following possible inputs | ||
// 1) "." add the cwd but throw error for no recursion flag | ||
// 2) "." -r return the cwd | ||
// 3) "/some/path" but throw error for no recursion | ||
// 4) "/some/path" -r | ||
// 5) No path, throw err | ||
// 6) filename.type return the cwd + filename | ||
|
||
if (!inPath) { | ||
throw new Error('Error: Argument \'path\' is required') | ||
} | ||
|
||
var s = fs.statSync(inPath) | ||
|
||
if (s.isDirectory() && recursive === false) { | ||
throw new Error('Error: ' + process.cwd() + ' is a directory, use the \'-r\' flag to specify directories') | ||
} | ||
if (inPath === '.' && recursive === true) { | ||
inPath = process.cwd() | ||
return inPath | ||
} else if (inPath === '.' && 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') | ||
} | ||
} | ||
return inPath | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Add a file to IPFS using the UnixFS data format', | ||
|
@@ -18,16 +52,53 @@ module.exports = Command.extend({ | |
} | ||
}, | ||
|
||
run: (recursive, path) => { | ||
var node = new IPFS() | ||
path = process.cwd() + '/' + path | ||
node.files.add(path, { | ||
recursive: recursive | ||
}, (err, stats) => { | ||
run: (recursive, inPath) => { | ||
let rs | ||
|
||
inPath = checkPath(inPath, recursive) | ||
|
||
glob(path.join(inPath, '/**/*'), (err, res) => { | ||
if (err) { | ||
return console.log(err) | ||
throw err | ||
} | ||
console.log('added', bs58.encode(stats.Hash).toString(), stats.Name) | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
const i = ipfs.files.add() | ||
var filePair | ||
i.on('data', (file) => { | ||
console.log('added', bs58.encode(file.multihash).toString(), file.path) | ||
}) | ||
i.once('end', () => { | ||
return | ||
}) | ||
if (res.length !== 0) { | ||
const index = inPath.lastIndexOf('/') | ||
async.eachLimit(res, 10, (element, callback) => { | ||
const addPath = element.substring(index + 1, element.length) | ||
if (fs.statSync(element).isDirectory()) { | ||
callback() | ||
} else { | ||
rs = fs.createReadStream(element) | ||
filePair = {path: addPath, stream: rs} | ||
i.write(filePair) | ||
callback() | ||
} | ||
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. This could be simplified: if (!fs.statSync(element).isDirectory()) {
i.write({path: addPath, stream: fs.createReadStrem(element})
}
callback() |
||
}, (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
i.end() | ||
}) | ||
} else { | ||
rs = fs.createReadStream(inPath) | ||
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length) | ||
filePair = {path: inPath, stream: rs} | ||
i.write(filePair) | ||
i.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. what is all of this doing? Wouldn't glob do this for free? 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. glob returns the full path, so I have to trim it to so we don't add unnecessary directories 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. Isn't inPath being converted to the filename? Instead of trimming the full path till the Ran an experiment and I can only get the filename, the path value should be the total path relative to where the process was executed.
|
||
} | ||
}) | ||
}) | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'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 | ||
} | ||
ipfs.files.cat(path, (err, res) => { | ||
if (err) { | ||
throw (err) | ||
} | ||
if (res) { | ||
res.on('file', (data) => { | ||
data.stream.pipe(process.stdout) | ||
}) | ||
} | ||
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. Do we need the |
||
}) | ||
}) | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'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') | ||
const path = require('path') | ||
const pathExists = require('path-exists') | ||
const async = require('async') | ||
|
||
function checkArgs (hash, outPath) { | ||
if (!hash) { | ||
throw new Error("Argument 'path' is required") | ||
} | ||
// format the output directory | ||
if (!outPath) { | ||
var cwd = process.cwd() | ||
return cwd | ||
} else { | ||
if (!outPath.endsWith('/')) { | ||
outPath += '/' | ||
} | ||
if (!outPath.startsWith('/')) { | ||
outPath = path.join('/', outPath) | ||
} | ||
var directory = outPath | ||
return directory | ||
} | ||
} | ||
|
||
function getFiles (result, dir) { | ||
var filePath | ||
result.on('file', (file) => { | ||
// Check to see if the result is in a directory | ||
if (file.path.lastIndexOf('/') === -1) { | ||
filePath = file.path | ||
// Check to see if the result is a directory | ||
if (file.dir === false) { | ||
const ws = fs.createWriteStream(path.join(dir, file.path)) | ||
file.stream.pipe(ws) | ||
} else { | ||
// Check to see if the directory has already been created | ||
pathExists(path.join(dir, file.path)).then(exists => { | ||
if (!exists) { | ||
fs.mkdir(path.join(dir, file.path), (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. how about putting this into a function function ensureDir (dir) {
pathExists(dir).then((exists) => {
if (exists) {
return
}
fs.mkdir(dir, (err) => {
if (err) throw err
})
})
.catch((err) => {
throw err
})
}
ensureDir(path.join(dir, file.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. that way you can reuse it below |
||
if (err) { | ||
throw err | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} else { | ||
// Check to see if the directory has already been created | ||
filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1) | ||
pathExists(path.join(dir, filePath)).then(exists => { | ||
// Create a directory for the incoming files | ||
if (!exists) { | ||
async.waterfall([ | ||
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. as you are not actually passing values from one function to the next you use |
||
(cb) => { | ||
fs.mkdir(path.join(dir, filePath), (err) => { | ||
if (err) { | ||
cb(err) | ||
} | ||
cb(null) | ||
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 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. or even simpler: (cb) => fs.mkdir(path.join(dir, filePath), cb) |
||
}) | ||
}, | ||
(cb) => { | ||
const ws = fs.createWriteStream(path.join(dir, file.path)) | ||
file.stream.pipe(ws) | ||
cb(null) | ||
} | ||
], (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
}) | ||
} | ||
// Just write the file | ||
const ws = fs.createWriteStream(path.join(dir, file.path)) | ||
file.stream.pipe(ws) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Download IPFS objects', | ||
|
||
run: (hash, outPath) => { | ||
const dir = checkArgs(hash, outPath) | ||
|
||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
ipfs.files.get(hash, (err, result) => { | ||
if (err) { | ||
throw err | ||
} | ||
getFiles(result, dir) | ||
}) | ||
}) | ||
} | ||
}) |
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.
Is there a need for this return?
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.
inPath is not declared in this scope and needs to be returned on line 58, unless I'm missing something.
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.
I mean, since there is the return of inPath in the end of the func and this assignment of inPath is within a if/else clause, you just need the return once