Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Shapefile import when size is 0 #1622

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 28 additions & 114 deletions webapp/controllers/configuration/ImportStaticFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,21 @@
* @property {object} memberPath - 'path' module.
* @property {object} memberExportation - 'Exportation' model.
* @property {object} memberUnzip - 'unzip' module.
* @property {object} memberExec - Exec function.
* @property {object} memberExecSync - Exec function sync.
* @property {object} memberSpawn - Spawn function.
* @property {object} memberUtils - 'Utils' model.
*/
var ImportStaticFiles = function(app) {

const ImportStaticFiles = (/*app*/) => {
// 'fs' module
var memberFs = require('fs');
const memberFs = require('fs');
// 'path' module
var memberPath = require('path');
// 'Exportation' model
var memberExportation = new (require('../../core/Exportation.js'))();
const memberPath = require('path');
// 'DataManager' module
var memberDataManager = require('../../core/DataManager.js');
// 'unzip' module
var memberUnzip = require('unzip2');
// Exec function
var memberExec = require('child_process').exec;
// Exec function sync
var memberExecSync = require('child_process').execSync;
// Spawn function
var memberSpawn = require('child_process').spawn;
const memberDataManager = require('../../core/DataManager.js');
// 'Exportation' model
const memberExportation = new (require('../../core/Exportation.js'))();
// 'Utils' model
var memberUtils = require('../../core/Utils.js');
const memberUtils = require('../../core/Utils.js');

const ShapeImporter = require('./../../core/ShapeImporter');

/**
* Processes the request and returns a response.
Expand Down Expand Up @@ -65,111 +55,35 @@ var ImportStaticFiles = function(app) {
else {
var path = memberPath.join(__dirname, '../../tmp/' + filesFolder + '/' + request.files.file.name);

memberFs.writeFile(path, data, function(err) {
memberFs.writeFile(path, data, async err => {
if(err)
return sendResponse(err.toString(), folderPath);
else {
try {
memberFs.createReadStream(path).pipe(memberUnzip.Extract({ path: folderPath })).on('close', function() {
var files = memberFs.readdirSync(folderPath);
var shpName = null;
var shpCount = 0;
var shpError = null;

for(var i = 0, filesLength = files.length; i < filesLength; i++) {
var filename = memberPath.join(folderPath, files[i]);
var stat = memberFs.lstatSync(filename);

if(!stat.isDirectory() && memberPath.extname(filename) !== ".zip" && (stat.size / 1048576) > 300) {
shpError = "File is too large!";
break;
}

if(!stat.isDirectory() && memberPath.extname(filename) === ".shp") {
shpName = filename;
shpCount++;
};
}

if(shpError !== null) {
return sendResponse(shpError, folderPath);
} else if(shpName !== null) {
if(shpCount === 1) {
if(request.body.semantics === 'STATIC_DATA-postgis') {
memberExportation.getPsqlString(request.body.dataProviderId).then(function(connectionString) {
memberExportation.tableExists(request.body.tableName, request.body.dataProviderId).then(function(resultTable) {
if(resultTable.rowCount > 0 && resultTable.rows[0].table_name == request.body.tableName) {
return sendResponse("Table already exists!", folderPath);
} else {
memberExec(connectionString.exportPassword + memberExportation.shp2pgsql() + " -I -s " + request.body.srid + " -W \"" + request.body.encoding + "\" " + shpName + " " + request.body.tableName + " | " + connectionString.connectionString, function(commandErr, commandOut, commandCode) {
if(commandErr)
return sendResponse(commandErr.toString(), folderPath);

memberExportation.tableExists(request.body.tableName, request.body.dataProviderId).then(function(resultTable) {
if(resultTable.rowCount > 0 && resultTable.rows[0].table_name == request.body.tableName)
return sendResponse(null, folderPath);
else
return sendResponse(commandCode, folderPath);
}).catch(function(err) {
return sendResponse(err.toString(), folderPath);
});
});
}
}).catch(function(err) {
return sendResponse(err.toString(), folderPath);
});
}).catch(function(err) {
return sendResponse(err.toString(), folderPath);
});
} else {
var mask = request.body.mask.split("\\").join("/");
// Retrieve file information such size, permissions, etc.
const fileProperties = memberFs.lstatSync(path);

if(memberPath.extname(mask) === ".shp") {
memberDataManager.getDataProvider({ id: request.body.dataProviderId }).then(function(dataProvider) {
var dataProviderPath = dataProvider.uri.replace("file://", "");

if(memberFs.existsSync(dataProviderPath)) {
var finalFilePath = (dataProviderPath + "/" + mask).split("//").join("/");
// Check file size. TODO: Remove it, since the it should be validated on header parsing
if(!fileProperties.isDirectory() && (fileProperties.size / 1048576) > 300) {
return sendResponse('File is too large', path);
}

if(!memberFs.existsSync(finalFilePath)) {
var maskArray = mask.split("/");
const importer = new ShapeImporter(folderPath, request.body.srid, request.body.encoding);
importer.unzip(path);

if(maskArray.length > 1)
var pathCreationResult = memberExportation.createPathToFile(dataProviderPath, maskArray);
else
var pathCreationResult = {
error: null,
createdPath: dataProviderPath
};
if(request.body.semantics === 'STATIC_DATA-postgis') {
await importer.toDatabase(request.body.tableName, request.body.dataProviderId);
} else {
const mask = request.body.mask.split("\\").join("/");

if(!pathCreationResult.error) {
var createdPath = pathCreationResult.createdPath;
var newFilename = maskArray[maskArray.length - 1].replace(memberPath.extname(maskArray[maskArray.length - 1]), "");
if(memberPath.extname(mask) !== ".shp") {
return sendResponse("Invalid file name!", folderPath);
}

memberExportation.copyShpFiles(folderPath, createdPath, newFilename);
await importer.toDataProvider(request.body.dataProviderId, mask);
}

return sendResponse(null, folderPath);
} else {
return sendResponse(pathCreationResult.error, folderPath);
}
} else {
return sendResponse("File already exists!", folderPath);
}
} else {
return sendResponse("Invalid data provider path!", folderPath);
}
});
} else {
return sendResponse("Invalid file name!", folderPath);
}
}
} else {
return sendResponse("More than one shapefile found!", folderPath);
}
} else {
return sendResponse("No shapefile found!", folderPath);
}
});
return sendResponse(null, folderPath);
} catch(err) {
return sendResponse(err.toString(), folderPath);
}
Expand Down
9 changes: 6 additions & 3 deletions webapp/core/Exportation.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var Exportation = function() {

var database = (uriObject.database.charAt(0) === '/' ? uriObject.database.substr(1) : uriObject.database);

var connectionString = "psql -h " + uriObject.host + " -d " + database + " -U " + uriObject.user;
var connectionString = `psql -h ${uriObject.host} -d ${database} -U ${uriObject.user} -p ${uriObject.port}`;
var exportPassword = "export PGPASSWORD='" + uriObject.password + "';";

return resolve({
Expand Down Expand Up @@ -140,8 +140,11 @@ var Exportation = function() {
client.query("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_name = $1;", [tableName], function(err, result) {
client.end();

if(err) return reject(err.toString());
else return resolve(result);
if(err) {
return reject(err.toString())
}

return resolve(result.rows.length !== 0);
});
});
}).catch(function(err) {
Expand Down
Loading