mirror of
https://github.com/dap-ps/discover.git
synced 2025-03-04 02:40:38 +00:00
use our IPFS cluster at ipfs.status.im for uploads (#72)
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
f38689cccd
commit
9dee93c077
@ -17,8 +17,8 @@ const config = {
|
||||
ADMIN_USER : env.ADMIN_USER || "admin",
|
||||
ADMIN_PASSWORD : env.ADMIN_PASSWORD || "discoverbancor",
|
||||
/* IPFS */
|
||||
IPFS_HOST : env.IPFS_HOST || "ipfs.infura.io",
|
||||
IPFS_PORT : env.IPFS_PORT || "5001",
|
||||
IPFS_HOST : env.IPFS_HOST || "ipfs.status.im",
|
||||
IPFS_PORT : env.IPFS_PORT || "443",
|
||||
IPFS_PROTOCOL : env.IPFS_PROTOCOL || "https",
|
||||
/* Blockchain */
|
||||
DISCOVER_CONTRACT : env.DISCOVER_CONTRACT || "0x02d990A1C66e4Cf00bCdD98a0196149F7DdA2065",
|
||||
|
@ -141,7 +141,8 @@ class DAppsMetadataController {
|
||||
if (dappMetadata) {
|
||||
dappMetadata.status = DAPP_METADATA_STATUSES.APPROVED
|
||||
|
||||
dappMetadata.ipfsHash = await IPFSService.addContent(dappMetadata.details)
|
||||
var json = JSON.stringify(dappMetadata.details);
|
||||
dappMetadata.ipfsHash = await IPFSService.addContent(json)
|
||||
|
||||
await dappMetadata.save()
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
let mongoose = require('mongoose');
|
||||
let Schema = mongoose.Schema;
|
||||
|
||||
const logger = require('../logger/logger').getLoggerFor('DAPPS-Images-Model');
|
||||
const IPFSService = require('./../services/ipfs-service');
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
let DAppsImageSchema = new Schema({
|
||||
id: Schema.Types.ObjectId,
|
||||
@ -13,21 +13,21 @@ let DAppsImageSchema = new Schema({
|
||||
});
|
||||
|
||||
DAppsImageSchema.pre('save', async function () {
|
||||
const formatedContent = JSON.stringify(this.content).split('base64,')[1];
|
||||
|
||||
if (!formatedContent) {
|
||||
const content = this.content.split('base64,')[1];
|
||||
if (!content) {
|
||||
throw new Error('Invalid base64 image');
|
||||
}
|
||||
|
||||
const hash = await IPFSService.generateContentHash(formatedContent);
|
||||
this.set({ content: formatedContent, hash: hash });
|
||||
const data = Buffer.from(content, 'base64');
|
||||
const hash = await IPFSService.addContent(data);
|
||||
this.set({ content, hash });
|
||||
});
|
||||
|
||||
DAppsImageSchema.statics.findByContent = async function (content) {
|
||||
const formatedContent = JSON.stringify(content).split('base64,')[1];
|
||||
const hash = await IPFSService.generateContentHash(formatedContent);
|
||||
const content = content.split('base64,')[1];
|
||||
const data = Buffer.from(content, 'base64');
|
||||
const hash = await IPFSService.generateContentHash(data);
|
||||
|
||||
return this.findOne({ hash: hash });
|
||||
return this.findOne({ hash });
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('DAppsImage', DAppsImageSchema);
|
||||
|
@ -66,13 +66,13 @@ let DAppsMetadataSchema = new Schema({
|
||||
});
|
||||
|
||||
DAppsMetadataSchema.pre('save', async function () {
|
||||
const hash = await IPFSService.generateContentHash(this.details);
|
||||
this.set({ hash: hash });
|
||||
const hash = await IPFSService.addContent(this.details);
|
||||
this.set({ hash });
|
||||
});
|
||||
|
||||
DAppsMetadataSchema.statics.findByPlainMetadata = async function (metadata) {
|
||||
const hash = await IPFSService.generateContentHash(metadata);
|
||||
return this.findOne({ hash: hash });
|
||||
return this.findOne({ hash });
|
||||
}
|
||||
|
||||
DAppsMetadataSchema.statics.findByBytes32Hash = async function (bytes32Hash) {
|
||||
|
@ -17,17 +17,22 @@ class IPFSService {
|
||||
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 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;
|
||||
}
|
||||
|
||||
async generateContentHash(content) {
|
||||
const contentHash = await this.storage.add(Buffer.from(JSON.stringify(content)), { onlyHash: true });
|
||||
return contentHash[0].hash;
|
||||
return this.addContent(content);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user