Mercurial > gemma
comparison 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 |
comparison
equal
deleted
inserted
replaced
2072:b4d8d320feab | 2073:e6dccc7a3ea1 |
---|---|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 | |
2 // without warranty, see README.md and license for details. | |
3 // | |
4 // SPDX-License-Identifier: AGPL-3.0-or-later | |
5 // License-Filename: LICENSES/AGPL-3.0.txt | |
6 // | |
7 // Copyright (C) 2018 by via donau | |
8 // – Österreichische Wasserstraßen-Gesellschaft mbH | |
9 // Software engineering by Intevation GmbH | |
10 // | |
11 // Author(s): | |
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> | |
13 | |
14 package controllers | |
15 | |
16 import ( | |
17 "bufio" | |
18 "io" | |
19 "io/ioutil" | |
20 "log" | |
21 "net/http" | |
22 "os" | |
23 "path/filepath" | |
24 "time" | |
25 | |
26 "gemma.intevation.de/gemma/pkg/auth" | |
27 "gemma.intevation.de/gemma/pkg/common" | |
28 "gemma.intevation.de/gemma/pkg/config" | |
29 "gemma.intevation.de/gemma/pkg/imports" | |
30 ) | |
31 | |
32 const ( | |
33 maxWaterwayProfilesSize = 25 * 1024 * 1024 | |
34 waterwayProfilesName = "wp" | |
35 ) | |
36 | |
37 func storeWaterwayProfiles(req *http.Request) (string, error) { | |
38 | |
39 // Check for direct upload. | |
40 f, _, err := req.FormFile(waterwayProfilesName) | |
41 if err != nil { | |
42 return "", err | |
43 } | |
44 defer f.Close() | |
45 | |
46 dir, err := ioutil.TempDir(config.TmpDir(), waterwayProfilesName) | |
47 if err != nil { | |
48 return "", err | |
49 } | |
50 | |
51 o, err := os.Create(filepath.Join(dir, "wp.csv")) | |
52 if err != nil { | |
53 os.RemoveAll(dir) | |
54 return "", err | |
55 } | |
56 | |
57 out := bufio.NewWriter(o) | |
58 | |
59 if _, err = io.Copy(out, io.LimitReader(f, maxWaterwayProfilesSize)); err != nil { | |
60 o.Close() | |
61 os.RemoveAll(dir) | |
62 return "", err | |
63 } | |
64 | |
65 if err = out.Flush(); err != nil { | |
66 o.Close() | |
67 os.RemoveAll(dir) | |
68 return "", err | |
69 } | |
70 | |
71 return dir, nil | |
72 } | |
73 func importWaterwayProfiles(rw http.ResponseWriter, req *http.Request) { | |
74 | |
75 dir, err := storeWaterwayProfiles(req) | |
76 if err != nil { | |
77 log.Printf("error: %v\n", err) | |
78 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) | |
79 return | |
80 } | |
81 | |
82 wp := &imports.WaterwayProfiles{Dir: dir} | |
83 | |
84 serialized, err := common.ToJSONString(wp) | |
85 if err != nil { | |
86 log.Printf("error: %v\n", err) | |
87 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) | |
88 return | |
89 } | |
90 | |
91 session, _ := auth.GetSession(req) | |
92 | |
93 sendEmail := req.FormValue("email") != "" | |
94 | |
95 jobID, err := imports.AddJob( | |
96 imports.AGMJobKind, | |
97 time.Time{}, // due | |
98 nil, // trys | |
99 nil, // wait retry | |
100 session.User, | |
101 sendEmail, | |
102 serialized) | |
103 | |
104 if err != nil { | |
105 log.Printf("error: %v\n", err) | |
106 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) | |
107 return | |
108 } | |
109 | |
110 log.Printf("info: added import #%d to queue\n", jobID) | |
111 | |
112 result := struct { | |
113 ID int64 `json:"id"` | |
114 }{ | |
115 ID: jobID, | |
116 } | |
117 SendJSON(rw, http.StatusCreated, &result) | |
118 } |