view pkg/controllers/uploadedimports.go @ 2549:9bf6b767a56a

client: refactored and improved splitscreen for diagrams To make different diagrams possible, the splitscreen view needed to be decoupled from the cross profiles. Also the style has changed to make it more consistent with the rest of the app. The standard box header is now used and there are collapse and expand animations.
author Markus Kottlaender <markus@intevation.de>
date Fri, 08 Mar 2019 08:50:47 +0100
parents 07f892bc4bb0
children ce39e9954e85
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2018 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>

package controllers

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"strconv"
	"time"

	"gemma.intevation.de/gemma/pkg/auth"
	"gemma.intevation.de/gemma/pkg/common"
	"gemma.intevation.de/gemma/pkg/imports"
	"gemma.intevation.de/gemma/pkg/misc"
)

const maxUploadSize = 25 * 1024 * 1024

type BadUploadParameterError string

func importWaterwayProfiles() http.HandlerFunc {
	return uploadedImport(
		imports.WPJobKind,
		"wp.csv",
		func(req *http.Request, dir string) (imports.Job, error) {
			url := req.FormValue("url")
			if url == "" {
				return nil, BadUploadParameterError("missing 'url' parameter")
			}

			featureType := req.FormValue("feature-type")
			if featureType == "" {
				return nil, BadUploadParameterError("missing 'feature-type' parameter")
			}

			sortBy := req.FormValue("sort-by")

			var precision *float64
			if p := req.FormValue("precision"); p != "" {
				v, err := strconv.ParseFloat(p, 64)
				if err != nil {
					return nil, BadUploadParameterError(
						fmt.Sprintf("Invalid 'precision' parameter: %v", err))
				}
				precision = &v
			}

			return &imports.WaterwayProfiles{
				Dir:         dir,
				URL:         url,
				FeatureType: featureType,
				SortBy:      sortBy,
				Precision:   precision,
			}, nil
		},
	)
}

func importApprovedGaugeMeasurements() http.HandlerFunc {
	return uploadedImport(
		imports.AGMJobKind,
		"agm.csv",
		func(_ *http.Request, dir string) (imports.Job, error) {
			return &imports.ApprovedGaugeMeasurements{Dir: dir}, nil
		},
	)
}

func importUploadedBottleneck() http.HandlerFunc {
	return uploadedImport(
		imports.UBNJobKind,
		"data.xml",
		func(_ *http.Request, dir string) (imports.Job, error) {
			return &imports.UploadedBottleneck{Dir: dir}, nil
		},
	)
}

func importUploadedFairwayAvailability() http.HandlerFunc {
	return uploadedImport(
		imports.UFAJobKind,
		"data.xml",
		func(_ *http.Request, dir string) (imports.Job, error) {
			return &imports.UploadedFairwayAvailability{Dir: dir}, nil
		},
	)
}

func importUploadedGaugeMeasurement() http.HandlerFunc {
	return uploadedImport(
		imports.UGMJobKind,
		"data.xml",
		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(*http.Request, string) (imports.Job, error),
) http.HandlerFunc {

	return func(rw http.ResponseWriter, req *http.Request) {
		dir, err := misc.StoreUploadedFile(
			req,
			string(kind),
			fname,
			maxUploadSize)
		if err != nil {
			log.Printf("error: %v\n", err)
			http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
			return
		}

		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 {
			log.Printf("error: %v\n", err)
			http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
			return
		}

		session, _ := auth.GetSession(req)

		sendEmail := req.FormValue("email") != ""

		jobID, err := imports.AddJob(
			kind,
			time.Time{}, // due
			nil,         // trys
			nil,         // retry wait
			session.User,
			sendEmail,
			serialized)

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

		log.Printf("info: added import #%d to queue\n", jobID)

		result := struct {
			ID int64 `json:"id"`
		}{
			ID: jobID,
		}
		SendJSON(rw, http.StatusCreated, &result)
	}
}