1
0
mirror of https://github.com/dap-ps/discover.git synced 2025-02-25 07:25:33 +00:00
discover/back-end/services/ipfs-service.js

40 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2019-06-03 21:01:42 +03:00
const ipfsClient = require('ipfs-http-client');
const logger = require('../logger/logger').getLoggerFor('IPFS-Service');
const config = require('../config')
2019-06-03 21:01:42 +03:00
class IPFSService {
constructor() {
if (!IPFSService.instance) {
this.storage = ipfsClient(
config.IPFS_HOST,
config.IPFS_PORT,
{ protocol: config.IPFS_PROTOCOL }
)
2019-06-03 21:01:42 +03:00
IPFSService.instance = this;
}
return IPFSService.instance;
}
async addContent(content, filename='data.json') {
let data
if (Buffer.isBuffer(content)) {
data = content
} else if (typeof content == "object") {
data = Buffer.from(JSON.stringify(content));
} else {
data = Buffer.from(content);
}
const resp = await this.storage.add(data, {pin: true});
logger.info(`Content uploaded to IPFS: ${resp[0].hash}`);
return resp[0].hash;
2019-06-03 21:01:42 +03:00
}
async generateContentHash(content) {
return this.addContent(content);
2019-06-03 21:01:42 +03:00
}
}
module.exports = new IPFSService();