Mercurial > gemma
comparison pkg/controllers/wpimports.go @ 2194:4d6979dedb11
Imports: Deduplicted file upload code.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 12 Feb 2019 11:09:55 +0100 |
parents | 7a3378c14397 |
children |
comparison
equal
deleted
inserted
replaced
2193:35c56ace42b9 | 2194:4d6979dedb11 |
---|---|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> | 12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
13 | 13 |
14 package controllers | 14 package controllers |
15 | 15 |
16 import ( | 16 import ( |
17 "bufio" | |
18 "fmt" | 17 "fmt" |
19 "io" | |
20 "io/ioutil" | |
21 "log" | 18 "log" |
22 "net/http" | 19 "net/http" |
23 "os" | |
24 "path/filepath" | |
25 "strconv" | 20 "strconv" |
26 "time" | 21 "time" |
27 | 22 |
28 "gemma.intevation.de/gemma/pkg/auth" | 23 "gemma.intevation.de/gemma/pkg/auth" |
29 "gemma.intevation.de/gemma/pkg/common" | 24 "gemma.intevation.de/gemma/pkg/common" |
30 "gemma.intevation.de/gemma/pkg/config" | |
31 "gemma.intevation.de/gemma/pkg/imports" | 25 "gemma.intevation.de/gemma/pkg/imports" |
26 "gemma.intevation.de/gemma/pkg/misc" | |
32 ) | 27 ) |
33 | 28 |
34 const ( | 29 const ( |
35 maxWaterwayProfilesSize = 25 * 1024 * 1024 | 30 maxWaterwayProfilesSize = 25 * 1024 * 1024 |
36 waterwayProfilesName = "wp" | 31 waterwayProfilesName = "wp" |
37 ) | 32 ) |
38 | 33 |
39 func storeWaterwayProfiles(req *http.Request) (string, error) { | |
40 | |
41 // Check for direct upload. | |
42 f, _, err := req.FormFile(waterwayProfilesName) | |
43 if err != nil { | |
44 return "", err | |
45 } | |
46 defer f.Close() | |
47 | |
48 dir, err := ioutil.TempDir(config.TmpDir(), waterwayProfilesName) | |
49 if err != nil { | |
50 return "", err | |
51 } | |
52 | |
53 o, err := os.Create(filepath.Join(dir, "wp.csv")) | |
54 if err != nil { | |
55 os.RemoveAll(dir) | |
56 return "", err | |
57 } | |
58 | |
59 out := bufio.NewWriter(o) | |
60 | |
61 if _, err = io.Copy(out, io.LimitReader(f, maxWaterwayProfilesSize)); err != nil { | |
62 o.Close() | |
63 os.RemoveAll(dir) | |
64 return "", err | |
65 } | |
66 | |
67 if err = out.Flush(); err != nil { | |
68 o.Close() | |
69 os.RemoveAll(dir) | |
70 return "", err | |
71 } | |
72 | |
73 return dir, nil | |
74 } | |
75 func importWaterwayProfiles(rw http.ResponseWriter, req *http.Request) { | 34 func importWaterwayProfiles(rw http.ResponseWriter, req *http.Request) { |
76 | 35 |
77 url := req.FormValue("url") | 36 url := req.FormValue("url") |
78 if url == "" { | 37 if url == "" { |
79 http.Error(rw, "missing 'url' parameter", http.StatusBadRequest) | 38 http.Error(rw, "missing 'url' parameter", http.StatusBadRequest) |
84 if featureType == "" { | 43 if featureType == "" { |
85 http.Error(rw, "missing 'feature-type' parameter", http.StatusBadRequest) | 44 http.Error(rw, "missing 'feature-type' parameter", http.StatusBadRequest) |
86 return | 45 return |
87 } | 46 } |
88 | 47 |
89 dir, err := storeWaterwayProfiles(req) | 48 dir, err := misc.StoreUploadedFile( |
49 req, | |
50 waterwayProfilesName, | |
51 "wp.csv", | |
52 maxWaterwayProfilesSize) | |
90 if err != nil { | 53 if err != nil { |
91 log.Printf("error: %v\n", err) | 54 log.Printf("error: %v\n", err) |
92 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) | 55 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError) |
93 return | 56 return |
94 } | 57 } |