2018-12-18 18:52:59 +01:00
|
|
|
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,
|
2018-12-19 18:56:18 +01:00
|
|
|
OBJECT_POST,
|
|
|
|
OBJECT_SETTINGS
|
2018-12-18 18:52:59 +01:00
|
|
|
}
|
|
|
|
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();
|
2018-12-20 19:50:28 +01:00
|
|
|
const uploadPath = "./content/user-images/" + dateUtils.getDate('year', new Date()) + "/" + dateUtils.getDate('month', new Date());
|
2018-12-18 18:52:59 +01:00
|
|
|
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
|
2018-12-20 19:50:28 +01:00
|
|
|
}).array('avatar_upload');
|
2018-12-18 18:52:59 +01:00
|
|
|
var background_upload = multer(
|
|
|
|
{
|
|
|
|
storage: storage
|
2018-12-20 19:50:28 +01:00
|
|
|
}).array('background_upload');
|
2018-12-18 18:52:59 +01:00
|
|
|
//** SYNC POSTS */
|
|
|
|
router.post("/sync", (req, res, next) =>
|
|
|
|
{
|
|
|
|
let payload = req.body;
|
|
|
|
Models.User.findById(req.session.user.id).then((user) =>
|
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE))
|
2018-12-18 18:52:59 +01:00
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
fs.readJson('site-settings.json').then(obj =>
|
2018-12-18 18:52:59 +01:00
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
if (user.hande != payload.handle || user.email != payload.email)
|
2018-12-18 18:52:59 +01:00
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
user.update(
|
2018-12-18 18:52:59 +01:00
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
handle: payload.handle,
|
|
|
|
email: payload.email
|
|
|
|
}).then(updated =>
|
|
|
|
{
|
|
|
|
console.log("UPDATED")
|
|
|
|
}).catch(err =>
|
2018-12-18 18:52:59 +01:00
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
console.log("ERR", err);
|
2018-12-18 18:52:59 +01:00
|
|
|
})
|
2018-12-19 18:56:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.log("USER NOT UPDATED")
|
|
|
|
}
|
|
|
|
obj.url = payload.url;
|
|
|
|
obj.title = payload.title;
|
|
|
|
obj.description = payload.descriptions;
|
|
|
|
obj.private = payload.private;
|
|
|
|
obj.theme = payload.theme;
|
|
|
|
fs.writeJson('site-settings.json', obj).then(() =>
|
|
|
|
{
|
|
|
|
res.json(
|
2018-12-18 18:52:59 +01:00
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
message: DataEvent.SETTINGS_UPDATED
|
2018-12-18 18:52:59 +01:00
|
|
|
});
|
|
|
|
}).catch(err =>
|
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
console.error(err)
|
2018-12-18 18:52:59 +01:00
|
|
|
})
|
2018-12-19 18:56:18 +01:00
|
|
|
//console.log("PAYLOAD", payload);
|
|
|
|
//console.log("SETTINGS", obj);
|
|
|
|
}).catch(err =>
|
2018-12-18 18:52:59 +01:00
|
|
|
{
|
2018-12-19 18:56:18 +01:00
|
|
|
console.error(err)
|
|
|
|
})
|
2018-12-18 18:52:59 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
2018-12-20 19:50:28 +01:00
|
|
|
var avatar = req.files[0].path;
|
|
|
|
user.update(
|
|
|
|
{
|
|
|
|
avatar: avatar.substr(7, avatar.length)
|
|
|
|
}).then(updated =>
|
|
|
|
{
|
|
|
|
|
|
|
|
req.session.user = updated;
|
|
|
|
}).catch(err =>
|
|
|
|
{
|
|
|
|
console.log("ERR", err);
|
|
|
|
})
|
2018-12-18 18:52:59 +01:00
|
|
|
return res.json(
|
|
|
|
{
|
2018-12-20 19:50:28 +01:00
|
|
|
message: DataEvent.AVATAR_UPLOADED,
|
|
|
|
url: avatar.substr(7, avatar.length)
|
2018-12-18 18:52:59 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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;
|