2019-06-03 21:01:42 +03:00
|
|
|
const DAppImage = require('./../models/dapps-images-model');
|
|
|
|
|
|
|
|
class DAppImageService {
|
|
|
|
|
|
|
|
static async upload(req, image) {
|
|
|
|
try {
|
|
|
|
const uploadedImage = await DAppImage.create({ content: image });
|
|
|
|
return buildImageUrl(req, uploadedImage.hash);
|
|
|
|
} catch (error) {
|
|
|
|
// Code 11000 is because of uniqueness, so just return the already exist document
|
|
|
|
if (error.code == 11000) {
|
|
|
|
const existingImage = await DAppImage.findByContent(image);
|
|
|
|
return buildImageUrl(req, existingImage.hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static async retrieveImage(imageHash) {
|
|
|
|
return DAppImage.findOne({ 'hash': imageHash });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const buildImageUrl = function (req, imageHash) {
|
2019-12-12 21:16:05 +01:00
|
|
|
return `/metadata/image/${imageHash}`;
|
2019-06-03 21:01:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DAppImageService;
|