Mercurial > gemma
changeset 2191:4b9496752d89
Upload bottleneck import: Added stub.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 12 Feb 2019 10:44:00 +0100 |
parents | ecb0ad836aa0 |
children | 7939349f9ffc |
files | pkg/controllers/routes.go pkg/controllers/ubnimports.go pkg/imports/ubn.go |
diffstat | 3 files changed, 195 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/routes.go Tue Feb 12 10:04:01 2019 +0100 +++ b/pkg/controllers/routes.go Tue Feb 12 10:44:00 2019 +0100 @@ -205,6 +205,9 @@ api.Handle("/imports/wp", waterwayAdmin( http.HandlerFunc(importWaterwayProfiles))).Methods(http.MethodPost) + api.Handle("/imports/ubn", waterwayAdmin( + http.HandlerFunc(importUploadedBottleneck))).Methods(http.MethodPost) + api.Handle("/imports/{kind:st}", sysAdmin(&JSONHandler{ Input: importModel, Handle: manualImport,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/controllers/ubnimports.go Tue Feb 12 10:44:00 2019 +0100 @@ -0,0 +1,119 @@ +// 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 ( + "bufio" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "path/filepath" + "time" + + "gemma.intevation.de/gemma/pkg/auth" + "gemma.intevation.de/gemma/pkg/common" + "gemma.intevation.de/gemma/pkg/config" + "gemma.intevation.de/gemma/pkg/imports" +) + +const ( + maxUploadedBottleneckSize = 25 * 1024 * 1024 + uploadBottleneckName = "ubn" +) + +func storeUploadedBottleneck(req *http.Request) (string, error) { + + // Check for direct upload. + f, _, err := req.FormFile(uploadBottleneckName) + if err != nil { + return "", err + } + defer f.Close() + + dir, err := ioutil.TempDir(config.TmpDir(), uploadBottleneckName) + if err != nil { + return "", err + } + + o, err := os.Create(filepath.Join(dir, "data.xml")) + if err != nil { + os.RemoveAll(dir) + return "", err + } + + out := bufio.NewWriter(o) + + if _, err = io.Copy(out, io.LimitReader(f, maxUploadedBottleneckSize)); err != nil { + o.Close() + os.RemoveAll(dir) + return "", err + } + + if err = out.Flush(); err != nil { + o.Close() + os.RemoveAll(dir) + return "", err + } + + return dir, nil +} + +func importUploadedBottleneck(rw http.ResponseWriter, req *http.Request) { + + dir, err := storeUploadedBottleneck(req) + if err != nil { + log.Printf("error: %v\n", err) + http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) + return + } + + ubn := &imports.UploadedBottleneck{Dir: dir} + + serialized, err := common.ToJSONString(ubn) + 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.UBNJobKind, + 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) +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/imports/ubn.go Tue Feb 12 10:44:00 2019 +0100 @@ -0,0 +1,73 @@ +// 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 imports + +import ( + "context" + "database/sql" + "errors" + "os" +) + +type UploadedBottleneck struct { + Dir string `json:"dir"` +} + +// UBNJobKind is the import queue type identifier. +const UBNJobKind JobKind = "ubn" + +type ubnJobCreator struct{} + +func init() { + RegisterJobCreator(UBNJobKind, ubnJobCreator{}) +} + +func (ubnJobCreator) Description() string { return "uploaded bottlenecks" } + +func (ubnJobCreator) AutoAccept() bool { return false } + +func (ubnJobCreator) Create() Job { return new(UploadedBottleneck) } + +func (ubnJobCreator) Depends() []string { + return []string{ + "gauges", + "bottlenecks", + } +} + +// StageDone moves the imported bottleneck out of the staging area. +func (ubnJobCreator) StageDone( + ctx context.Context, + tx *sql.Tx, + id int64, +) error { + // TODO: Implement me! + return nil +} + +// CleanUp of a uploaded bottleneck import removes the temp dir. +func (ubn *UploadedBottleneck) CleanUp() error { + return os.RemoveAll(ubn.Dir) +} + +// Do executes the actual uploaded bottleneck import. +func (ubn *UploadedBottleneck) Do( + ctx context.Context, + importID int64, + conn *sql.Conn, + feedback Feedback, +) (interface{}, error) { + // TODO: Implement me! + return nil, errors.New("Not implemented, yet!") +}