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
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 #204 from ipfs/files-core-fix
Files core fix
- Loading branch information
Showing
13 changed files
with
382 additions
and
72 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
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) { | ||
return process.cwd() | ||
} | ||
|
||
if (!outPath.endsWith('/')) { | ||
outPath += '/' | ||
} | ||
|
||
if (!outPath.startsWith('/')) { | ||
outPath = path.join('/', outPath) | ||
} | ||
|
||
return outPath | ||
} | ||
|
||
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.