This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Files core fix #204
Merged
Merged
Files core fix #204
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e84ef8
files add, cat, get core + cli
nginnever 8df6248
updated get command / libp2p
nginnever aae2259
code review fixes
dignifiedquire 46d9e6a
fix(files:add): simplify checkPath
dignifiedquire 7f89bfb
fix(files:get): simplify checkArgs
dignifiedquire 56f965d
test(http-api): give socket closing more time
dignifiedquire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -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) | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'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') | ||
|
||
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 | ||
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. can be reduced to just the return |
||
} else { | ||
if (!outPath.endsWith('/')) { | ||
outPath += '/' | ||
} | ||
if (!outPath.startsWith('/')) { | ||
outPath = path.join('/', outPath) | ||
} | ||
var directory = outPath | ||
return directory | ||
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. same here |
||
} | ||
} | ||
|
||
function ensureDir (dir, cb) { | ||
pathExists(dir) | ||
.then((exists) => { | ||
if (!exists) { | ||
fs.mkdir(dir, cb) | ||
} else { | ||
cb() | ||
} | ||
}) | ||
.catch(cb) | ||
} | ||
|
||
function fileHandler (result, dir) { | ||
return function onFile (file) { | ||
// Check to see if the result is in a directory | ||
if (file.path.lastIndexOf('/') === -1) { | ||
const dirPath = path.join(dir, file.path) | ||
// Check to see if the result is a directory | ||
if (file.dir === false) { | ||
file.stream.pipe(fs.createWriteStream(dirPath)) | ||
} else { | ||
ensureDir(dirPath, (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
}) | ||
} | ||
} else { | ||
const filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1) | ||
const dirPath = path.join(dir, filePath) | ||
ensureDir(dirPath, (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
file.stream.pipe(fs.createWriteStream(dirPath)) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
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 | ||
} | ||
result.on('file', fileHandler(result, dir)) | ||
}) | ||
}) | ||
} | ||
}) |
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,13 +1,57 @@ | ||
'use strict' | ||
|
||
const importer = require('ipfs-data-importing').import | ||
const Importer = require('ipfs-unixfs-engine').importer | ||
const Exporter = require('ipfs-unixfs-engine').exporter | ||
const UnixFS = require('ipfs-unixfs') | ||
|
||
module.exports = function libp2p (self) { | ||
module.exports = function files (self) { | ||
return { | ||
add: (path, options, callback) => { | ||
options.path = path | ||
options.dagService = self._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(self._dagS) | ||
} | ||
|
||
const i = new Importer(self._dagS) | ||
const res = [] | ||
|
||
i.on('data', (info) => { | ||
res.push(info) | ||
}) | ||
|
||
i.once('end', () => { | ||
callback(null, res) | ||
}) | ||
|
||
arr.forEach((tuple) => { | ||
i.write(tuple) | ||
}) | ||
|
||
i.end() | ||
}, | ||
cat: (hash, callback) => { | ||
self._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, self._dagS) | ||
callback(null, exportEvent) | ||
} | ||
}) | ||
}, | ||
get: (hash, callback) => { | ||
var exportFile = Exporter(hash, self._dagS) | ||
callback(null, exportFile) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
won't
process.cwd()
(current working directory) always be a directory?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.
yeah