1- module . exports = function ( app , security ) {
1+ module . exports = function ( app , security ) {
22 const api = require ( '../api' )
33 , access = require ( '../access' )
44 , fs = require ( 'fs-extra' )
5- , { defaultHandler : upload } = require ( '../upload' )
5+ , Zip = require ( 'adm-zip' )
6+ , { defaultHandler : upload } = require ( '../upload' )
67 , DOMParser = require ( '@xmldom/xmldom' ) . DOMParser
78 , toGeoJson = require ( '../utilities/togeojson' ) ;
89
@@ -14,29 +15,53 @@ module.exports = function(app, security) {
1415 }
1516
1617 if ( ! req . file ) {
17- return res . status ( 400 ) . send ( 'Invalid file, please upload a KML file.' ) ;
18+ return res . status ( 400 ) . send ( 'Invalid file, please upload a KML or KMZ file.' ) ;
1819 }
1920
20- fs . readFile ( req . file . path , 'utf8' , function ( err , data ) {
21- if ( err ) return next ( err ) ;
21+ const fileExtension = req . file . originalname . toLowerCase ( ) . split ( '.' ) . pop ( ) ;
2222
23- const parser = new DOMParser ( ) ;
24- const kml = parser . parseFromString ( data , "application/xml" ) ;
25- const parseError = kml . getElementsByTagName ( "parsererror" ) ;
23+ if ( fileExtension === 'kmz' ) {
24+ try {
25+ const zip = new Zip ( req . file . path ) ;
26+ const zipEntries = zip . getEntries ( ) ;
27+ const kmlEntry = zipEntries . find ( entry => entry . entryName . toLowerCase ( ) . endsWith ( '.kml' ) ) ;
2628
27- if ( parseError . length > 0 ) {
28- console . error ( "KML Parsing Error:" , parseError [ 0 ] . textContent ) ;
29- } else {
30- console . log ( "Parsed KML successfully" ) ;
31- }
29+ if ( ! kmlEntry ) {
30+ return res . status ( 400 ) . send ( 'No KML file found inside.' ) ;
31+ }
3232
33- if ( ! kml || kml . documentElement . nodeName !== 'kml' ) {
34- return res . status ( 400 ) . send ( 'Invalid file, please upload a KML file.' ) ;
33+ const kmlData = kmlEntry . getData ( ) . toString ( 'utf8' ) ;
34+ processKmlData ( kmlData , req , res , next ) ;
35+ } catch ( err ) {
36+ return res . status ( 400 ) . send ( 'Unable to extract contents from KMZ file.' ) ;
3537 }
38+ } else if ( fileExtension === 'kml' ) {
39+ fs . readFile ( req . file . path , 'utf8' , function ( err , data ) {
40+ if ( err ) return next ( err ) ;
41+ processKmlData ( data , req , res , next ) ;
42+ } ) ;
43+ } else {
44+ return res . status ( 400 ) . send ( 'Invalid file, please upload a KML or KMZ file.' ) ;
45+ }
46+ }
47+
48+ function processKmlData ( data , req , res , next ) {
49+ const parser = new DOMParser ( ) ;
50+ const kml = parser . parseFromString ( data , "application/xml" ) ;
51+ const parseError = kml . getElementsByTagName ( "parsererror" ) ;
52+
53+ if ( parseError . length > 0 ) {
54+ console . error ( "KML Parsing Error:" , parseError [ 0 ] . textContent ) ;
55+ } else {
56+ console . log ( "Parsed KML successfully" ) ;
57+ }
58+
59+ if ( ! kml || kml . documentElement . nodeName !== 'kml' ) {
60+ return res . status ( 400 ) . send ( 'Invalid file, please upload a KML or KMZ file.' ) ;
61+ }
3662
37- req . kml = kml ;
38- return next ( ) ;
39- } ) ;
63+ req . kml = kml ;
64+ return next ( ) ;
4065 }
4166
4267 app . post (
@@ -45,7 +70,8 @@ module.exports = function(app, security) {
4570 access . authorize ( 'CREATE_LAYER' ) ,
4671 upload . single ( 'file' ) ,
4772 validate ,
48- function ( req , res , next ) {
73+ function ( req , res , next ) {
74+ console . log ( 'Importing KML file:' , req . file . originalname ) ;
4975 const features = toGeoJson . kml ( req . kml ) ;
5076 new api . Feature ( req . layer ) . createFeatures ( features )
5177 . then ( newFeatures => {
0 commit comments