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