mirror of
https://github.com/dap-ps/discover.git
synced 2025-02-12 09:17:32 +00:00
35 lines
1.0 KiB
JavaScript
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();
|