Skip to content

Commit

Permalink
pinCID comments (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
SidSethi committed Mar 4, 2021
1 parent 2167892 commit 6fdca0e
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions creator-node/src/pinCID.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const IPFS_PIN_TIMEOUT = 30 /* sec */ * 1000 /* ms */

/**
* Pin CIDs from config
* Is a best-effort operation - will swallow any errors
*
* @param {IPFS} ipfs
*/
const pinCID = async (ipfs) => {
Expand All @@ -19,12 +21,14 @@ const pinCID = async (ipfs) => {
.map(cid => new CID(cid))
const removeCIDs = []

// Build list of CIDs currently pinned that are not in addCIDs list list for future removal
for await (const { cid } of ipfs.pin.ls({ type: 'recursive' })) {
if (!addCIDs.some(addCID => addCID.equals(cid))) {
removeCIDs.push(cid)
}
}

// Pin every CID in addCIDs list
for (const cid of addCIDs) {
try {
await ipfs.pin.add(cid, { recursive: true, timeout: IPFS_PIN_TIMEOUT })
Expand All @@ -34,6 +38,7 @@ const pinCID = async (ipfs) => {
}
}

// Unpin every CID in removeCIDs list
for (const cid of removeCIDs) {
try {
await ipfs.pin.rm(cid, { recursive: true, timeout: IPFS_PIN_TIMEOUT })
Expand Down

0 comments on commit 6fdca0e

Please sign in to comment.