let express = require('express') var multer = require('multer') var upload = multer({ dest: '../staticFiles/' }) //@TODO make this a global value let router = express.Router() let Attachment = require('@models/Attachment') let Note = require('@models/Note') let userId = null let masterKey = null // middleware that is specific to this router router.use(function setUserId (req, res, next) { //Session key is required to continue if(!req.headers.sessionId){ next('Unauthorized') } if(req.headers.userId){ userId = req.headers.userId masterKey = req.headers.masterKey next() } }) router.post('/search', function (req, res) { Attachment.search(userId, req.body.noteId, req.body.attachmentType, req.body.offset, req.body.setSize, req.body.includeShared) .then( data => res.send(data) ) }) router.post('/textsearch', function (req, res) { Attachment.textSearch(userId, req.body.searchTerm) .then( data => res.send(data) ) }) router.post('/update', function (req, res) { Attachment.update(userId, req.body.attachmentId, req.body.updatedText, req.body.noteId) .then( result => { Note.reindex(userId, req.body.noteId) .then( data => res.send(data) ) }) }) router.post('/delete', function (req, res) { Attachment.delete(userId, req.body.attachmentId) .then( data => res.send(data) ) }) router.post('/upload', upload.single('file'), function (req, res, next) { //Create attachment with file information and node id const noteId = parseInt(req.body.noteId) Attachment.processUploadedFile(userId, noteId, req.file) .then( uploadResults => { res.send(uploadResults) }) }) // // Push URL to attachments // push action on - public controller // // get push key router.post('/getbookmarklet', function (req, res) { Attachment.getPushkeyBookmarklet(userId) .then( data => res.send(data) ) }) // generate new push key router.post('/generatepushkey', function (req, res) { }) // delete push key router.post('/deletepushkey', function (req, res) { }) module.exports = router