annotate pkg/controllers/geostyling.go @ 873:ad9272460ef3 geo-style

Do the XSLT to adjust the layer name when updating the style column in the database.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 30 Sep 2018 14:24:37 +0200
parents f0b6852c14d1
children 876d1f5433be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
1 package controllers
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
2
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
3 import (
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
4 "bytes"
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
5 "fmt"
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
6 "io"
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
7 "log"
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
8 "net/http"
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
9
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
10 "gemma.intevation.de/gemma/pkg/models"
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
11 "github.com/gorilla/mux"
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
12 )
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
13
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
14 const (
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
15 maxStyleSize = 5 * 1024 * 1024
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
16 styleName = "style"
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
17 )
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
18
873
ad9272460ef3 Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 871
diff changeset
19 func extractStyle(req *http.Request) (string, error) {
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
20
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
21 f, _, err := req.FormFile(styleName)
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
22 if err != nil {
873
ad9272460ef3 Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 871
diff changeset
23 return "", err
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
24 }
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
25 defer f.Close()
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
26
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
27 var buf bytes.Buffer
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
28
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
29 if _, err := io.Copy(&buf, io.LimitReader(f, maxStyleSize)); err != nil {
873
ad9272460ef3 Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 871
diff changeset
30 return "", err
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
31 }
873
ad9272460ef3 Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 871
diff changeset
32 return buf.String(), nil
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
33 }
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
34
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
35 func supportedWMSFeature(name string) bool {
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
36 return len(models.InternalServices.Filter(
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
37 models.IntAnd(models.IntWMS, models.IntByName(name)))) > 0
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
38 }
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
39
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
40 func uploadStyle(rw http.ResponseWriter, req *http.Request) {
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
41
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
42 feature := mux.Vars(req)["feature"]
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
43
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
44 // only allow internal WMS features
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
45 if !supportedWMSFeature(feature) {
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
46 http.Error(rw,
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
47 fmt.Sprintf("WMS feature %s is not found.", feature),
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
48 http.StatusNotFound)
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
49 return
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
50 }
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
51
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
52 style, err := extractStyle(req)
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
53 if err != nil {
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
54 log.Printf("error: %v\n", err)
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
55 http.Error(rw, "error: "+err.Error(), http.StatusBadRequest)
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
56 return
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
57 }
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
58
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
59 log.Printf("uploaded file length: %d\n", len(style))
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
60
873
ad9272460ef3 Do the XSLT to adjust the layer name when updating the style column in the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 871
diff changeset
61 if err := models.UpdateInternalStyle(req, feature, style); err != nil {
871
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
62 log.Printf("error: %v\n", err)
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
63 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
64 return
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
65 }
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
66
f0b6852c14d1 More on uploading styles to gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 870
diff changeset
67 // TODO: Configure GeoServer
870
29c11f4bf9db Started with endpoint to upload geo style.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
68 }