Commit f3912024 authored by taivo's avatar taivo
Browse files

KEEL-59

parent 52594a82
......@@ -57,6 +57,10 @@ config.availableWappers = {
MORFYHESTAJA : {
class: 'morfyhestaja',
commandTemplate: '/home/priit/Downloads/morfyhestaja/./parse.sh [data]'
},
ARCHIVE_EXTRACTOR: {
class: 'archiveExtractor',
commandTemplate: ''
}
};
......
......@@ -37,11 +37,13 @@ router.get('/:sessionId/:fileId', function(req, res) {
logger.debug({request:'Get file', instance: req.params.sessionId, file: req.params.fileId});
wrapperService.getServiceFile(req.params.sessionId, req.params.fileId, function (err, filePath) {
wrapperService.getServiceFile(req.params.sessionId, req.params.fileId, function (err, outputFile) {
if(err){
return res.status(404).send({errors: err});
}
var filePath = outputFile.filePath;
var stat = fs.statSync(filePath);
res.writeHead(200,{
'Content-Length': stat.size
......
......@@ -14,16 +14,23 @@
"express": "~4.12.4",
"fs": "0.0.2",
"graceful-fs": "^4.1.2",
"is-gzip": "^1.0.0",
"is-tar": "^1.0.0",
"is-zip": "^1.0.0",
"jade": "~1.9.2",
"log4js": "^0.6.26",
"mime": "^1.3.4",
"morgan": "~1.5.3",
"multer": "0.1.6",
"node-schedule": "^0.2.9",
"nodemailer": "^1.4.0",
"randomstring": "^1.0.5",
"recursive-readdir": "^1.2.1",
"redis": "^0.12.1",
"request": "^2.57.0",
"rimraf": "^2.4.0"
"rimraf": "^2.4.0",
"tar": "^2.1.1",
"unzip": "^0.1.11"
},
"devDependencies": {
"assert": "^1.3.0",
......
......@@ -32,13 +32,15 @@ var CleanerService = function () {
fs.readdir(folder, function(err, files) {
console.log('readFolder');
if(files.length > 0){
if(files != undefined && files.length > 0){
self.checkOnIndex(0, files, folder, function (err) {
if(err){
return logger.error(err);
}
return logger.info('Cleaned');
});
} else {
return logger.info('Cleaned');
}
});
};
......
......@@ -183,9 +183,9 @@ function SessionService() {
response.data = session.data;
}
var filesList = [];
for(i in session.outputFiles){
filesList.push(i);
var filesList = {};
for(var i in session.outputFiles){
filesList[i] = { fileName: session.outputFiles[i].fileName, contentType: session.outputFiles[i].contentType };
}
response.data.files = filesList;
......
......@@ -49,13 +49,13 @@ function WrapperService() {
return callback(err);
}
var path = session.outputFiles[fileId];
var outputFile = session.outputFiles[fileId];
if(!path){
if(!outputFile){
return callback('Dokumenti ei leitud');
}
callback(null, path);
callback(null, outputFile);
});
};
}
......
var fs = require('fs');
var unzip = require('unzip');
var mkdirp = require('mkdirp');
var path = require('path');
var SessionService = require('./../src/service/sessionService');
var Session = require('../src/model/session');
var randomstring = require('randomstring');
var logger = require('log4js').getLogger('wrapper');
var mime = require('mime');
var isZip = require('is-zip');
var isGzip = require('is-gzip');
var isTar = require('is-tar');
var zlib = require('zlib');
var tar = require('tar');
var recursiveReaddir = require('recursive-readdir');
function ArchiveExtractor() {
var self = this;
this.process = function(session, callback) {
var zipFile = session.requestFiles.resourceFile;
var read = fs.readFileSync(zipFile);
var zip = isZip(read);
var gzip = isTar(read);
if (isZip(read)) {
return self._extractZipFile(zipFile, session, callback);
} else if (isGzip(read)) {
return self._extractGzipFile(zipFile, session, callback);
} else if (isTar(read)) {
return self._extractTarFile(zipFile, session, callback);
} else {
session.message = Session.messages.ERROR;
return callback('Not supported archive file format.', session);
}
};
this._extractZipFile = function(file, session, callback) {
var count = 0;
//logger.debug('Extracting *.zip file');
fs.createReadStream(file).on('error', function(error) {
session.setErrors(error);
return callback(error, session);
})
.pipe(unzip.Parse())
.on('entry', function(entry) {
var savePath = SessionService.getStorePath(session.id);
var isFile = ('File' == entry.type);
var fullpath = path.join(savePath, entry.path);
//logger.debug('Fullpath: ' + fullpath);
var directory = (isFile ? path.dirname(fullpath) : fullpath);
if (isFile) {
count++;
}
//logger.debug('Countx: ' + count);
mkdirp(directory, function(err) {
if (err) {
// log err
session.setErrors(err);
return callback(err, session);
}
if (isFile) {
var uniqid = randomstring.generate(10);
var filePath = path.join(directory, uniqid);
//logger.debug('Filepath: ' + filePath);
entry.pipe(fs.createWriteStream(filePath)).on('close', function() {
session.addOutputFile(uniqid, {
filePath: filePath,
fileName: entry.path,
contentType: mime.lookup(fullpath) // getting the original file mime type
});
count--;
//logger.debug('Countx: ' + count);
if (count == 0) {
session.message = Session.messages.OK;
}
});
} else {
entry.autodrain();
}
});
})
.on('close', function() {
//logger.debug('Count: ' + count);
if (count == 0) {
session.message = Session.messages.OK;
}
return callback(null, session);
});
};
this._extractGzipFile = function(file, session, callback) {
//logger.debug('Extracting *.tar.gz file');
var savePath = SessionService.getStorePath(session.id);
fs.createReadStream(file).pipe(zlib.Unzip())
.pipe(tar.Extract({
path: savePath
})).on('end', function() {
recursiveReaddir(savePath, [file], function(err, files) {
if (err) {
session.setErrors(err);
return callback(err, session);
}
for (var i = 0; i < files.length; i++) {
var uniqid = randomstring.generate(10);
var filePath = files[i];
var fileName = files[i].substring(filePath.indexOf(session.id) + session.id.length + 1);
session.addOutputFile(uniqid, {
filePath: filePath,
fileName: fileName,
contentType: mime.lookup(filePath) // getting the original file mime type
});
}
session.message = Session.messages.OK;
return callback(null, session);
});
});
};
this._extractTarFile = function(file, session, callback) {
//logger.debug('Extracting *.tar file');
var savePath = SessionService.getStorePath(session.id);
fs.createReadStream(file)
.pipe(tar.Extract({
path: savePath
})).on('end', function() {
recursiveReaddir(savePath, [file], function(err, files) {
if (err) {
session.setErrors(err);
return callback(err, session);
}
for (var i = 0; i < files.length; i++) {
var uniqid = randomstring.generate(10);
var filePath = files[i];
var fileName = files[i].substring(filePath.indexOf(session.id) + session.id.length + 1);
session.addOutputFile(uniqid, {
filePath: filePath,
fileName: fileName,
contentType: mime.lookup(filePath) // getting the original file mime type
});
}
session.message = Session.messages.OK;
return callback(null, session);
});
});
}
}
module.exports = ArchiveExtractor;
\ No newline at end of file
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