1
0
mirror of https://github.com/dap-ps/discover.git synced 2025-02-07 15:05:07 +00:00
discover/back-end/models/dapps-images-model.js
2019-06-03 21:01:42 +03:00

34 lines
953 B
JavaScript

let mongoose = require('mongoose');
let Schema = mongoose.Schema;
const IPFSService = require('./../services/ipfs-service');
let DAppsImageSchema = new Schema({
id: Schema.Types.ObjectId,
content: String,
hash: {
type: String,
unique: true,
}
});
DAppsImageSchema.pre('save', async function () {
const formatedContent = JSON.stringify(this.content).split('base64,')[1];
if (!formatedContent) {
throw new Error('Invalid base64 image');
}
const hash = await IPFSService.generateContentHash(formatedContent);
this.set({ content: formatedContent, hash: hash });
});
DAppsImageSchema.statics.findByContent = async function (content) {
const formatedContent = JSON.stringify(content).split('base64,')[1];
const hash = await IPFSService.generateContentHash(formatedContent);
return this.findOne({ hash: hash });
};
module.exports = mongoose.model('DAppsImage', DAppsImageSchema);