Mercurial > gemma
annotate pkg/controllers/srimports.go @ 1741:44398a8bdf94
Approved gauge measurements: Added a stub to upload a CSV file for parsing.
TODO: Implement the parsing and store the values in the DB.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 09 Jan 2019 18:26:52 +0100 |
parents | 49e047c2106e |
children | 807569b08513 |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1010
diff
changeset
|
13 |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
1225
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
17 "archive/zip" |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "bufio" |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
19 "database/sql" |
1221
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
20 "encoding/hex" |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
21 "fmt" |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 "io" |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 "io/ioutil" |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 "log" |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 "net/http" |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 "os" |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 "path/filepath" |
1239
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
28 "strconv" |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
29 "sync" |
1221
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
30 "time" |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 |
1239
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
32 "github.com/gorilla/mux" |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
33 |
979
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
34 "gemma.intevation.de/gemma/pkg/auth" |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
35 "gemma.intevation.de/gemma/pkg/common" |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 "gemma.intevation.de/gemma/pkg/config" |
979
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
37 "gemma.intevation.de/gemma/pkg/imports" |
1221
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
38 "gemma.intevation.de/gemma/pkg/misc" |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
39 "gemma.intevation.de/gemma/pkg/models" |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 ) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 const ( |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 maxSoundingResultSize = 25 * 1024 * 1024 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 soundingResultName = "soundingresult" |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 ) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
47 func fetchSoundingResult(req *http.Request) (string, error) { |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 |
1221
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
49 // Check first if we have a token. |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
50 if token := req.FormValue("token"); token != "" { |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
51 if _, err := hex.DecodeString(token); err != nil { |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
52 return "", err |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
53 } |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
54 dir := config.TmpDir() |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
55 if dir == "" { |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
56 dir = os.TempDir() |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
57 } |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
58 // XXX: This should hopefully be race-free enough. |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
59 now := time.Now().Format("2006-15-04-05") |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
60 dst := filepath.Join(dir, soundingResultName+"-"+token+"-"+now) |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
61 if err := misc.UnmakeTempFile(token, dst); err != nil { |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
62 return "", err |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
63 } |
1241
a45fa8943254
Sounding result import: Forgot to return the token temp file to the importer. Doh!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1239
diff
changeset
|
64 return dst, nil |
1221
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
65 } |
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
66 |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
67 return storeSoundingResult(req) |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
68 } |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
69 |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
70 func storeSoundingResult(req *http.Request) (string, error) { |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
71 |
1221
c193649d4f11
Add an area for temp uploads on the server to be addressed
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1203
diff
changeset
|
72 // Check for direct upload. |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 f, _, err := req.FormFile(soundingResultName) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 if err != nil { |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 return "", err |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 } |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 defer f.Close() |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 dir, err := ioutil.TempDir(config.TmpDir(), soundingResultName) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 if err != nil { |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 return "", err |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 } |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 o, err := os.Create(filepath.Join(dir, "sr.zip")) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 if err != nil { |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 os.RemoveAll(dir) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 return "", err |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 out := bufio.NewWriter(o) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 if _, err = io.Copy(out, io.LimitReader(f, maxSoundingResultSize)); err != nil { |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 o.Close() |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 os.RemoveAll(dir) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 return "", err |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 } |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 if err = out.Flush(); err != nil { |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 o.Close() |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 os.RemoveAll(dir) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 return "", err |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 } |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 |
984
3c9ea8ab856a
Fixed small glitches in sounding result importer.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
979
diff
changeset
|
104 return dir, nil |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 } |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 |
1239
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
107 func fetchSoundingResultMetaOverrides(sr *imports.SoundingResult, req *http.Request) error { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
108 |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
109 if v := req.FormValue("epsg"); v != "" { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
110 epsg, err := strconv.ParseUint(v, 10, 32) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
111 if err != nil { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
112 return err |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
113 } |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
114 srid := uint(epsg) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
115 sr.EPSG = &srid |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
116 } |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
117 |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
118 if v := req.FormValue("date"); v != "" { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
119 date, err := time.Parse(models.SoundingResultDateFormat, v) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
120 if err != nil { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
121 return err |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
122 } |
1321
8d9754007089
Used keyed initializer to make 'go vet' happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1297
diff
changeset
|
123 sr.Date = &models.SoundingResultDate{Time: date} |
1239
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
124 } |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
125 |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
126 if v := req.FormValue("depth-reference"); v != "" { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
127 sr.DepthReference = &v |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
128 } |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
129 |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
130 if v := req.FormValue("bottleneck"); v != "" { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
131 sr.Bottleneck = &v |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
132 } |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
133 |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
134 return nil |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
135 } |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
136 |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
137 func importSoundingResult(rw http.ResponseWriter, req *http.Request) { |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
138 |
1239
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
139 sr := new(imports.SoundingResult) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
140 |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
141 if err := fetchSoundingResultMetaOverrides(sr, req); err != nil { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
142 log.Printf("error: %v\n", err) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
143 http.Error(rw, "error: "+err.Error(), http.StatusBadRequest) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
144 return |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
145 } |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
146 |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
147 dir, err := fetchSoundingResult(req) |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 if err != nil { |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 log.Printf("error: %v\n", err) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 return |
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 } |
1239
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
153 sr.Dir = dir |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
154 |
1531
24445a618513
Added stub for bottleneck importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1528
diff
changeset
|
155 serialized, err := common.ToJSONString(sr) |
1239
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
156 if err != nil { |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
157 log.Printf("error: %v\n", err) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
158 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
159 return |
d842d9d10872
Sounding result import: Added the feature to override bottleneck, EPSG, depth reference and date in meta.json by POST arguments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1228
diff
changeset
|
160 } |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 |
979
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
162 session, _ := auth.GetSession(req) |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
163 |
1646
a0982c38eac0
Import queue: Implemented email notifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1642
diff
changeset
|
164 sendEmail := req.FormValue("bottleneck") != "" |
a0982c38eac0
Import queue: Implemented email notifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1642
diff
changeset
|
165 |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
166 var due time.Time |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
167 if d := req.FormValue("due"); d != "" { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
168 var err error |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
169 if due, err = time.Parse("2006-01-02T15:04:05", d); err != nil { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
170 log.Printf("error: %v\n", err) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
171 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
172 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
173 |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
174 retries := -1 |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
175 if r := req.FormValue("retries"); r != "" { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
176 var err error |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
177 if retries, err = strconv.Atoi(r); err != nil { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
178 log.Printf("error: %v\n", err) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
179 retries = -1 |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
180 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
181 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
182 |
1642
49c04bb64e0a
Import queue: Implemented auto-accept and email sending.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1531
diff
changeset
|
183 jobID, err := imports.AddJob( |
49c04bb64e0a
Import queue: Implemented auto-accept and email sending.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1531
diff
changeset
|
184 imports.SRJobKind, |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1646
diff
changeset
|
185 due, retries, |
1642
49c04bb64e0a
Import queue: Implemented auto-accept and email sending.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1531
diff
changeset
|
186 session.User, |
1646
a0982c38eac0
Import queue: Implemented email notifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1642
diff
changeset
|
187 sendEmail, false, |
1642
49c04bb64e0a
Import queue: Implemented auto-accept and email sending.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1531
diff
changeset
|
188 serialized) |
49c04bb64e0a
Import queue: Implemented auto-accept and email sending.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1531
diff
changeset
|
189 |
998
75e65599ea52
Persist job queue in database. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
991
diff
changeset
|
190 if err != nil { |
75e65599ea52
Persist job queue in database. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
991
diff
changeset
|
191 log.Printf("error: %v\n", err) |
75e65599ea52
Persist job queue in database. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
991
diff
changeset
|
192 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) |
75e65599ea52
Persist job queue in database. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
991
diff
changeset
|
193 return |
75e65599ea52
Persist job queue in database. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
991
diff
changeset
|
194 } |
75e65599ea52
Persist job queue in database. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
991
diff
changeset
|
195 |
1010
8f23ec811afb
Fixed and harmonized wording in importer queue a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
998
diff
changeset
|
196 log.Printf("info: added import #%d to queue\n", jobID) |
979
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
197 |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
198 result := struct { |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
199 ID int64 `json:"id"` |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
200 }{ |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
201 ID: jobID, |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
202 } |
7934b5c1a910
Finally enqueue sounding result import job to import jobs.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
978
diff
changeset
|
203 SendJSON(rw, http.StatusCreated, &result) |
978
544a5cfe07cd
Started with endpoint for uploading sounding result and trigger respective import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
204 } |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
205 |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
206 func loadMeta(f *zip.File) (*models.SoundingResultMeta, error) { |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
207 r, err := f.Open() |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
208 if err != nil { |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
209 return nil, err |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
210 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
211 defer r.Close() |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
212 var m models.SoundingResultMeta |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
213 return &m, m.Decode(r) |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
214 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
215 |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
216 func uploadSoundingResult( |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
217 _ interface{}, |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
218 req *http.Request, |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
219 conn *sql.Conn, |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
220 ) (jr JSONResult, err error) { |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
221 |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
222 var dir string |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
223 if dir, err = storeSoundingResult(req); err != nil { |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
224 return |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
225 } |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
226 |
1225
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
227 srFile := filepath.Join(dir, "sr.zip") |
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
228 |
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
229 var zr *zip.ReadCloser |
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
230 if zr, err = zip.OpenReader(srFile); err != nil { |
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
231 return |
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
232 } |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
233 var once sync.Once |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
234 closeOnce := func() { zr.Close() } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
235 defer once.Do(closeOnce) |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
236 |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
237 var messages []string |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
238 |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
239 var result struct { |
1227
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
240 Token string `json:"token,omitempty"` |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
241 Meta interface{} `json:"meta,omitempty"` |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
242 Messages []string `json:"messages,omitempty"` |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
243 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
244 |
1528
5874cedd7f91
Sounding result import: Accept *.txt files for XYZ data, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
245 find := func(ext string) *zip.File { return common.FindInZIP(zr, ext) } |
5874cedd7f91
Sounding result import: Accept *.txt files for XYZ data, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
246 |
5874cedd7f91
Sounding result import: Accept *.txt files for XYZ data, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
247 noXYZ := find(".xyz") == nil && find(".txt") == nil |
5874cedd7f91
Sounding result import: Accept *.txt files for XYZ data, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
248 if noXYZ { |
5874cedd7f91
Sounding result import: Accept *.txt files for XYZ data, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
249 messages = append(messages, "no .xyz or .txt file found.") |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
250 } |
1225
4d7c44f7044e
Factored out som zip lookup code to be reusable in sounding result upload controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1224
diff
changeset
|
251 |
1528
5874cedd7f91
Sounding result import: Accept *.txt files for XYZ data, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
252 if mj := find("meta.json"); mj == nil { |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
253 messages = append(messages, "no 'meta.json' file found.") |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
254 } else { |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
255 if meta, err := loadMeta(mj); err != nil { |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
256 messages = append(messages, |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
257 fmt.Sprintf("'meta.json' found but invalid: %v", err)) |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
258 } else { |
1328
d753ce6cf588
To make golint happier made context.Context to be the first argument in all calls.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1321
diff
changeset
|
259 errs := meta.Validate(req.Context(), conn) |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
260 for _, err := range errs { |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
261 messages = append(messages, |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
262 fmt.Sprintf("invalid 'meta.json': %v", err)) |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
263 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
264 result.Meta = meta |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
265 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
266 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
267 once.Do(closeOnce) |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
268 |
1227
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
269 code := http.StatusCreated |
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
270 |
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
271 // If there are no XYZ data we cant help the user anyway. |
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
272 if noXYZ { |
1228
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
273 code = http.StatusBadRequest |
1227
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
274 if err2 := os.RemoveAll(dir); err2 != nil { |
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
275 log.Printf("error: %v\n", err2) |
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
276 } |
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
277 } else if result.Token, err = misc.MakeTempFile(dir); err != nil { |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
278 if err2 := os.RemoveAll(dir); err2 != nil { |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
279 log.Printf("error: %v\n", err2) |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
280 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
281 return |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
282 } |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
283 |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
284 result.Messages = messages |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
285 |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
286 jr = JSONResult{ |
1227
737e1acea1f1
If there are no XYZ files in the ZIP we cant help the user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1226
diff
changeset
|
287 Code: code, |
1226
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
288 Result: &result, |
2e65e8ddacab
Finished the sounding result upload controller to temp uploads.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1225
diff
changeset
|
289 } |
1224
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
290 return |
bc4b642c8d04
Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1221
diff
changeset
|
291 } |
1228
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
292 |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
293 func deleteSoundingUpload(rw http.ResponseWriter, req *http.Request) { |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
294 token := mux.Vars(req)["token"] |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
295 if _, err := hex.DecodeString(token); err != nil { |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
296 http.Error(rw, "Invalid token", http.StatusBadRequest) |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
297 return |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
298 } |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
299 if err := misc.DeleteTempFile(token); err != nil { |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
300 http.Error(rw, fmt.Sprintf("error: %v", err), http.StatusInternalServerError) |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
301 return |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
302 } |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
303 result := struct { |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
304 Message string `json:"message"` |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
305 }{ |
1297
1c0c9190fcf2
Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1241
diff
changeset
|
306 Message: fmt.Sprintf("Token %s deleted.", token), |
1228
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
307 } |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
308 SendJSON(rw, http.StatusOK, &result) |
17131f0f9fcb
Added endpoint to explicitly delete a temp upload.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1227
diff
changeset
|
309 } |