forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_setups.go
219 lines (158 loc) · 4.86 KB
/
content_setups.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package servermanager
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/cj123/ini"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
)
// CarSetups is a map of car name to the setups for that car.
// it is a map of car name => track name => []setup
type CarSetups map[string]map[string][]string
func ListAllSetups() (CarSetups, error) {
setupDirectory := filepath.Join(ServerInstallPath, "setups")
if _, err := os.Stat(setupDirectory); os.IsNotExist(err) {
err := os.MkdirAll(setupDirectory, 0755)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
setups := make(CarSetups)
// multi server installs may use symbolic links for setups folder, which filepath.Walk will ignore
setupDirectory, err := filepath.EvalSymlinks(setupDirectory)
if err != nil {
setupDirectory = filepath.Join(ServerInstallPath, "setups")
}
err = filepath.Walk(setupDirectory, func(path string, file os.FileInfo, _ error) error {
if file.IsDir() || filepath.Ext(file.Name()) != ".ini" {
return nil
}
// corner case of stray ini files winding up in the top level of the folder
if filepath.ToSlash(filepath.Dir(path)) == filepath.ToSlash(setupDirectory) {
return nil
}
// read the setup file to get the car name
name, err := getCarNameFromSetup(path)
if err != nil {
logrus.WithError(err).Errorf("Could not get car name from setup file %s", file.Name())
return nil
}
if setups[name] == nil {
setups[name] = make(map[string][]string)
}
trackParts := strings.Split(filepath.ToSlash(filepath.Dir(path)), "/")
if len(trackParts) > 0 {
trackName := trackParts[len(trackParts)-1]
if trackName == lockedTyreSetupFolder {
return nil // don't list locked tyre setup folder
}
setups[name][trackName] = append(setups[name][trackName], file.Name())
}
return nil
})
if err != nil {
return nil, err
}
return setups, nil
}
func ListSetupsForCar(model string) (map[string][]string, error) {
setups := make(map[string][]string)
setupDirectory := filepath.Join(ServerInstallPath, "setups", model)
if _, err := os.Stat(setupDirectory); os.IsNotExist(err) {
return setups, nil
} else if err != nil {
return nil, err
}
err := filepath.Walk(setupDirectory, func(path string, file os.FileInfo, _ error) error {
if file.IsDir() || filepath.Ext(file.Name()) != ".ini" {
return nil
}
trackParts := strings.Split(filepath.ToSlash(filepath.Dir(path)), "/")
if len(trackParts) > 0 {
trackName := trackParts[len(trackParts)-1]
if trackName == lockedTyreSetupFolder {
return nil // don't list locked tyre setup folder
}
setups[trackName] = append(setups[trackName], file.Name())
}
return nil
})
if err != nil {
return nil, err
}
return setups, nil
}
func getCarNameFromSetup(setupFile interface{}) (string, error) {
i, err := ini.Load(setupFile)
if err != nil {
return "", err
}
car, err := i.GetSection("CAR")
if err != nil {
return "", err
}
name, err := car.GetKey("MODEL")
if err != nil {
return "", err
}
return name.String(), nil
}
func uploadCarSetup(r *http.Request) (string, error) {
track := r.FormValue("Track")
uploadedFile, header, err := r.FormFile("SetupFile")
if err != nil {
return "", err
}
defer uploadedFile.Close()
carName, err := getCarNameFromSetup(uploadedFile)
if err != nil {
return carName, err
}
_, err = uploadedFile.Seek(0, 0)
if err != nil {
return carName, err
}
saveFilepath := filepath.Join(ServerInstallPath, "setups", carName, track)
if err := os.MkdirAll(saveFilepath, 0755); err != nil {
return carName, err
}
savedFile, err := os.Create(filepath.Join(saveFilepath, header.Filename))
if err != nil {
return carName, err
}
defer savedFile.Close()
_, err = io.Copy(savedFile, uploadedFile)
return carName, err
}
func carSetupsUploadHandler(w http.ResponseWriter, r *http.Request) {
if carName, err := uploadCarSetup(r); err != nil {
logrus.WithError(err).Errorf("Could not upload setup file")
if carName != "" {
AddErrorFlash(w, r, fmt.Sprintf("Unable to upload setup file for %s", carName))
} else {
AddErrorFlash(w, r, "Unable to upload setup file")
}
} else {
AddFlash(w, r, fmt.Sprintf("The setup file for %s was uploaded successfully!", carName))
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
func carSetupDeleteHandler(w http.ResponseWriter, r *http.Request) {
carName := chi.URLParam(r, "car")
trackName := chi.URLParam(r, "track")
setupName := chi.URLParam(r, "setup")
err := os.RemoveAll(filepath.Join(ServerInstallPath, "setups", carName, trackName, setupName))
if err != nil {
logrus.WithError(err).Errorf("Could not remove setup %s/%s/%s", carName, trackName, setupName)
AddErrorFlash(w, r, "Couldn't delete setup for "+carName)
} else {
AddFlash(w, r, "Setup successfully deleted!")
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
}