Mercurial > gemma
view pkg/controllers/wpimports.go @ 2073:e6dccc7a3ea1
Waterway profiles import: Added upload route.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 30 Jan 2019 17:23:22 +0100 |
parents | |
children | dacf79a0658e |
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 ( "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 ( maxWaterwayProfilesSize = 25 * 1024 * 1024 waterwayProfilesName = "wp" ) func storeWaterwayProfiles(req *http.Request) (string, error) { // Check for direct upload. f, _, err := req.FormFile(waterwayProfilesName) if err != nil { return "", err } defer f.Close() dir, err := ioutil.TempDir(config.TmpDir(), waterwayProfilesName) if err != nil { return "", err } o, err := os.Create(filepath.Join(dir, "wp.csv")) if err != nil { os.RemoveAll(dir) return "", err } out := bufio.NewWriter(o) if _, err = io.Copy(out, io.LimitReader(f, maxWaterwayProfilesSize)); 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 importWaterwayProfiles(rw http.ResponseWriter, req *http.Request) { dir, err := storeWaterwayProfiles(req) if err != nil { log.Printf("error: %v\n", err) http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) return } wp := &imports.WaterwayProfiles{Dir: dir} serialized, err := common.ToJSONString(wp) 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.AGMJobKind, 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) }