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.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,296 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:pin') | ||
log.error = debug('cli:pin:error') | ||
const bs58 = require('bs58') | ||
|
||
const onError = (err) => { | ||
if (err) { | ||
console.error(err) | ||
throw err | ||
} | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Pins objects to local storage.', | ||
|
||
options: { | ||
// Recursively pin the object linked to by the specified object(s) | ||
recursive: { | ||
alias: 'r', | ||
type: 'boolean', | ||
default: true | ||
} | ||
}, | ||
|
||
run: (recursive, path) => { | ||
utils.getIPFS((err, ipfs) => { | ||
onError(err) | ||
// load persistent pin set from datastore | ||
ipfs.pinner.load(() => { | ||
const matched = path.match(/^(?:\/ipfs\/)?([^\/]+(?:\/[^\/]+)*)\/?$/) | ||
if (!matched) { | ||
onError(new Error('invalid ipfs ref path')) | ||
} | ||
const split = matched[1].split('/') | ||
const rootHash = split[0] | ||
const key = new Buffer(bs58.decode(rootHash)) | ||
const links = split.slice(1, split.length) | ||
const pathFn = (err, obj) => { | ||
onError(err) | ||
if (links.length) { | ||
const linkName = links.shift() | ||
const nextLink = obj.links.filter((link) => { | ||
return (link.name === linkName) | ||
}) | ||
if (!nextLink.length) { | ||
onError(new Error( | ||
'pin: no link named ' + linkName + | ||
' under ' + obj.toJSON().Hash | ||
)) | ||
} | ||
const nextHash = nextLink[0].hash | ||
ipfs.object.get(nextHash, pathFn) | ||
} else { | ||
ipfs.pinner.pin(obj, recursive, (err) => { | ||
onError(err) | ||
// save modified pin state to datastore | ||
ipfs.pinner.flush((err, root) => { | ||
onError(err) | ||
const mode = recursive ? ' recursively' : ' directly' | ||
console.log('pinned ' + obj.toJSON().Hash + mode) | ||
}) | ||
}) | ||
} | ||
} | ||
ipfs.object.get(key, pathFn) | ||
}) | ||
}) | ||
} | ||
}) |
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,125 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:pin') | ||
log.error = debug('cli:pin:error') | ||
const bs58 = require('bs58') | ||
|
||
const onError = (err) => { | ||
if (err) { | ||
console.error(err) | ||
throw err | ||
} | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Pins objects to local storage.', | ||
|
||
options: { | ||
// The type of pinned keys to list. Can be | ||
// "direct", "indirect", "recursive", or "all". | ||
type: { | ||
alias: 't', | ||
type: 'string', | ||
default: 'all' | ||
}, | ||
// Write just hashes of objects. | ||
quiet: { | ||
alias: 'q', | ||
type: 'boolean', | ||
default: false | ||
} | ||
}, | ||
|
||
run: (type, quiet, path) => { | ||
utils.getIPFS((err, ipfs) => { | ||
onError(err) | ||
const types = ipfs.pinner.types | ||
// load persistent pin set from datastore | ||
ipfs.pinner.load(() => { | ||
if (path) { | ||
const matched = path.match(/^(?:\/ipfs\/)?([^\/]+(?:\/[^\/]+)*)\/?$/) | ||
if (!matched) { | ||
onError(new Error('invalid ipfs ref path')) | ||
} | ||
const split = matched[1].split('/') | ||
const rootHash = split[0] | ||
const key = new Buffer(bs58.decode(rootHash)) | ||
const links = split.slice(1, split.length) | ||
const pathFn = (err, obj) => { | ||
onError(err) | ||
if (links.length) { | ||
const linkName = links.shift() | ||
const nextLink = obj.links.filter((link) => { | ||
return (link.name === linkName) | ||
}) | ||
if (!nextLink.length) { | ||
onError(new Error( | ||
'pin: no link named ' + linkName + | ||
' under ' + obj.toJSON().Hash | ||
)) | ||
} | ||
const nextHash = nextLink[0].hash | ||
ipfs.object.get(nextHash, pathFn) | ||
} else { | ||
ipfs.pinner.isPinnedWithType(obj.multihash(), type, (err, pinned, reason) => { | ||
onError(err) | ||
if (!pinned) { | ||
onError(new Error('Path ' + path + ' is not pinned')) | ||
} | ||
if (reason !== types.direct && | ||
reason !== types.recursive) { | ||
reason = 'indirect through ' + reason | ||
} | ||
console.log(obj.toJSON().Hash + (quiet ? '' : ' ' + reason)) | ||
}) | ||
} | ||
} | ||
ipfs.object.get(key, pathFn) | ||
} else { | ||
const printDirect = () => { | ||
ipfs.pinner.directKeyStrings().forEach((key) => { | ||
console.log(key + (quiet ? '' : ' direct')) | ||
}) | ||
} | ||
const printRecursive = () => { | ||
ipfs.pinner.recursiveKeyStrings().forEach((key) => { | ||
console.log(key + (quiet ? '' : ' recursive')) | ||
}) | ||
} | ||
const printIndirect = () => { | ||
ipfs.pinner.getIndirectKeys((err, keys) => { | ||
onError(err) | ||
keys.forEach((key) => { | ||
console.log(key + (quiet ? '' : ' indirect')) | ||
}) | ||
}) | ||
} | ||
switch (type) { | ||
case types.direct: | ||
printDirect() | ||
break | ||
case types.recursive: | ||
printRecursive() | ||
break | ||
case types.indirect: | ||
printIndirect() | ||
break | ||
case types.all: | ||
printDirect() | ||
printRecursive() | ||
printIndirect() | ||
break | ||
default: | ||
onError(new Error( | ||
"Invalid type '" + type + "', " + | ||
'must be one of {direct, indirect, recursive, all}' | ||
)) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
}) |
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,77 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:pin') | ||
log.error = debug('cli:pin:error') | ||
const bs58 = require('bs58') | ||
|
||
const onError = (err) => { | ||
if (err) { | ||
console.error(err) | ||
throw err | ||
} | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Removes the pinned object from local storage.', | ||
|
||
options: { | ||
recursive: { | ||
alias: 'r', | ||
type: 'boolean', | ||
default: true | ||
} | ||
}, | ||
|
||
run: (recursive, path) => { | ||
utils.getIPFS((err, ipfs) => { | ||
onError(err) | ||
// load persistent pin set from datastore | ||
ipfs.pinner.load(() => { | ||
const matched = path.match(/^(?:\/ipfs\/)?([^\/]+(?:\/[^\/]+)*)\/?$/) | ||
if (!matched) { | ||
onError(new Error('invalid ipfs ref path')) | ||
} | ||
const split = matched[1].split('/') | ||
const rootHash = split[0] | ||
const key = new Buffer(bs58.decode(rootHash)) | ||
const links = split.slice(1, split.length) | ||
const pathFn = (err, obj) => { | ||
onError(err) | ||
if (links.length) { | ||
const linkName = links.shift() | ||
const nextLink = obj.links.filter((link) => { | ||
return (link.name === linkName) | ||
}) | ||
if (!nextLink.length) { | ||
onError(new Error( | ||
'pin: no link named ' + linkName + | ||
' under ' + obj.toJSON().Hash | ||
)) | ||
} | ||
const nextHash = nextLink[0].hash | ||
ipfs.object.get(nextHash, pathFn) | ||
} else { | ||
ipfs.pinner.isPinned(obj.multihash(), (err, pinned, reason) => { | ||
onError(err) | ||
if (!pinned) { | ||
onError(new Error('not pinned')) | ||
} | ||
ipfs.pinner.unpin(obj.multihash(), recursive, (err) => { | ||
onError(err) | ||
// save modified pin state to datastore | ||
ipfs.pinner.flush((err, root) => { | ||
onError(err) | ||
console.log('unpinned ' + obj.toJSON().Hash) | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
ipfs.object.get(key, pathFn) | ||
}) | ||
}) | ||
} | ||
}) |
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
Oops, something went wrong.