Spaces:
Runtime error
Runtime error
File size: 4,814 Bytes
56b6519 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
module.exports = function (app) {
var Response = require('../lib/httpResponse.js');
var Template = require('mongoose').model('Template');
var acl = require('../lib/auth').acl;
var utils = require('../lib/utils');
var fs = require('fs');
// Get templates list
app.get(
'/api/templates',
acl.hasPermission('templates:read'),
function (req, res) {
Template.getAll()
.then(msg => Response.Ok(res, msg))
.catch(err => Response.Internal(res, err));
},
);
// Create template
app.post(
'/api/templates',
acl.hasPermission('templates:create'),
function (req, res) {
if (!req.body.name || !req.body.file || !req.body.ext) {
Response.BadParameters(
res,
'Missing required parameters: name, ext, file',
);
return;
}
if (
!utils.validFilename(req.body.name) ||
!utils.validFilename(req.body.ext)
) {
Response.BadParameters(res, 'Bad name or ext format');
return;
}
var template = {};
// Required parameters
template.name = req.body.name;
template.ext = req.body.ext;
Template.create(template)
.then(data => {
var fileBuffer = Buffer.from(req.body.file, 'base64');
fs.writeFileSync(
`${__basedir}/../report-templates/${template.name}.${template.ext}`,
fileBuffer,
);
Response.Created(res, data);
})
.catch(err => Response.Internal(res, err));
},
);
// Update template
app.put(
'/api/templates/:templateId',
acl.hasPermission('templates:update'),
function (req, res) {
if (req.body.name && !utils.validFilename(req.body.name)) {
Response.BadParameters(res, 'Bad name format');
return;
}
var template = {};
// Optional parameters
if (req.body.name) template.name = req.body.name;
if (req.body.file && req.body.ext) template.ext = req.body.ext;
Template.update(req.params.templateId, template)
.then(data => {
// Update file only
if (!req.body.name && req.body.file && req.body.ext) {
var fileBuffer = Buffer.from(req.body.file, 'base64');
try {
fs.unlinkSync(
`${__basedir}/../report-templates/${data.name}.${data.ext || 'docx'}`,
);
} catch {}
fs.writeFileSync(
`${__basedir}/../report-templates/${data.name}.${req.body.ext}`,
fileBuffer,
);
}
// Update name only
else if (req.body.name && !req.body.file) {
fs.renameSync(
`${__basedir}/../report-templates/${data.name}.${data.ext || 'docx'}`,
`${__basedir}/../report-templates/${req.body.name}.${data.ext || 'docx'}`,
);
}
// Update both name and file
else if (req.body.name && req.body.file && req.body.ext) {
var fileBuffer = Buffer.from(req.body.file, 'base64');
try {
fs.unlinkSync(
`${__basedir}/../report-templates/${data.name}.${data.ext || 'docx'}`,
);
} catch {}
fs.writeFileSync(
`${__basedir}/../report-templates/${req.body.name}.${req.body.ext}`,
fileBuffer,
);
}
Response.Ok(res, 'Template updated successfully');
})
.catch(err => {
if (err.code && err.code === 'ENOENT')
Response.NotFound(res, 'Template File was not Found');
else Response.Internal(res, err);
});
},
);
// Delete template
app.delete(
'/api/templates/:templateId',
acl.hasPermission('templates:delete'),
function (req, res) {
Template.delete(req.params.templateId)
.then(data => {
fs.unlinkSync(
`${__basedir}/../report-templates/${data.name}.${data.ext || 'docx'}`,
);
Response.Ok(res, 'Template deleted successfully');
})
.catch(err => {
if (err.code && err.code === 'ENOENT')
Response.Ok(
res,
'Template File not found but deleted successfully in database',
);
else Response.Internal(res, err);
});
},
);
// Download template file
app.get(
'/api/templates/download/:templateId',
acl.hasPermission('templates:read'),
function (req, res) {
Template.getOne(req.params.templateId)
.then(data => {
var file = `${__basedir}/../report-templates/${data.name}.${data.ext || 'docx'}`;
res.download(file, `${data.name}.${data.ext}`);
})
.catch(err => Response.Internal(res, err));
},
);
};
|