Mercurial > gemma
comparison pkg/controllers/srimports.go @ 1205:5ba14f7f5bdc
merge
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Mon, 19 Nov 2018 12:40:56 +0100 |
parents | 49eead4fad3a |
children | c193649d4f11 |
comparison
equal
deleted
inserted
replaced
1204:ddfdf440da24 | 1205:5ba14f7f5bdc |
---|---|
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 | |
25 "gemma.intevation.de/gemma/pkg/auth" | |
26 "gemma.intevation.de/gemma/pkg/config" | |
27 "gemma.intevation.de/gemma/pkg/imports" | |
28 ) | |
29 | |
30 const ( | |
31 maxSoundingResultSize = 25 * 1024 * 1024 | |
32 soundingResultName = "soundingresult" | |
33 ) | |
34 | |
35 func downloadSoundingResult(req *http.Request) (string, error) { | |
36 | |
37 f, _, err := req.FormFile(soundingResultName) | |
38 if err != nil { | |
39 return "", err | |
40 } | |
41 defer f.Close() | |
42 | |
43 dir, err := ioutil.TempDir(config.TmpDir(), soundingResultName) | |
44 if err != nil { | |
45 return "", err | |
46 } | |
47 | |
48 o, err := os.Create(filepath.Join(dir, "sr.zip")) | |
49 if err != nil { | |
50 os.RemoveAll(dir) | |
51 return "", err | |
52 } | |
53 | |
54 out := bufio.NewWriter(o) | |
55 | |
56 if _, err = io.Copy(out, io.LimitReader(f, maxSoundingResultSize)); err != nil { | |
57 o.Close() | |
58 os.RemoveAll(dir) | |
59 return "", err | |
60 } | |
61 | |
62 if err = out.Flush(); err != nil { | |
63 o.Close() | |
64 os.RemoveAll(dir) | |
65 return "", err | |
66 } | |
67 | |
68 return dir, nil | |
69 } | |
70 | |
71 func importSoundingResult(rw http.ResponseWriter, req *http.Request) { | |
72 | |
73 dir, err := downloadSoundingResult(req) | |
74 if err != nil { | |
75 log.Printf("error: %v\n", err) | |
76 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) | |
77 return | |
78 } | |
79 | |
80 session, _ := auth.GetSession(req) | |
81 | |
82 jobID, err := imports.AddJob(imports.SRJobKind, session.User, dir) | |
83 if err != nil { | |
84 log.Printf("error: %v\n", err) | |
85 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) | |
86 return | |
87 } | |
88 | |
89 log.Printf("info: added import #%d to queue\n", jobID) | |
90 | |
91 result := struct { | |
92 ID int64 `json:"id"` | |
93 }{ | |
94 ID: jobID, | |
95 } | |
96 SendJSON(rw, http.StatusCreated, &result) | |
97 } |