1
0
mirror of https://github.com/dap-ps/discover.git synced 2025-02-12 09:17:32 +00:00
discover/back-end/services/ipfs-service.js
2019-06-03 21:01:42 +03:00

30 lines
957 B
JavaScript

const ipfsClient = require('ipfs-http-client');
const logger = require('./../logger/logger').getLoggerFor('IPFS-Service');
class IPFSService {
constructor() {
if (!IPFSService.instance) {
this.storage = ipfsClient(process.env.IPFS_HOST, process.env.IPFS_PORT, { protocol: process.env.IPFS_PROTOCOL })
IPFSService.instance = this;
}
return IPFSService.instance;
}
async addContent(content) {
// Todo: pin the hash. Infura does not support it.
const contentHash = await this.storage.add(Buffer.from(JSON.stringify(content)));
logger.info(`Content ${content} was successfully uploaded in IPFS`);
return contentHash[0].hash;
}
async generateContentHash(content) {
const contentHash = await this.storage.add(Buffer.from(JSON.stringify(content)), { onlyHash: true });
return contentHash[0].hash;
}
}
module.exports = new IPFSService();