-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
plugin_realpenalty.go
165 lines (127 loc) · 4.05 KB
/
plugin_realpenalty.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package servermanager
import (
"archive/zip"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/sirupsen/logrus"
)
// handles configuration of the realpenalty plugin
// https://www.racedepartment.com/downloads/real-penalty-tool.29591/
const (
realPenaltyBaseFolderName = "realpenalty"
)
func RealPenaltyExecutablePath() string {
if runtime.GOOS == "windows" {
return filepath.Join(RealPenaltyFolderPath(), "ac_penalty.exe")
}
return filepath.Join(RealPenaltyFolderPath(), "ac_penalty")
}
func fixRealPenaltyExecutablePermissions() error {
if runtime.GOOS == "linux" {
return os.Chmod(RealPenaltyExecutablePath(), 0755)
}
return nil
}
func RealPenaltyFolderPath() string {
return filepath.Join(ServerInstallPath, realPenaltyBaseFolderName)
}
func IsRealPenaltyInstalled() bool {
if _, err := os.Stat(RealPenaltyExecutablePath()); os.IsNotExist(err) {
return false
} else if err != nil {
logrus.WithError(err).Error("Could not determine if realpenalty is enabled")
return false
} else {
return true
}
}
type RealPenaltyHandler struct {
*BaseHandler
store Store
}
func NewRealPenaltyHandler(baseHandler *BaseHandler, store Store) *RealPenaltyHandler {
return &RealPenaltyHandler{BaseHandler: baseHandler, store: store}
}
type realPenaltyConfigurationTemplateVars struct {
BaseTemplateVars
Form template.HTML
IsRealPenaltyInstalled bool
RealPenaltySupportedVersion string
}
func (rph *RealPenaltyHandler) options(w http.ResponseWriter, r *http.Request) {
realPenaltyOptions, err := rph.store.LoadRealPenaltyOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load real penalty options")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if r.Method == http.MethodPost {
err := DecodeFormData(realPenaltyOptions, r)
if err != nil {
logrus.WithError(err).Errorf("couldn't submit form")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = rph.store.UpsertRealPenaltyOptions(realPenaltyOptions)
if err != nil {
logrus.WithError(err).Errorf("couldn't save Real Penalty options")
AddErrorFlash(w, r, "Failed to save Real Penalty options")
} else {
AddFlash(w, r, "Real Penalty options successfully saved!")
}
}
form, err := EncodeFormData(realPenaltyOptions, r)
if err != nil {
logrus.WithError(err).Errorf("Couldn't encode form data")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
rph.viewRenderer.MustLoadTemplate(w, r, "server/realpenalty-options.html", &realPenaltyConfigurationTemplateVars{
Form: form,
IsRealPenaltyInstalled: IsRealPenaltyInstalled(),
RealPenaltySupportedVersion: RealPenaltySupportedVersion,
})
}
func (rph *RealPenaltyHandler) downloadLogs(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment;filename="realpenalty_logs_%s.zip"`, time.Now().Format("2006-01-02_15_04")))
w.Header().Set("Content-Type", "application/zip")
if err := rph.buildLogZip(w); err != nil {
logrus.WithError(err).Errorf("Could not create real penalty log zip")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
func (rph *RealPenaltyHandler) buildLogZip(w io.Writer) error {
z := zip.NewWriter(w)
defer z.Close()
logFiles, err := ioutil.ReadDir(filepath.Join(RealPenaltyFolderPath(), "logs"))
if err != nil {
return err
}
for _, file := range logFiles {
if err := rph.writeRealPenaltyLogFileToZip(z, file); err != nil {
return err
}
}
return nil
}
func (rph *RealPenaltyHandler) writeRealPenaltyLogFileToZip(z *zip.Writer, info os.FileInfo) error {
zf, err := z.Create(info.Name())
if err != nil {
return err
}
f, err := os.Open(filepath.Join(filepath.Join(RealPenaltyFolderPath(), "logs"), info.Name()))
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(zf, f)
return err
}