Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

feat: pin #107

Merged
merged 1 commit into from
Oct 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@
"async": "^2.5.0",
"bl": "^1.2.1",
"boom": "^5.2.0",
"debug": "^3.0.1",
"cids": "^0.5.1",
"debug": "^3.0.1",
"file-type": "^6.1.0",
"filesize": "^3.5.10",
"fnv1a": "^1.0.1",
"fsm-event": "^2.1.0",
"glob": "^7.1.2",
"hapi": "^16.5.2",
"hapi-set-header": "^1.0.2",
"hoek": "^4.2.0",
"interface-datastore": "^0.3.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get or put in the datastore, the first argument should be a datastore Key object. Based on the example here it seems like I need to import this like const Key = require('interface-datastore').Key

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

@dignifiedquire is there any reason why the .get and .put couldn't take a string or a buffer? I understand that Key has some utility methods, but the cast could happen internally as well.

"ipfs-api": "^14.3.5",
"ipfs-bitswap": "~0.17.2",
"ipfs-block": "~0.6.0",
Expand All @@ -126,6 +128,7 @@
"libp2p-websockets": "~0.10.1",
"lodash.flatmap": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.0",
"lodash.sortby": "^4.7.0",
"lodash.values": "^4.3.0",
"mafmt": "^3.0.1",
Expand All @@ -139,6 +142,7 @@
"peer-id": "~0.10.1",
"peer-info": "~0.11.0",
"promisify-es6": "^1.0.3",
"protocol-buffers": "^3.2.1",
"pull-file": "^1.0.0",
"pull-paramap": "^1.2.2",
"pull-pushable": "^2.1.1",
Expand Down Expand Up @@ -216,4 +220,4 @@
"Łukasz Magiera <[email protected]>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>"
]
}
}
15 changes: 15 additions & 0 deletions src/cli/commands/pin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

module.exports = {
command: 'pin',

description: 'Pin and unpin objects to local storage.',

builder (yargs) {
return yargs
.commandDir('pin')
},

handler (argv) {
}
}
28 changes: 28 additions & 0 deletions src/cli/commands/pin/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

module.exports = {
command: 'add <ipfs-path>',

describe: 'Pins objects to local storage.',

builder: {
recursive: {
type: 'boolean',
alias: 'r',
default: true,
describe: 'Recursively pin the object linked to by the specified object(s).'
}
},

handler (argv) {
const paths = argv['ipfs-path'].split(' ')
const recursive = argv.recursive
const type = recursive ? 'recursive' : 'direct'
argv.ipfs.pin.add(paths[0], { recursive }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
console.log(`pinned ${res.hash} ${type}ly`)
})
})
}
}
43 changes: 43 additions & 0 deletions src/cli/commands/pin/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

module.exports = {
command: 'ls',

describe: 'List objects pinned to local storage.',

builder: {
path: {
type: 'string',
describe: 'List pinned state of specific <ipfs-path>.'
},
type: {
type: 'string',
alias: 't',
default: 'all',
describe: ('The type of pinned keys to list. ' +
'Can be "direct", "indirect", "recursive", or "all".')
},
quiet: {
type: 'boolean',
alias: 'q',
default: false,
describe: 'Write just hashes of objects.'
}
},

handler: (argv) => {
const paths = argv.path && argv.path.split(' ')
const type = argv.type
const quiet = argv.quiet
argv.ipfs.pin.ls(paths, { type }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
let line = res.hash
if (!quiet) {
line += ` ${res.type}`
}
console.log(line)
})
})
}
}
35 changes: 35 additions & 0 deletions src/cli/commands/pin/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:pin')
log.error = debug('cli:pin:error')

module.exports = {
command: 'rm <ipfs-path>',

describe: 'Removes the pinned object from local storage.',

builder: {
recursive: {
type: 'boolean',
alias: 'r',
default: true,
describe: 'Recursively unpin the objects linked to by the specified object(s).'
}
},

handler: (argv) => {
const paths = argv['ipfs-path'].split(' ')
const recursive = argv.recursive
utils.getIPFS((err, ipfs) => {
if (err) { throw err }
ipfs.pin.rm(paths, { recursive }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
console.log(`unpinned ${res.hash}`)
})
})
})
}
}
1 change: 1 addition & 0 deletions src/core/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = (self) => {

series([
(cb) => self._repo.open(cb),
(cb) => self.pin.load(cb),
(cb) => self.preStart(cb),
(cb) => {
self.state.initialized()
Expand Down
5 changes: 5 additions & 0 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ module.exports = function files (self) {
if (a.path > b.path) return -1
return 0
}),
pull.asyncMap((file, cb) => {
self.pin.add(file.hash, (err) => {
cb(err, file)
})
}),
pull.collect(callback)
)
}),
Expand Down
1 change: 1 addition & 0 deletions src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports.dag = require('./dag')
exports.libp2p = require('./libp2p')
exports.swarm = require('./swarm')
exports.ping = require('./ping')
exports.pin = require('./pin')
exports.files = require('./files')
exports.bitswap = require('./bitswap')
exports.pubsub = require('./pubsub')
Expand Down
31 changes: 31 additions & 0 deletions src/core/components/key-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const multihashes = require('multihashes')

module.exports = function KeySet (keys) {
// Buffers with identical data are still different objects, so
// they need to be cast to strings to prevent duplicates in Sets
this.keys = {}
this.add = (key) => {
this.keys[multihashes.toB58String(key)] = key
}
this.delete = (key) => {
delete this.keys[multihashes.toB58String(key)]
}
this.clear = () => {
this.keys = {}
}
this.has = (key) => {
return (multihashes.toB58String(key) in this.keys)
}
this.toArray = () => {
return Object.keys(this.keys).map((hash) => {
return this.keys[hash]
})
}
this.toStringArray = () => {
return Object.keys(this.keys)
}
keys = keys || []
keys.forEach(this.add)
}
Loading