Commit ac5c7624 by Fernando Martínez Santiago Committed by Arturo Montejo Ráez

upload sound on server side fixed

parent 426ea672
......@@ -1200,28 +1200,34 @@ module.exports = {
upload_sound: function (req, res) {
var soundFileName;
var soundDirectory = sails.config.pictogram.paths.pictoSoundDirectory;
soundFileName = sails.config.pictogram.paths.getCustomPictoSoundFilename(req.params.id_stu_picto);
soundFileName = sails.config.pictogram.paths.getCustomPictoSoundFilename(req.params.id_stu, req.params.id_picto);
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.params.id_stu_picto })
.then((sp) => {
if (!sp)
throw Error("Not found");
sp.attributes.uri_sound = soundFileName;
StuPicto.findOne({ student: req.params.id_stu, picto: req.params.id_picto})
.then((sp) => {
if (!sp)
throw Error("Not found");
sp.attributes.uri_sound = sails.config.pictogram.urls.getSoundUrl(soundFileName);
sp.save(function (err) {
if (err) throw err;
return res.ok(sp);
});
})
.catch(err => {
fs.unlink(uploadedFiles[0].fd);
return res.serverError("Error uploading " + err);
});
......
/* global sails */
var path = require('path');
var ASSETS_PATH = path.join(__dirname, '..', 'assets');
var UPLOAD_PATH = path.join(__dirname, '..', '..', 'upload');
......@@ -53,6 +54,14 @@ module.exports.pictogram = {
*/
getSupervisorCustomPictoUrl: function (filename) {
return `/upload/supervisorCustomPicto/${filename}`;
},
/**
* Gets the public url of a sound for a given picto
* @param {String} filename filename of sound
* @return {String} Public url of the picto sound
*/
getSoundUrl: function (filename) {
return `/upload/pictoSound/${filename}`;
}
},
......@@ -63,8 +72,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'),
pictoSoundDirectory: path.join(UPLOAD_PATH, 'pictoSound'),
/**
* Get a random name used for uploaded file names
* @param {string} randomString String used for generating the name
......@@ -137,10 +145,10 @@ module.exports.pictogram = {
* @param {supervisorId} supervisorId supervisorId
* @return {string} fileName
*/
getCustomPictoSoundFilename: function (supervisorId) {
getCustomPictoSoundFilename: function (studentId,pictoId) {
return sails.config.pictogram.paths._getRandomSoundFileName(
'SUPERVISOR_CUSTOM_PICTO',
supervisorId
'PICTO_SOUND',
studentId+'_'+pictoId
);
}
},
......
......@@ -96,7 +96,7 @@ module.exports.routes = {
'POST /stu/login': 'StudentController.login',
'POST /stu': 'StudentController.create',
'POST /stu/upload': 'StudentController.upload',
'POST /stu/upload/:id_stu_picto': 'StudentController.upload_sound',
'POST /stu/:id_stu/upload_sound/: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