ipfs: add pin_metadata.js for fixing dapp hashes
Details: https://github.com/status-im/infra-ipfs/issues/4 Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
55c0fc6f96
commit
f3687c0042
|
@ -5,6 +5,8 @@
|
|||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"base64-img": "^1.0.4",
|
||||
"ipfs-http-client": "32.0.1"
|
||||
"fetch-timeout": "^0.0.2",
|
||||
"ipfs-http-client": "32.0.1",
|
||||
"mongodb": "^3.5.2"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs')
|
||||
const fetch = require('fetch-timeout')
|
||||
const ipfsClient = require('ipfs-http-client')
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
|
||||
const ipfsUpload = async (ipfs, data) => {
|
||||
let json = JSON.stringify(data)
|
||||
let content = Buffer.from(json)
|
||||
resp = await ipfs.add(content, { pin: false })
|
||||
return resp[0].hash
|
||||
}
|
||||
|
||||
const updateDapp = async (dbCol, oldHash, newHash) => {
|
||||
return dbCol.updateOne(
|
||||
{hash: oldHash},
|
||||
{'$set': {hash: newHash}}
|
||||
)
|
||||
}
|
||||
|
||||
const main = async () => {
|
||||
const mongo = await MongoClient.connect(
|
||||
process.env.MONGODB_URI,
|
||||
{ useUnifiedTopology: true }
|
||||
)
|
||||
const db = mongo.db()
|
||||
const dbDapps = db.collection('dappsmetadatas')
|
||||
|
||||
const ipfs = ipfsClient(
|
||||
process.env.IPFS_HOST || 'ipfs.status.im',
|
||||
process.env.IPFS_PORT || 443,
|
||||
{ protocol: 'https' }
|
||||
)
|
||||
|
||||
const dapps = await dbDapps.find().toArray()
|
||||
|
||||
for (let dapp of dapps) {
|
||||
let oldHash = dapp.hash
|
||||
console.log(` * ${dapp.details.name} - ${dapp.details.url}`)
|
||||
console.log(` - OLD HASH: ${oldHash}`)
|
||||
let newHash = await ipfsUpload(ipfs, dapp.details)
|
||||
console.log(` - NEW HASH: ${newHash}`)
|
||||
if (oldHash == newHash) {
|
||||
console.log(' - MATCHING')
|
||||
continue
|
||||
}
|
||||
|
||||
let rval = await updateDapp(dbDapps, oldHash, newHash)
|
||||
if (rval.result.ok != 1) {
|
||||
console.log(' ! FAILURE')
|
||||
}
|
||||
console.log(' ! UPDATED')
|
||||
}
|
||||
|
||||
await mongo.close()
|
||||
}
|
||||
|
||||
main()
|
Loading…
Reference in New Issue