view pkg/controllers/geostyling.go @ 913:876d1f5433be geo-style

Started with direct applying style after modification via controller. WIP.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 02 Oct 2018 23:29:55 +0200
parents ad9272460ef3
children 4bf3a3a20ce1
line wrap: on
line source

package controllers

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"net/http"

	"gemma.intevation.de/gemma/pkg/geoserver"
	"gemma.intevation.de/gemma/pkg/models"
	"github.com/gorilla/mux"
)

const (
	maxStyleSize = 5 * 1024 * 1024
	styleName    = "style"
)

func extractStyle(req *http.Request) (string, error) {

	f, _, err := req.FormFile(styleName)
	if err != nil {
		return "", err
	}
	defer f.Close()

	var buf bytes.Buffer

	if _, err := io.Copy(&buf, io.LimitReader(f, maxStyleSize)); err != nil {
		return "", err
	}
	return buf.String(), nil
}

func supportedWMSFeature(name string) bool {
	return len(models.InternalServices.Filter(
		models.IntAnd(models.IntWMS, models.IntByName(name)))) > 0
}

func uploadStyle(rw http.ResponseWriter, req *http.Request) {

	feature := mux.Vars(req)["feature"]

	// only allow internal WMS features
	if !supportedWMSFeature(feature) {
		http.Error(rw,
			fmt.Sprintf("WMS feature %s is not found.", feature),
			http.StatusNotFound)
		return
	}

	style, 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(style))

	if err := models.UpdateInternalStyle(req, feature, style); err != nil {
		log.Printf("error: %v\n", err)
		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
		return
	}

	geoserver.ReconfigureStyle(feature)
}