Commit c998179b authored by priit's avatar priit
Browse files

Ressursi piirang

parent 9f4ad001
...@@ -15,21 +15,22 @@ router.post('/', function ( req, res ) { ...@@ -15,21 +15,22 @@ router.post('/', function ( req, res ) {
var serviceRequest = new ServiceRequest( req.body, req.files ); var serviceRequest = new ServiceRequest( req.body, req.files );
if(!serviceRequest.isValid()){ serviceRequest.isValid(function (err, isValid) {
logger.debug('Invalid request');
res.send(serviceRequest.getMessages());
return;
}
logger.debug(serviceRequest ); if(err || !isValid){
logger.debug('Invalid request');
wrapperService.execute( serviceRequest, function (err, data) { res.send(serviceRequest.getMessages());
if(err){ return;
logger.debug('Got error', err);
return res.send({errors: err});
} }
logger.debug('Send response');
res.send(data); wrapperService.execute( serviceRequest, function (err, data) {
if(err){
logger.debug('Got error', err);
return res.send({errors: err});
}
logger.debug('Send response');
return res.send(data);
});
}); });
}); });
......
var logger = require('log4js').getLogger('service_request'); var logger = require('log4js').getLogger('service_request');
var config = require('../../config'); var config = require('../../config');
var fs = require('fs');
var async = require('async');
function ServiceRequest( requestBody, requestFiles ) { function ServiceRequest( requestBody, requestFiles ) {
var self = this; var self = this;
...@@ -8,10 +10,11 @@ function ServiceRequest( requestBody, requestFiles ) { ...@@ -8,10 +10,11 @@ function ServiceRequest( requestBody, requestFiles ) {
this.files = requestFiles; this.files = requestFiles;
var messages = null; var messages = null;
this.isValid = function(){ this.isValid = function( cb ){
self._mapParams(); self._mapParams();
self._checkFiles(); self._checkFiles(function (err) {
return messages == null; cb(err, messages == null);
});
}; };
this.setMessage = function (key, value) { this.setMessage = function (key, value) {
...@@ -55,14 +58,48 @@ function ServiceRequest( requestBody, requestFiles ) { ...@@ -55,14 +58,48 @@ function ServiceRequest( requestBody, requestFiles ) {
} }
}; };
this._checkFiles = function () { this._checkFiles = function ( cb ) {
for( var fileId in config.wrapper.requestConf.requestFiles ){ async.forEachOf(config.wrapper.requestConf.requestFiles, function (requestFile, fileId, innerCb) {
if(!self.files[fileId]){
logger.debug( requestFile );
logger.debug( fileId );
var fileItem = self.files[fileId];
if(!fileItem){
logger.error('Nõutud faili ei saadetud' + fileId, self.files); logger.error('Nõutud faili ei saadetud' + fileId, self.files);
self.setMessage(fileId, 'Nõutud faili ei saadetud'); self.setMessage(fileId, 'Nõutud faili ei saadetud');
return innerCb();
} }
}
if(requestFile.isList == false && Array.isArray( fileItem )){
self.setMessage(fileId, 'Lubatud on ainult 1 fail');
return innerCb();
}
if(requestFile.sizeLimit > 0){
var limitLeft = requestFile.sizeLimit;
if( Array.isArray( fileItem ) ){
for(i in fileItem){
var fileItemItem = fileItem[i];
var stat = fs.statSync(fileItemItem.path);
limitLeft = limitLeft - stat.size;
}
} else {
var stat = fs.statSync(fileItem.path);
limitLeft = limitLeft - stat.size;
}
if(limitLeft < 0){
self.setMessage(fileId, 'Saadetud failide mahu piirang ületati');
}
}
return innerCb();
}, cb);
}; };
this.getMessages = function(){ this.getMessages = function(){
......
...@@ -21,6 +21,7 @@ var DaoService = function(){ ...@@ -21,6 +21,7 @@ var DaoService = function(){
this.client.set(prefix + key, JSON.stringify(value), function (err, reply) { this.client.set(prefix + key, JSON.stringify(value), function (err, reply) {
if(cb != undefined){ if(cb != undefined){
self.client.expire(prefix + key, config.wrapper.sessionMaxLifetime );
cb(err, reply); cb(err, reply);
} }
}); });
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment