-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
98 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,41 @@ | ||
import { create, globSource } from 'kubo-rpc-client'; | ||
import { writeFile } from 'node:fs'; | ||
|
||
const auth = process.env.IPFS_RPC_API_SECRET; | ||
const url = process.env.IPFS_RPC_API_URL; | ||
const key = process.env.IPFS_RPC_API_NAME_KEY; | ||
const cids_path = process.env.CIDS_FILE_PATH; | ||
|
||
async function IPFSDeploy(){ | ||
const files = []; | ||
const client = await create({ | ||
url: `${url}/api/v0`, | ||
headers: { | ||
authorization: `Bearer ${auth}`, | ||
} | ||
}) | ||
|
||
console.log('adding files on IPFS..'); | ||
for await (const file of client.addAll(globSource(".", "public/**/*"))) { | ||
files.push({ path: file.path, cid: file.cid.toString() }); | ||
async function IPFSDeploy(ipfsRpcApiKey, ipfsRpcApiUrl, ipfsRpcApiNameKey){ | ||
const client = await create({ | ||
url: `${ipfsRpcApiUrl}/api/v0`, | ||
headers: { | ||
authorization: `Bearer ${ipfsRpcApiKey}`, | ||
} | ||
|
||
const newPublicDirCID = files[files.length - 1].cid; | ||
console.log(`pinned: ${newPublicDirCID}`); | ||
|
||
let unpinned = ''; | ||
for await (const file of client.pin.ls()) { | ||
if (file.type === 'recursive' && file.cid.toString() !== newPublicDirCID) { | ||
for await (const unpinnedFile of client.pin.rmAll(file)) { | ||
unpinned = unpinnedFile; | ||
} | ||
} | ||
}); | ||
|
||
let lastUploadedCID; | ||
console.log('adding files on IPFS..'); | ||
for await (const file of client.addAll(globSource(".", "public/**/*"))) { | ||
lastUploadedCID = file.cid.toString(); | ||
} | ||
console.log(`pinned: ${lastUploadedCID}`); | ||
|
||
for await (const file of client.pin.ls()) { | ||
const cid = file.cid.toString(); | ||
if (file.type === 'recursive' && cid !== lastUploadedCID) { | ||
await client.pin.rmAll(file); | ||
console.log(`unpinned: ${cid}`); | ||
} | ||
} | ||
|
||
console.log(`unpinned: ${unpinned}`); | ||
|
||
console.log('publishing on IPNS...') | ||
const r = await client.name.publish(`${newPublicDirCID}`, { key }); | ||
|
||
console.log('new version available on:'); | ||
console.log(`IPFS address: https://ipfs.io/ipfs/${newPublicDirCID}`); | ||
console.log(`IPNS address: https://ipfs.io/ipns/${r.name}`); | ||
console.log('publishing on IPNS...') | ||
const r = await client.name.publish(`${lastUploadedCID}`, { | ||
key: ipfsRpcApiNameKey | ||
}); | ||
console.log('new version available on:'); | ||
console.log(`IPFS address: https://ipfs.io/ipfs/${lastUploadedCID}`); | ||
console.log(`IPNS address: https://ipfs.io/ipns/${r.name}`); | ||
|
||
console.log(`saving unpinned and pinned CIDs in ${cids_path}`) | ||
await writeFile(cids_path, `${unpinned}\n${newPublicDirCID}`, err => { | ||
if (err) throw err; | ||
console.log(`${cids_path} has been saved!`); | ||
}); | ||
console.log(lastUploadedCID); | ||
} | ||
|
||
IPFSDeploy(); | ||
IPFSDeploy( | ||
process.argv[2], | ||
process.argv[3], | ||
process.argv[4] | ||
); |
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,54 @@ | ||
let API_KEY = ''; | ||
const PINATA_BASE_URL = 'https://api.pinata.cloud'; | ||
const ENDPOINT_UNPIN = '/pinning/unpin/'; | ||
const ENDPOINT_PIN_LIST = '/data/pinList'; | ||
const ENDPOINT_PIN_BY_HASH = '/pinning/pinByHash'; | ||
|
||
async function pinataApiReq(resource, method = 'GET', body = null) { | ||
const headers = { | ||
authorization: `Bearer ${API_KEY}`, | ||
}; | ||
|
||
const options = { | ||
method, | ||
headers | ||
}; | ||
|
||
if (body) { | ||
options.headers['Content-Type'] = 'application/json'; | ||
options.body = JSON.stringify(body); | ||
} | ||
|
||
return fetch(PINATA_BASE_URL + resource, options).then(data => | ||
data.json().then(encoded => encoded, _ => data) | ||
); | ||
} | ||
|
||
async function upadtePinned(apiKey, pinName, cidToPin) { | ||
API_KEY = apiKey; | ||
let response = await pinataApiReq(ENDPOINT_PIN_LIST); | ||
const hashesToUnpin = response.rows | ||
.filter(r => r.date_unpinned === null && | ||
r.metadata.name === pinName) | ||
.map(r => r.ipfs_pin_hash); | ||
|
||
await Promise.all(hashesToUnpin.map(cid => | ||
pinataApiReq(ENDPOINT_UNPIN + cid, 'DELETE').then(_ => { | ||
console.log(`unpinned: ${cid}`); | ||
}) | ||
)); | ||
|
||
response = await pinataApiReq(ENDPOINT_PIN_BY_HASH, 'POST', { | ||
hashToPin: cidToPin, | ||
pinataMetadata: JSON.stringify({ | ||
name: pinName | ||
}), | ||
}); | ||
|
||
console.log(`pinned: ${response.ipfsHash}`); | ||
} | ||
|
||
(() => { | ||
API_KEY = process.argv[2]; | ||
upadtePinned(process.argv[3], process.argv[4]); | ||
})(); |
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