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
Jakub Sokołowski f7c0be8eb6
add config/index.js for central config management
Signed-off-by: Jakub Sokołowski <jakub@status.im>
2019-08-05 12:36:32 -04:00

35 lines
1.0 KiB
JavaScript

const ipfsClient = require('ipfs-http-client');
const logger = require('../logger/logger').getLoggerFor('IPFS-Service');
const config = require('../config')
class IPFSService {
constructor() {
if (!IPFSService.instance) {
this.storage = ipfsClient(
config.IPFS_HOST,
config.IPFS_PORT,
{ protocol: config.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();