Created
March 3, 2018 01:08
-
-
Save XertroV/1c909efeb061193a8feb4f4a2531153b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const IPFS = require('ipfs-api'); | |
const R = require('ramda'); | |
const async = require('async'); | |
const child_process = require('child_process') | |
const ipfs = IPFS('localhost', '5001', {protocol: 'http'}); | |
let size = 0; | |
let nErrors = 0; | |
let nRm = 0; | |
const rmAll = (mhs) => { | |
const tasks = R.map(mh => cb => ipfs.pin.rm(mh, (err, stdout, stderr) => { | |
if (err) { | |
console.log(err); | |
nErrors += 1; | |
} else { | |
nRm += 1; | |
console.log(`rm'd ${mh}`); | |
cb(null); | |
} | |
}), mhs); | |
async.parallelLimit(tasks, 50, (err, res) => { | |
if (err) | |
throw err; | |
console.log(`Total rm'd pins: ${nRm}`); | |
console.log(`Total errors: ${nErrors}`); | |
}); | |
} | |
const sumSize = mhs => { | |
const tasks = R.map(mh => cb => ipfs.object.stat(mh, (err, res) => { | |
if (err) { | |
console.log(err); | |
nErrors += 1; | |
} else { | |
const {CumulativeSize} = res; | |
size += CumulativeSize; | |
cb(null, CumulativeSize); | |
} | |
}), mhs); | |
async.parallelLimit(tasks, 50, (err, res) => { | |
if (err) | |
throw err; | |
const summedSize = R.sum(res); | |
console.log(`Total size of pinned objs returned is ${summedSize / 1000 / 1000} MB`); | |
console.log(`Total size of pinned objs summed is ${size / 1000 / 1000} MB`); | |
console.log(`Total errors: ${nErrors}`); | |
}); | |
} | |
const haveArg = str => R.contains(str, process.argv); | |
let todo; | |
if (haveArg("--rmAll")) { | |
todo = rmAll; | |
} else if (haveArg("--sumSize")) { | |
todo = sumSize; | |
} else { | |
console.error("Valid args are `--rmAll` or `--sumSize`"); | |
process.exit(1); | |
} | |
ipfs.pin.ls((err, pinset) => { | |
if (err) | |
throw err; | |
const mhs = R.keys(pinset); | |
console.log(`N pins: ${R.length(mhs)}`); | |
todo(mhs); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment