Mercurial > gemma
comparison pkg/controllers/report.go @ 5321:0919946f624b extented-report
Added a report controller.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 27 May 2021 01:22:10 +0200 |
parents | |
children | 80d9fd782f00 |
comparison
equal
deleted
inserted
replaced
5320:866eae1bd888 | 5321:0919946f624b |
---|---|
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) 2021 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 "fmt" | |
18 "log" | |
19 "net/http" | |
20 "os" | |
21 "path/filepath" | |
22 | |
23 "gemma.intevation.de/gemma/pkg/config" | |
24 "gemma.intevation.de/gemma/pkg/middleware" | |
25 "gemma.intevation.de/gemma/pkg/xlsx" | |
26 | |
27 "github.com/360EntSecGroup-Skylar/excelize/v2" | |
28 "github.com/gorilla/mux" | |
29 ) | |
30 | |
31 func report(rw http.ResponseWriter, req *http.Request) { | |
32 | |
33 vars := mux.Vars(req) | |
34 | |
35 name := vars["name"] | |
36 | |
37 config.WaitReady() | |
38 path := config.ReportPath() | |
39 if path == "" { | |
40 http.NotFound(rw, req) | |
41 return | |
42 } | |
43 | |
44 stat, err := os.Stat(path) | |
45 if err != nil { | |
46 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | |
47 return | |
48 } | |
49 | |
50 if !stat.Mode().IsDir() { | |
51 http.NotFound(rw, req) | |
52 return | |
53 } | |
54 | |
55 xlsxFilename := filepath.Join(path, name+".xlsx") | |
56 yamlFilename := filepath.Join(path, name+".yaml") | |
57 | |
58 _, errX := os.Stat(xlsxFilename) | |
59 _, errY := os.Stat(yamlFilename) | |
60 if errX != nil || errX != nil { | |
61 if (errX != nil && os.IsNotExist(errX)) || (errY != nil && os.IsNotExist(errY)) { | |
62 http.NotFound(rw, req) | |
63 } else { | |
64 http.Error(rw, "Something is wrong", http.StatusInternalServerError) | |
65 } | |
66 return | |
67 } | |
68 | |
69 template, err := excelize.OpenFile(xlsxFilename) | |
70 if err != nil { | |
71 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | |
72 log.Printf("error: %v\n", err) | |
73 return | |
74 } | |
75 | |
76 action, err := xlsx.ActionFromFile(yamlFilename) | |
77 if err != nil { | |
78 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | |
79 log.Printf("error: %v\n", err) | |
80 return | |
81 } | |
82 | |
83 ctx := req.Context() | |
84 conn := middleware.GetDBConn(req) | |
85 | |
86 if err := action.Execute(ctx, conn, template); err != nil { | |
87 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | |
88 log.Printf("error: %v\n", err) | |
89 return | |
90 } | |
91 rw.Header().Set( | |
92 "Content-Disposition", | |
93 fmt.Sprintf("attachment; filename=%s.xlsx", name)) | |
94 rw.Header().Set( | |
95 "Content-Type", | |
96 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") | |
97 | |
98 if _, err := template.WriteTo(rw); err != nil { | |
99 log.Printf("error: %v\n", err) | |
100 } | |
101 } |