changeset 870:29c11f4bf9db geo-style

Started with endpoint to upload geo style.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 29 Sep 2018 23:33:14 +0200
parents aa8f30c1ed27
children f0b6852c14d1
files pkg/auth/opendb.go pkg/controllers/geostyling.go pkg/controllers/routes.go
diffstat 3 files changed, 63 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/auth/opendb.go	Sat Sep 29 22:34:24 2018 +0200
+++ b/pkg/auth/opendb.go	Sat Sep 29 23:33:14 2018 +0200
@@ -4,6 +4,7 @@
 	"context"
 	"database/sql"
 	"errors"
+	"net/http"
 	"sync"
 
 	"github.com/jackc/pgx"
@@ -12,7 +13,10 @@
 	"gemma.intevation.de/gemma/pkg/config"
 )
 
-var ErrNoMetamorphUser = errors.New("No metamorphic user configured")
+var (
+	ErrNoMetamorphUser = errors.New("No metamorphic user configured")
+	ErrNotLoggedIn     = errors.New("Not logged in")
+)
 
 func OpenDB(user, password string) (*sql.DB, error) {
 
@@ -117,3 +121,15 @@
 	defer conn.Close()
 	return fn(conn)
 }
+
+func RunAsSessionUser(req *http.Request, fn func(*sql.Conn) error) error {
+	token, ok := GetToken(req)
+	if !ok {
+		return ErrNotLoggedIn
+	}
+	session := Sessions.Session(token)
+	if session == nil {
+		return ErrNotLoggedIn
+	}
+	return RunAs(session.User, req.Context(), fn)
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/geostyling.go	Sat Sep 29 23:33:14 2018 +0200
@@ -0,0 +1,42 @@
+package controllers
+
+import (
+	"bytes"
+	"io"
+	"log"
+	"net/http"
+)
+
+const (
+	maxStyleSize = 5 * 1024 * 1024
+	styleName    = "style"
+)
+
+func extractStyle(req *http.Request) ([]byte, error) {
+
+	f, _, err := req.FormFile(styleName)
+	if err != nil {
+		return nil, err
+	}
+	defer f.Close()
+
+	var buf bytes.Buffer
+
+	if _, err := io.Copy(&buf, io.LimitReader(f, maxStyleSize)); err != nil {
+		return nil, err
+	}
+	return buf.Bytes(), nil
+}
+
+func uploadStyle(rw http.ResponseWriter, req *http.Request) {
+
+	data, err := extractStyle(req)
+	if err != nil {
+		log.Printf("error: %v\n", err)
+		http.Error(rw, "error: "+err.Error(), http.StatusBadRequest)
+		return
+	}
+
+	log.Printf("uploaded file length: %d\n", len(data))
+	// TODO: Implement me!
+}
--- a/pkg/controllers/routes.go	Sat Sep 29 22:34:24 2018 +0200
+++ b/pkg/controllers/routes.go	Sat Sep 29 23:33:14 2018 +0200
@@ -124,7 +124,6 @@
 	})).Methods(http.MethodGet)
 
 	// Cross sections
-
 	api.Handle("/cross", any(&JSONHandler{
 		Input:  func() interface{} { return new(models.CrossSectionInput) },
 		Handle: crossSection,
@@ -136,6 +135,10 @@
 		Handle: searchFeature,
 	})).Methods(http.MethodPost)
 
+	// Geo styling
+	api.Handle("/geo/style/{feature}",
+		any(http.HandlerFunc(uploadStyle))).Methods(http.MethodPost)
+
 	// Token handling: Login/Logout.
 	api.HandleFunc("/login", login).
 		Methods(http.MethodPost)