comparison pkg/controllers/srimports.go @ 1221:c193649d4f11

Add an area for temp uploads on the server to be addressed by tokens. If unused they will me thrown away after 45 minutes. There could be max 100 of them. If there are more to upload the oldest are removed first.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 19 Nov 2018 16:15:30 +0100
parents 49eead4fad3a
children bc4b642c8d04
comparison
equal deleted inserted replaced
1220:d11d5e39c8d0 1221:c193649d4f11
13 13
14 package controllers 14 package controllers
15 15
16 import ( 16 import (
17 "bufio" 17 "bufio"
18 "encoding/hex"
18 "io" 19 "io"
19 "io/ioutil" 20 "io/ioutil"
20 "log" 21 "log"
21 "net/http" 22 "net/http"
22 "os" 23 "os"
23 "path/filepath" 24 "path/filepath"
25 "time"
24 26
25 "gemma.intevation.de/gemma/pkg/auth" 27 "gemma.intevation.de/gemma/pkg/auth"
26 "gemma.intevation.de/gemma/pkg/config" 28 "gemma.intevation.de/gemma/pkg/config"
27 "gemma.intevation.de/gemma/pkg/imports" 29 "gemma.intevation.de/gemma/pkg/imports"
30 "gemma.intevation.de/gemma/pkg/misc"
28 ) 31 )
29 32
30 const ( 33 const (
31 maxSoundingResultSize = 25 * 1024 * 1024 34 maxSoundingResultSize = 25 * 1024 * 1024
32 soundingResultName = "soundingresult" 35 soundingResultName = "soundingresult"
33 ) 36 )
34 37
35 func downloadSoundingResult(req *http.Request) (string, error) { 38 func downloadSoundingResult(req *http.Request) (string, error) {
36 39
40 // Check first if we have a token.
41 if token := req.FormValue("token"); token != "" {
42 if _, err := hex.DecodeString(token); err != nil {
43 return "", err
44 }
45 dir := config.TmpDir()
46 if dir == "" {
47 dir = os.TempDir()
48 }
49 // XXX: This should hopefully be race-free enough.
50 now := time.Now().Format("2006-15-04-05")
51 dst := filepath.Join(dir, soundingResultName+"-"+token+"-"+now)
52 if err := misc.UnmakeTempFile(token, dst); err != nil {
53 return "", err
54 }
55 }
56
57 // Check for direct upload.
37 f, _, err := req.FormFile(soundingResultName) 58 f, _, err := req.FormFile(soundingResultName)
38 if err != nil { 59 if err != nil {
39 return "", err 60 return "", err
40 } 61 }
41 defer f.Close() 62 defer f.Close()