changeset 1225:4d7c44f7044e

Factored out som zip lookup code to be reusable in sounding result upload controller.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 19 Nov 2018 17:28:35 +0100
parents bc4b642c8d04
children 2e65e8ddacab
files pkg/common/zip.go pkg/controllers/srimports.go pkg/imports/sr.go
diffstat 3 files changed, 42 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/common/zip.go	Mon Nov 19 17:28:35 2018 +0100
@@ -0,0 +1,29 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2018 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package common
+
+import (
+	"archive/zip"
+	"strings"
+)
+
+func FindInZIP(z *zip.ReadCloser, needle string) *zip.File {
+	needle = strings.ToLower(needle)
+	for _, straw := range z.File {
+		if strings.HasSuffix(strings.ToLower(straw.Name), needle) {
+			return straw
+		}
+	}
+	return nil
+}
--- a/pkg/controllers/srimports.go	Mon Nov 19 17:14:42 2018 +0100
+++ b/pkg/controllers/srimports.go	Mon Nov 19 17:28:35 2018 +0100
@@ -14,6 +14,7 @@
 package controllers
 
 import (
+	"archive/zip"
 	"bufio"
 	"database/sql"
 	"encoding/hex"
@@ -134,6 +135,14 @@
 		return
 	}
 
+	srFile := filepath.Join(dir, "sr.zip")
+
+	var zr *zip.ReadCloser
+	if zr, err = zip.OpenReader(srFile); err != nil {
+		return
+	}
+	defer zr.Close()
+
 	_ = dir
 	// TODO: Implement me!
 
--- a/pkg/imports/sr.go	Mon Nov 19 17:14:42 2018 +0100
+++ b/pkg/imports/sr.go	Mon Nov 19 17:28:35 2018 +0100
@@ -152,7 +152,7 @@
 	defer z.Close()
 
 	feedback.Info("Looking for 'meta.json'")
-	mf := find("meta.json", z.File)
+	mf := common.FindInZIP(z, "meta.json")
 	if mf == nil {
 		return errors.New("Cannot find 'meta.json'")
 	}
@@ -167,7 +167,7 @@
 	}
 
 	feedback.Info("Looking for '*.xyz'")
-	xyzf := find(".xyz", z.File)
+	xyzf := common.FindInZIP(z, ".xyz")
 	if xyzf == nil {
 		return errors.New("Cannot find any *.xyz file")
 	}
@@ -273,16 +273,6 @@
 	return os.RemoveAll(string(sr))
 }
 
-func find(needle string, haystack []*zip.File) *zip.File {
-	needle = strings.ToLower(needle)
-	for _, straw := range haystack {
-		if strings.HasSuffix(strings.ToLower(straw.Name), needle) {
-			return straw
-		}
-	}
-	return nil
-}
-
 func loadMeta(f *zip.File) (*models.SoundingResultMeta, error) {
 	r, err := f.Open()
 	if err != nil {
@@ -345,12 +335,12 @@
 }
 
 func loadBoundary(z *zip.ReadCloser) (Polygon, error) {
-	shpF := find(".shp", z.File)
+	shpF := common.FindInZIP(z, ".shp")
 	if shpF == nil {
 		return nil, nil
 	}
 	prefix := strings.TrimSuffix(shpF.Name, path.Ext(shpF.Name))
-	dbfF := find(prefix+".dbf", z.File)
+	dbfF := common.FindInZIP(z, prefix+".dbf")
 	if dbfF == nil {
 		return nil, fmt.Errorf("No DBF file found for %s", shpF.Name)
 	}