forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_upload.go
152 lines (117 loc) · 3.47 KB
/
content_upload.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
package servermanager
import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/sirupsen/logrus"
)
type ContentType string
const (
ContentTypeCar = "Car"
ContentTypeTrack = "Track"
ContentTypeWeather = "Weather"
)
type ContentFile struct {
Name string `json:"name"`
FileType string `json:"type"`
FilePath string `json:"filepath"`
Data string `json:"dataBase64"`
Size int `json:"size"`
}
var base64HeaderRegex = regexp.MustCompile("^(data:.+;base64,)")
type ContentUploadHandler struct {
*BaseHandler
carManager *CarManager
}
func NewContentUploadHandler(baseHandler *BaseHandler, carManager *CarManager) *ContentUploadHandler {
return &ContentUploadHandler{
BaseHandler: baseHandler,
carManager: carManager,
}
}
// Stores Files encoded into r.Body
func (cuh *ContentUploadHandler) upload(contentType ContentType) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var files []ContentFile
err := json.NewDecoder(r.Body).Decode(&files)
if err != nil {
logrus.WithError(err).Errorf("could not decode %s json", contentType)
return
}
err = cuh.addFiles(files, contentType)
if err != nil {
logrus.WithError(err).Error("couldn't upload file")
AddErrorFlash(w, r, string(contentType)+"(s) could not be added")
return
}
AddFlash(w, r, string(contentType)+"(s) added successfully!")
}
}
// Stores files in the correct location
func (cuh *ContentUploadHandler) addFiles(files []ContentFile, contentType ContentType) error {
var contentPath string
switch contentType {
case ContentTypeTrack:
contentPath = filepath.Join(ServerInstallPath, "content", "tracks")
case ContentTypeCar:
contentPath = filepath.Join(ServerInstallPath, "content", "cars")
case ContentTypeWeather:
contentPath = filepath.Join(ServerInstallPath, "content", "weather")
}
uploadedCars := make(map[string]bool)
for _, file := range files {
var fileDecoded []byte
if file.Size > 0 {
// zero-size files will still be created, just with no content. (some data files exist but are empty)
var err error
fileDecoded, err = base64.StdEncoding.DecodeString(base64HeaderRegex.ReplaceAllString(file.Data, ""))
if err != nil {
logrus.WithError(err).Errorf("could not decode %s file data", contentType)
return err
}
}
// If user uploaded a "tracks" or "cars" folder containing multiple tracks
parts := strings.Split(file.FilePath, "/")
if parts[0] == "tracks" || parts[0] == "cars" || parts[0] == "weather" {
parts = parts[1:]
file.FilePath = ""
for _, part := range parts {
file.FilePath = filepath.Join(file.FilePath, part)
}
}
path := filepath.Join(contentPath, file.FilePath)
// Makes any directories in the path that don't exist (there can be multiple)
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
logrus.WithError(err).Errorf("could not create %s file directory", contentType)
return err
}
if contentType == ContentTypeCar {
uploadedCars[parts[0]] = true
}
err = ioutil.WriteFile(path, fileDecoded, 0644)
if err != nil {
logrus.WithError(err).Error("could not write file")
return err
}
}
if contentType == ContentTypeCar {
// index the cars that have been uploaded.
for car := range uploadedCars {
car, err := cuh.carManager.LoadCar(car, nil)
if err != nil {
return err
}
err = cuh.carManager.IndexCar(car)
if err != nil {
return err
}
}
}
return nil
}