comparison 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
comparison
equal deleted inserted replaced
872:b882b2c796c1 873:ad9272460ef3
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "database/sql"
6 "fmt" 5 "fmt"
7 "io" 6 "io"
8 "log" 7 "log"
9 "net/http" 8 "net/http"
10 "strings"
11 9
12 "gemma.intevation.de/gemma/pkg/auth"
13 "gemma.intevation.de/gemma/pkg/models" 10 "gemma.intevation.de/gemma/pkg/models"
14 "github.com/gorilla/mux" 11 "github.com/gorilla/mux"
15 ) 12 )
16 13
17 const ( 14 const (
18 maxStyleSize = 5 * 1024 * 1024 15 maxStyleSize = 5 * 1024 * 1024
19 styleName = "style" 16 styleName = "style"
20 ) 17 )
21 18
22 const ( 19 func extractStyle(req *http.Request) (string, error) {
23 replaceNameXSLT = `<?xml version="1.0"?>
24 <xsl:stylesheet version="1.0"
25 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
26 xmlns:sld="http://www.opengis.net/sld"
27 xmlns:se="http://www.opengis.net/se">
28
29 <xsl:param name="name"/>
30
31 <xsl:template
32 match="/sld:StyledLayerDescriptor/sld:NamedLayer/sld:Name/text()">
33 <xsl:value-of select="$name"/>
34 </xsl:template>
35
36 <xsl:template
37 match="/sld:StyledLayerDescriptor/sld:NamedLayer/se:Name/text()">
38 <xsl:value-of select="$name"/>
39 </xsl:template>
40
41 <xsl:template match="@*|node()">
42 <xsl:copy>
43 <xsl:apply-templates select="@*|node()"/>
44 </xsl:copy>
45 </xsl:template>
46 </xsl:stylesheet>`
47
48 xsltSQL = `SELECT xslt_process($1, $2, $3)`
49 )
50
51 func runXSLT(
52 req *http.Request,
53 document, stylesheet string,
54 params ...string,
55 ) (string, error) {
56 var result string
57
58 var args strings.Builder
59
60 for len(params) > 1 {
61 if args.Len() > 0 {
62 args.WriteByte(',')
63 }
64 fmt.Fprintf(&args, "%s='%s'",
65 strings.Replace(params[0], ",", "", -1),
66 strings.Replace(params[1], ",", "", -1))
67 params = params[2:]
68 }
69
70 log.Printf("params: %s\n", args.String())
71
72 err := auth.RunAsSessionUser(req, func(conn *sql.Conn) error {
73 return conn.QueryRowContext(
74 req.Context(), xsltSQL,
75 document, stylesheet, args.String()).Scan(&result)
76 })
77
78 return result, err
79 }
80
81 func extractStyle(req *http.Request) ([]byte, error) {
82 20
83 f, _, err := req.FormFile(styleName) 21 f, _, err := req.FormFile(styleName)
84 if err != nil { 22 if err != nil {
85 return nil, err 23 return "", err
86 } 24 }
87 defer f.Close() 25 defer f.Close()
88 26
89 var buf bytes.Buffer 27 var buf bytes.Buffer
90 28
91 if _, err := io.Copy(&buf, io.LimitReader(f, maxStyleSize)); err != nil { 29 if _, err := io.Copy(&buf, io.LimitReader(f, maxStyleSize)); err != nil {
92 return nil, err 30 return "", err
93 } 31 }
94 return buf.Bytes(), nil 32 return buf.String(), nil
95 } 33 }
96 34
97 func supportedWMSFeature(name string) bool { 35 func supportedWMSFeature(name string) bool {
98 return len(models.InternalServices.Filter( 36 return len(models.InternalServices.Filter(
99 models.IntAnd(models.IntWMS, models.IntByName(name)))) > 0 37 models.IntAnd(models.IntWMS, models.IntByName(name)))) > 0
118 return 56 return
119 } 57 }
120 58
121 log.Printf("uploaded file length: %d\n", len(style)) 59 log.Printf("uploaded file length: %d\n", len(style))
122 60
123 result, err := runXSLT( 61 if err := models.UpdateInternalStyle(req, feature, style); err != nil {
124 req, string(style), replaceNameXSLT, "name", feature)
125
126 if err != nil {
127 log.Printf("error: %v\n", err)
128 http.Error(rw, "error: "+err.Error(), http.StatusBadRequest)
129 return
130 }
131
132 if err := models.UpdateInternalStyle(feature, result); err != nil {
133 log.Printf("error: %v\n", err) 62 log.Printf("error: %v\n", err)
134 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) 63 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
135 return 64 return
136 } 65 }
137 66