UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

364 lines (320 loc) 13.6 kB
var logger = require('../logger'); var api = require('../api'); var auth = require('../auth'); var config = require('../config'); var multiparty = require('multiparty'); var path_mod = require('path'); var fs = require('fs'); var moment = require('moment'); var cms = require("sheercms"); var util = require('../utilities'); /* callback: (err) */ module.exports.init = function(server, callback) { //routes server.get('/cms/content/items', auth.authenticate, routeContentGetItems); server.get('/cms/content/itemlist', auth.authenticate, routeContentGetItemList); server.get('/cms/content/recent', auth.authenticate, routeContentGetRecent); server.get('/cms/content/get', auth.authenticate, routeContentGet); server.get('/cms/content/tags', auth.authenticate, routeContentTags); server.post('/cms/content/create', auth.authenticate, routeContentCreate); server.post('/cms/content/command', auth.authenticate, routeContentCommand); server.post('/cms/content/upload', auth.authenticate, routeContentUpload); server.post('/cms/content/change-dates', auth.authenticate, routeContentChangeDates); //make sure the temp/uploads folder exists createTempUploadsFolder(function(err) { callback(err); }); }; function createTempUploadsFolder(callback) { var uploadDir = path_mod.join(cms.getRoot(), 'temp/uploads'); var msg = "Ensuring the temp/uploads folder exists: " + uploadDir; logger.info(msg); util.ensureDirectoryExists(uploadDir, callback); } function routeContentGetItems(req, res, next) { try { var pageNum = req.query.page; var pageSize = req.query.size; var groupId = req.query.groupId; var category = req.query.category; var start = req.query.start && !isNaN(req.query.start) ? new Date(parseInt(req.start)) : null; var end = req.query.end && !isNaN(req.query.start) ? new Date(parseInt(req.end)) : null; var options = { groupId: groupId, category: category, pageNumber: pageNum, pageSize: pageSize, start: start, end: end }; api.content.getItems(options, function(err, items, total) { if (err) { res.json({ success: false, message: 'Error retrieving the items.'}); } else { logger.info("Found " + total + " total content items."); res.json({ success: true, total: total, items: items }); } }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error retrieving the items.'}); } }; function routeContentGetRecent(req, res, next) { try { var count = req.query.count; var groupId = req.query.groupId; api.content.getRecentItems(groupId, count, function(err, items) { if (err || !items) { res.json({ success: false, message: 'Error retrieving the items.'}); } else { logger.info("Found " + items.length + " recent items in group: " + groupId); res.json({ success: true, items: items }); } }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error retrieving the items.'}); } } function routeContentGetItemList(req, res, next) { try { var idList = req.query.ids; var ids = (idList && idList.length > 0) ? idList.split(',') : []; api.content.getItemList(ids, "drafts", function(err, items) { if (err) { res.json({ success: false, message: 'Error retrieving the items.'}); } else { res.json({ success: true, items: items }); } }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error retrieving the items.'}); } }; function routeContentTags(req, res, next) { try { api.tags.getAll(function(err, tags) { if (err) res.json({ success: false, message: 'Error retrieving the tag list.'}); else res.json({ success: true, tags: tags }); }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error retrieving the tag list.'}); } }; function routeContentGet(req, res, next) { try { var contentId = req.query.contentId; var collection = req.query.collection || 'drafts'; var editData = req.query.editData == "true"; api.content.getContent({contentId: contentId, collection: collection}, function(err, item) { if (err) { res.json({ success: false, message: 'Error retrieving the content item.'}); } else { if (item && editData === true) { //load the group api.groups.getGroup(item.groupId, function(err, group) { if (err) { res.json({ success: false, message: 'Error retrieving the item group.'}); } else { var rights = api.groups.getGroupRights(group, req.user); res.json({ success: true, item: item, group: group, rights: rights }); } }); } else { res.json({ success: true, item: item }); } } }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error retrieving the content item.'}); } }; function routeContentCreate(req, res, next) { try { var name = req.body.name; var typeId = req.body.typeId; var templateId = req.body.templateId; var groupId = req.body.groupId; var username = req.user.username; var start = req.body.start; var end = req.body.end; var startDate, endDate; if (start && end && start.length > 0 && end.length > 0) { startDate = moment(start).toDate(); endDate = moment(end).toDate(); } api.content.createContent({name: name, groupId: groupId, typeId: typeId, templateId: templateId, username: username, startDate: startDate, endDate: endDate}, function(err, item) { if (err) { var msg = err.toString().replace("ValidationError:", "Error:"); res.json({ success: false, message: msg}); } else { logger.info("Content item created: " + item.contentId); res.json({ success: true, contentId: item.contentId }); } }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error creating the content item.'}); } }; function routeContentCommand(req, res, next) { var cmd = req.body.command; try { var json = req.body.data; var data = JSON.parse(json); var username = req.user.username; if (data.batch == true) { api.content.runBatchCommand(cmd, data.options, data.ids, username, function(err) { if (err) res.json({ success: false, message: 'Error executing the batch content command: ' + cmd}); else { logger.info("Batch content command executed: cmd=" + cmd); res.json({ success: true, message: 'Content command executed successfully.' }); } }); } else { api.content.runCommand(cmd, data.options, data.item, username, function (err, draftItem) { if (err) res.json({ success: false, message: err.message }); else { logger.info("Content command executed: id=" + data.item.contentId + "; cmd=" + cmd); res.json({ success: true, message: 'Content command executed successfully.', item: draftItem }); } }); } } catch(e) { logger.error(e); res.json({ success: false, message: 'Error executing content command: ' + cmd}); } }; function routeContentUpload(req, res, next) { try { var username = req.user.username; var uploadDir = path_mod.join(cms.getRoot(), 'temp/uploads'); logger.info("Using upload directory: " + uploadDir); // parse a file upload var form = new multiparty.Form({ maxFilesSize: 20 * 1024 * 1024, //20 MB uploadDir: uploadDir }); var count = 0; form.parse(req, function(err, fields, files) { try { if (err) { logger.error(err); res.json({ success: false, message: err.message}); } else { var publish = (fields.publish && fields.publish.length > 0 && (fields.publish[0] == true || fields.publish[0] == "true")); var contentId = (fields.contentId && fields.contentId.length > 0) ? fields.contentId[0] : null; var groupId = (fields.groupId && fields.groupId.length > 0) ? fields.groupId[0] : null; if (!groupId) throw new Error("The groupId is not defined."); var fileArr = files.file; //only one file uploaded at a time if (fileArr && fileArr.length > 0) { var file = fileArr[0]; logger.info("File uploaded to: " + file.path); var opt = { groupId: groupId, name: file.originalFilename, path: file.path, size: file.size, username: username, publish: publish, contentId: contentId //if exists, it will overwrite the media item on this content item }; if (contentId) { api.content.updateMedia(opt, function (err, mediaItem) { if (err) { res.json({ success: false, message: err.message}); } else { res.json({ success: true, message: "Media updated successfully.", item: mediaItem }); } }); } else { api.content.createMedia(opt, function (err, mediaItem) { if (err) { res.json({ success: false, message: err.message}); } else { res.json({ success: true, message: "Media uploaded successfully.", item: mediaItem }); } }); } } } } catch(e) { logger.error(e); res.json({ success: false, message: 'Error uploading the media.'}); } }); } catch(e) { logger.error(e); res.json({ success: false, message: 'Error uploading the media.'}); } }; function routeContentChangeDates(req, res, next) { try { var name = req.body.name; var contentId = req.body.contentId; var username = req.user.username; var start = req.body.start; var end = req.body.end; var startDate, endDate; if (start && end && start.length > 0 && end.length > 0) { startDate = moment(start).toDate(); endDate = moment(end).toDate(); } logger.info("Updating content dates: contentId=" + contentId + "; start=" + start + "; end=" + end); api.content.getContent({ contentId: contentId, collection: 'drafts'}, function(err, item) { if (err) res.json({ success: false, message: "Error updating the content dates: " + err, contentId: contentId }); else if (item) { item.startDate = startDate; item.endDate = endDate; api.content.updateContent(item, username, function(err, resultItem) { if (err) { res.json({ success: false, message: "Error updating the content dates: " + err, contentId: contentId }); } else { res.json({ success: true, contentId: contentId }); } }); } else { logger.error(new Error("routeContentChangeDates - content not found: " + contentId)); res.json({ success: false, message: "Content not found.", contentId: contentId }); } }); } catch(e) { logger.error(e); res.json({ success: false, message: "Error updating the content dates.", contentId: contentId }); } };