2019-06-03 21:01:42 +03:00
|
|
|
const ipfsClient = require('ipfs-http-client');
|
2019-08-05 12:36:32 -04:00
|
|
|
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) {
|
2019-08-05 12:36:32 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:08:04 +01:00
|
|
|
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) {
|
2020-01-21 15:08:04 +01:00
|
|
|
return this.addContent(content);
|
2019-06-03 21:01:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new IPFSService();
|