changeset 2536:0a3debcfbe8f

Upload imports: Increased power of upload imports to be able to cope parameterized imports. TODO: Made WP and maybe SR imports an upload import.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Mar 2019 12:31:34 +0100
parents 73c8762cee60
children 07f892bc4bb0
files pkg/controllers/uploadedimports.go
diffstat 1 files changed, 29 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/uploadedimports.go	Thu Mar 07 12:20:00 2019 +0100
+++ b/pkg/controllers/uploadedimports.go	Thu Mar 07 12:31:34 2019 +0100
@@ -16,6 +16,7 @@
 import (
 	"log"
 	"net/http"
+	"os"
 	"time"
 
 	"gemma.intevation.de/gemma/pkg/auth"
@@ -26,12 +27,14 @@
 
 const maxUploadSize = 25 * 1024 * 1024
 
+type BadUploadParameterError string
+
 func importApprovedGaugeMeasurements() http.HandlerFunc {
 	return uploadedImport(
 		imports.AGMJobKind,
 		"agm.csv",
-		func(dir string) imports.Job {
-			return &imports.ApprovedGaugeMeasurements{Dir: dir}
+		func(_ *http.Request, dir string) (imports.Job, error) {
+			return &imports.ApprovedGaugeMeasurements{Dir: dir}, nil
 		},
 	)
 }
@@ -40,8 +43,8 @@
 	return uploadedImport(
 		imports.UBNJobKind,
 		"data.xml",
-		func(dir string) imports.Job {
-			return &imports.UploadedBottleneck{Dir: dir}
+		func(_ *http.Request, dir string) (imports.Job, error) {
+			return &imports.UploadedBottleneck{Dir: dir}, nil
 		},
 	)
 }
@@ -50,8 +53,8 @@
 	return uploadedImport(
 		imports.UFAJobKind,
 		"data.xml",
-		func(dir string) imports.Job {
-			return &imports.UploadedFairwayAvailability{Dir: dir}
+		func(_ *http.Request, dir string) (imports.Job, error) {
+			return &imports.UploadedFairwayAvailability{Dir: dir}, nil
 		},
 	)
 }
@@ -60,16 +63,20 @@
 	return uploadedImport(
 		imports.UGMJobKind,
 		"data.xml",
-		func(dir string) imports.Job {
-			return &imports.UploadedGaugeMeasurement{Dir: dir}
+		func(_ *http.Request, dir string) (imports.Job, error) {
+			return &imports.UploadedGaugeMeasurement{Dir: dir}, nil
 		},
 	)
 }
 
+func (bup BadUploadParameterError) Error() string {
+	return string(bup)
+}
+
 func uploadedImport(
 	kind imports.JobKind,
 	fname string,
-	create func(string) imports.Job,
+	create func(*http.Request, string) (imports.Job, error),
 ) http.HandlerFunc {
 
 	return func(rw http.ResponseWriter, req *http.Request) {
@@ -84,7 +91,19 @@
 			return
 		}
 
-		job := create(dir)
+		job, err := create(req, dir)
+		if err != nil {
+			if err2 := os.RemoveAll(dir); err2 != nil {
+				log.Printf("warn: %v\n", err2)
+			}
+			if err2, ok := err.(BadUploadParameterError); ok {
+				http.Error(rw, string(err2), http.StatusBadRequest)
+				return
+			}
+			log.Printf("error: %v\n", err)
+			http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
+			return
+		}
 
 		serialized, err := common.ToJSONString(job)
 		if err != nil {