Añadido metodo upload_sound a StudentController con las policies, routes y path establecidos

parent 252741c0
......@@ -130,7 +130,7 @@ module.exports = {
"tape_background": "#00ffff",
"delivery": 0
}
}
}
*/
getInfo: function (req, res) {
Student.findOne({id: req.params.id_stu})
......@@ -1186,6 +1186,77 @@ module.exports = {
});
},
/**
* Upload a custom sound associated to a picto
* @param {request} req
* id_stu,
* id_picto,
* attributes: { @see StuPicto.getValidAttributes() }
* @param {response} res
* {
* id: <stu_picto ID>,
* student: <student ID>,
* attributes: {
* id_cat: categoryId or null,
* coord_x: 1 .. 5 or null,
* coord_y: 1 .. 10 or null,
* free_category_coord_x: 0 .. 4 or null,
* free_category_coord_y: 0 .. 9 or null,
* status: '[invisible]/enabled/disabled',
* highlight: true/[false],
* color: any valid HEX color or [null],
* expression: 'custom expression',
* legend: true/[false],
* uri_sound: 'path of sound associated',
* user_avatar:
* },
* picto: {
* id: <pictoID>,
* source: <sourceID>,
* owner: <ownerID> or null,
* id: <pictoID>,
* uri: <URL to image>,
* category: <categoryID>
* }
* }
* }
*/
upload_sound: function (req, res) {
Supervisor.findOne({ id: req.body.owner }).then(function (supervisor) {
var soundFileName;
var soundDirectory = sails.config.pictogram.paths.pictoSoundDirectory;
if (!supervisor)
throw new Error("No supervisor found");
soundFileName = sails.config.pictogram.paths.getSupervisorCustomPictoFileName(supervisor.id)
+ "_sound";
sails.log.debug("Uploading sound with FileName: " + soundFileName);
req.file('file').upload({
maxBytes: 1048576,
dirname: soundDirectory,
saveAs: soundFileName
},function whenDone(err, uploadedFiles) {
var fs = require('fs');
if (err || (uploadedFiles.length === 0))
return res.serverError("Error uploading " + err ? err : "");
StuPicto.findOne({ id: req.body.id})
.then(soundFileName => {
return res.ok(soundFileName);
})
.catch(err => {
fs.unlink(uploadedFiles[0].fd);
return res.serverError("Error uploading " + err);
});
});
})
.catch(function (err) {
return res.serverError("Error uploading sound: " + err);
});
}
};
// ***************************************************************
// WEBSOCKETS
// ***************************************************************
......
......@@ -55,6 +55,7 @@ module.exports.pictogram = {
supervisorAvatarDirectory: path.join(UPLOAD_PATH, 'supervisorAvatar'),
studentAvatarDirectory: path.join(UPLOAD_PATH, 'studentAvatar'),
supervisorCustomPictoDirectory: path.join(UPLOAD_PATH, 'supervisorCustomPicto'),
pictoSoundDirectory: path.join(UPLOAD_PATH, 'pictoSound'),
/**
* Get a random name used for uploaded file names
......
......@@ -105,6 +105,7 @@ module.exports.policies = {
login: true,
create: ['tokenAuth', 'isSupAdmin'],
upload: ['tokenAuth'],
upload_sound: ['tokenAuth', 'isSupervisorOfStudent'],
add_picto: ['tokenAuth', 'isSupervisorOfStudent'],
subscribe: ['tokenAuth'],
unsubscribe: true,
......
......@@ -87,6 +87,7 @@ module.exports.routes = {
'POST /stu/login': 'StudentController.login',
'POST /stu': 'StudentController.create',
'POST /stu/upload': 'StudentController.upload',
'POST /stu/:id_stu/upload/:id_picto': 'StudentController.upload_sound',
'POST /stu/:id_stu/picto/:id_picto': 'StudentController.add_picto',
'POST /stu/subscribe': 'StudentController.subscribe',
'POST /stu/unsubscribe': 'StudentController.unsubscribe',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment