forked from projects/fipamo
224 lines
6.5 KiB
JavaScript
224 lines
6.5 KiB
JavaScript
|
import DateUtils from '../../tools/utilities/DateUtils';
|
||
|
import StringUtils from '../../tools/utilities/StringUtils';
|
||
|
import * as DataEvent from '../../tools/events/DataEvent';
|
||
|
import RightsManager,
|
||
|
{
|
||
|
TASK_CREATE,
|
||
|
TASK_UPDATE,
|
||
|
TASK_READ,
|
||
|
TASK_DELETE,
|
||
|
OBJECT_CLIENT_ADMIN,
|
||
|
OBJECT_CLIENT_USER,
|
||
|
OBJECT_PROJECT_CLIENT,
|
||
|
OBJECT_PROJECT_FOLIO,
|
||
|
OBJECT_BOOKMARK,
|
||
|
OBJECT_POST
|
||
|
}
|
||
|
from '../../tools/utilities/RightsManager';
|
||
|
const express = require('express');
|
||
|
const router = express.Router();
|
||
|
const multer = require('multer');
|
||
|
const fs = require('fs-extra');
|
||
|
const Models = require('../../models');
|
||
|
const dateUtils = new DateUtils();
|
||
|
const rightsManager = new RightsManager();
|
||
|
const uploadPath = "./content/blog-images/" + dateUtils.getDate('year', new Date()) + "/" + dateUtils.getDate('month', new Date());
|
||
|
const Sequelize = require('sequelize');
|
||
|
const Op = Sequelize.Op;
|
||
|
const _ = require('lodash');
|
||
|
fs.ensureDir(uploadPath, function(err)
|
||
|
{
|
||
|
//console.log(err) // => null
|
||
|
// dir has now been created, including the directory it is to be placed in
|
||
|
})
|
||
|
var storage = multer.diskStorage(
|
||
|
{
|
||
|
destination: function(req, file, cb)
|
||
|
{
|
||
|
cb(null, uploadPath)
|
||
|
},
|
||
|
filename: function(req, file, cb)
|
||
|
{
|
||
|
var splice = file.originalname.split(':');
|
||
|
cb(null, splice[0]);
|
||
|
}
|
||
|
});
|
||
|
var avatar_upload = multer(
|
||
|
{
|
||
|
storage: storage
|
||
|
}).array('avatar');
|
||
|
var background_upload = multer(
|
||
|
{
|
||
|
storage: storage
|
||
|
}).array('feature_background');
|
||
|
//** SYNC POSTS */
|
||
|
router.post("/sync", (req, res, next) =>
|
||
|
{
|
||
|
let payload = req.body;
|
||
|
Models.User.findById(req.session.user.id).then((user) =>
|
||
|
{
|
||
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_UPDATE))
|
||
|
{
|
||
|
for (let index = 0; index < payload.length; index++)
|
||
|
{
|
||
|
const item = payload[index];
|
||
|
Models.FreshPost.findOne(
|
||
|
{
|
||
|
where:
|
||
|
{
|
||
|
"post":
|
||
|
{
|
||
|
[Op.contains]:
|
||
|
{
|
||
|
uuid: item.post.uuid
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}).then(found =>
|
||
|
{
|
||
|
let buffed = sanitize(item.post.plaintext,
|
||
|
{
|
||
|
allowedTags: ['del', 'a', 'iframe', 'img', ],
|
||
|
allowedAttributes:
|
||
|
{
|
||
|
a: ['href', 'name', 'target'],
|
||
|
img: ['src'],
|
||
|
iframe: ['height', 'width', 'src', 'frameborder', 'allow', 'allowfullscreen']
|
||
|
}
|
||
|
})
|
||
|
buffed = new StringUtils().decodeHTML(buffed);
|
||
|
item.post.plaintext = buffed;
|
||
|
item.post.html = md.render(buffed,
|
||
|
{
|
||
|
html: true,
|
||
|
xhtmlOut: true,
|
||
|
});
|
||
|
if (!_.isEqual(item.post, found.post))
|
||
|
{
|
||
|
found.update(item).then(updated =>
|
||
|
{
|
||
|
console.log("UPDATED", updated);
|
||
|
}).catch(err =>
|
||
|
{
|
||
|
//console.log("***ERROR***", err);
|
||
|
})
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//chilld
|
||
|
}
|
||
|
}).catch(err =>
|
||
|
{
|
||
|
//console.log("***ERRRORZ****", err);
|
||
|
Models.FreshPost.create(item).then(fresh =>
|
||
|
{
|
||
|
//console.log(fresh)
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
res.json(
|
||
|
{
|
||
|
message: "postsSynced"
|
||
|
});
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
res.json(
|
||
|
{
|
||
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
})
|
||
|
|
||
|
/***
|
||
|
UPLOAD AVATAR
|
||
|
*/
|
||
|
router.post('/add-avatar', function(req, res, next)
|
||
|
{
|
||
|
//console.log(req.body);
|
||
|
if (!req.session.user) return res.json(
|
||
|
{
|
||
|
message: "You need to be logged in, champ."
|
||
|
});
|
||
|
Models.User.findById(req.session.user.id).then((user) =>
|
||
|
{
|
||
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE))
|
||
|
{
|
||
|
avatar_upload(req, res, function(err)
|
||
|
{
|
||
|
if (err)
|
||
|
{
|
||
|
//console.log('Error in Saving Entry: ' + err);
|
||
|
res.json(
|
||
|
{
|
||
|
message: err
|
||
|
});
|
||
|
throw err;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
var postImage = req.files[0].path;
|
||
|
return res.json(
|
||
|
{
|
||
|
message: DataEvent.POST_IMAGE_ADDED,
|
||
|
url: postImage.substr(7, postImage.length)
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
res.json(
|
||
|
{
|
||
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
/***
|
||
|
UPLOAD FEATURE BACKGROUND
|
||
|
*/
|
||
|
router.post('/add-feature-background', function(req, res, next)
|
||
|
{
|
||
|
//console.log(req.body);
|
||
|
if (!req.session.user) return res.json(
|
||
|
{
|
||
|
message: "You need to be logged in, champ."
|
||
|
});
|
||
|
Models.User.findById(req.session.user.id).then((user) =>
|
||
|
{
|
||
|
if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE))
|
||
|
{
|
||
|
background_upload(req, res, function(err)
|
||
|
{
|
||
|
if (err)
|
||
|
{
|
||
|
//console.log('Error in Saving Entry: ' + err);
|
||
|
res.json(
|
||
|
{
|
||
|
message: err
|
||
|
});
|
||
|
throw err;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
var postImage = req.files[0].path;
|
||
|
return res.json(
|
||
|
{
|
||
|
message: DataEvent.FEATURE_IMAGE_ADDED,
|
||
|
url: postImage.substr(7, postImage.length)
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
res.json(
|
||
|
{
|
||
|
message: "Nah. You can't do that. Talk to the admin, sport."
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
module.exports = router;
|