Mercurial > gemma
changeset 2254:b53206045699
Uploaded gauge measurement import: Added missing file.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 13 Feb 2019 17:37:28 +0100 |
parents | cfc523c70e90 |
children | 33af355047e1 |
files | pkg/controllers/ugmimports.go |
diffstat | 1 files changed, 81 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/controllers/ugmimports.go Wed Feb 13 17:37:28 2019 +0100 @@ -0,0 +1,81 @@ +// 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 ( + "log" + "net/http" + "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 ( + maxUploadedGaugeMeasurementSize = 25 * 1024 * 1024 + uploadedGaugeMeasurementName = "ugm" +) + +func importUploadedGaugeMeasurement(rw http.ResponseWriter, req *http.Request) { + + dir, err := misc.StoreUploadedFile( + req, + uploadedGaugeMeasurementName, + "data.xml", + maxUploadedGaugeMeasurementSize) + if err != nil { + log.Printf("error: %v\n", err) + http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) + return + } + + ugm := &imports.UploadedGaugeMeasurement{Dir: dir} + + serialized, err := common.ToJSONString(ugm) + 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( + imports.UGMJobKind, + time.Time{}, // due + nil, // trys + nil, // wait retry + 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) +}