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