diff --git a/testdata/faults.zip b/testdata/faults.zip new file mode 100644 index 0000000..837ff23 Binary files /dev/null and b/testdata/faults.zip differ diff --git a/testdata/faults_empty.zip b/testdata/faults_empty.zip new file mode 100644 index 0000000..a5f4f48 Binary files /dev/null and b/testdata/faults_empty.zip differ diff --git a/utils.go b/utils.go index fcd68e9..4805784 100644 --- a/utils.go +++ b/utils.go @@ -2,12 +2,16 @@ package gismanager import ( "database/sql" + "errors" + "fmt" "io/ioutil" "os" "path" "path/filepath" "strings" + "github.com/mholt/archiver" + //postgres Driver _ "github.com/lib/pq" yaml "gopkg.in/yaml.v2" @@ -49,7 +53,15 @@ func GetGISFiles(root string) ([]string, error) { return files, statErr } if !fileInfo.IsDir() { - files = append(files, path.Join(root, fileInfo.Name())) + extension := strings.ToLower(filepath.Ext(fileInfo.Name())) + if isSupported(extension) { + finalPath, preProcessErr := preprocessFile(root, "") + if preProcessErr != nil { + return files, preProcessErr + } + files = append(files, finalPath) + return files, nil + } return files, nil } dirInfo, err := ioutil.ReadDir(root) @@ -57,19 +69,11 @@ func GetGISFiles(root string) ([]string, error) { return files, err } for _, file := range dirInfo { - if file.IsDir() { - subFiles, subErr := GetGISFiles(path.Join(root, file.Name())) - if subErr == nil { - files = append(files, subFiles...) - } - } else { - extension := strings.ToLower(filepath.Ext(file.Name())) - if isSupported(extension) { - files = append(files, path.Join(root, file.Name())) - } + subFiles, subErr := GetGISFiles(path.Join(root, file.Name())) + if subErr == nil { + files = append(files, subFiles...) } } - return files, nil } @@ -87,3 +91,51 @@ func DBIsAlive(dbType string, connectionStr string) (err error) { } return } +func zippedShapeFile(zippedPath string, destPath string) (err error) { + fileInfo, statErr := os.Stat(zippedPath) + if statErr != nil || os.IsNotExist(statErr) { + err = statErr + return + } + if fileInfo.IsDir() { + err = errors.New("zippedPath must be file not a directory") + return + } + err = archiver.Zip.Open(zippedPath, destPath) + return +} +func preprocessFile(filePath string, tempPath string) (finalPath string, err error) { + logger := GetLogger() + ext := strings.ToLower(filepath.Ext(filePath)) + switch ext { + case ".zip": + newDir, tempDirErr := ioutil.TempDir(tempPath, "zipped_shapeFile") + fmt.Println(newDir) + if tempDirErr != nil { + logger.Error(tempDirErr) + err = tempDirErr + break + } + unzipErr := zippedShapeFile(filePath, newDir) + if unzipErr != nil { + logger.Error(unzipErr) + err = unzipErr + break + } + files, filesErr := GetGISFiles(newDir) + if filesErr != nil { + logger.Error(filesErr) + err = filesErr + break + } + if len(files) == 0 { + err = errors.New("cannot find gis files") + break + } + finalPath = files[0] + break + default: + finalPath = filePath + } + return +} diff --git a/utils_test.go b/utils_test.go index 46804f6..e077cbb 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,6 +1,7 @@ package gismanager import ( + "io/ioutil" "testing" "github.com/stretchr/testify/assert" @@ -11,11 +12,34 @@ func TestIsSupported(t *testing.T) { assert.True(t, ok) fail := isSupported(".tiff") assert.False(t, fail) + zippedTest := isSupported(".zip") + assert.True(t, zippedTest) } +func TestZippedShapeFile(t *testing.T) { + newDir, _ := ioutil.TempDir("", "zipped_shapeFile") + unzipErr := zippedShapeFile("./testdata/faults.zip", newDir) + assert.Nil(t, unzipErr) + dummyErr := zippedShapeFile("./testdata/faults_ss.zip", newDir) + assert.NotNil(t, dummyErr) + dirErr := zippedShapeFile("./testdata/", newDir) + assert.NotNil(t, dirErr) +} +func TestPreprocessFile(t *testing.T) { + finalPath, preProcessErr := preprocessFile("./testdata/faults.zip", "") + assert.NotNil(t, finalPath) + assert.NotEqual(t, "", finalPath) + assert.Nil(t, preProcessErr) + errFinalPath, err := preprocessFile("./testdata/dummy.zip", "") + assert.Equal(t, "", errFinalPath) + assert.NotNil(t, err) + emptyFinalPath, emptyErr := preprocessFile("./testdata/faults_empty.zip", "") + assert.Equal(t, "", emptyFinalPath) + assert.NotNil(t, emptyErr) +} func TestGetGISFiles(t *testing.T) { files, err := GetGISFiles("./testdata") - assert.Equal(t, 3, len(files)) + assert.Equal(t, 4, len(files)) assert.Nil(t, err) noDir, NoDirerr := GetGISFiles("./testdata/sample.gpkg") assert.Equal(t, 1, len(noDir))