-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
image.go
234 lines (193 loc) · 5.96 KB
/
image.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"io/ioutil"
"os"
"net/http"
"path/filepath"
"io"
"encoding/json"
"strings"
"fmt"
"github.com/gorilla/mux"
"github.com/azukaar/cosmos-server/src/utils"
"github.com/azukaar/cosmos-server/src/docker"
)
var validExtensions = map[string]bool{
".jpg": true,
".jpeg": true,
".png": true,
".gif": true,
".bmp": true,
".svg": true,
".webp": true,
".tiff": true,
".avif": true,
}
func UploadImage(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
vars := mux.Vars(req)
originalName := vars["name"]
name := originalName + "-" + utils.GenerateRandomString(6)
// if name includes / or ..
if filepath.Clean(name) != name || strings.Contains(name, "/") {
utils.HTTPError(w, "Invalid file name", http.StatusBadRequest, "FILE002")
return
}
if(req.Method == "POST") {
// if the uploads directory does not exist, create it
if _, err := os.Stat(utils.CONFIGFOLDER + "/uploads"); os.IsNotExist(err) {
os.Mkdir(utils.CONFIGFOLDER + "/uploads", 0750)
}
// parse the form data
err := req.ParseMultipartForm(1 << 20)
if err != nil {
utils.HTTPError(w, "Error parsing form data", http.StatusInternalServerError, "FORM001")
return
}
// retrieve the file part of the form
file, header, err := req.FormFile("image")
if err != nil {
utils.HTTPError(w, "Error retrieving file from form data", http.StatusInternalServerError, "FORM002")
return
}
defer file.Close()
// get the file extension
ext := filepath.Ext(header.Filename)
if !validExtensions[ext] {
utils.HTTPError(w, "Invalid file extension " + ext, http.StatusBadRequest, "FILE001")
return
}
// create a new file in the config directory
dst, err := os.Create(utils.CONFIGFOLDER + "/uploads/" + name + ext)
if err != nil {
utils.HTTPError(w, "Error creating destination file", http.StatusInternalServerError, "FILE004")
return
}
defer dst.Close()
// copy the uploaded file to the destination file
if _, err := io.Copy(dst, file); err != nil {
utils.HTTPError(w, "Error writing to destination file", http.StatusInternalServerError, "FILE005")
return
}
// return a response to the client
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"data": map[string]interface{}{
"path": "/cosmos/api/image/" + name + ext,
"filename": header.Filename,
"size": header.Size,
"extension": ext,
},
})
} else {
utils.Error("UploadBackground: Method not allowed - " + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
func GetImage(w http.ResponseWriter, req *http.Request) {
if utils.LoggedInOnly(w, req) != nil {
return
}
utils.Log("API: GetImage")
vars := mux.Vars(req)
name := vars["name"]
// if name includes / or ..
if filepath.Clean(name) != name || strings.Contains(name, "/") {
utils.Error("GetBackground: Invalid file name - " + name, nil)
utils.HTTPError(w, "Invalid file name", http.StatusBadRequest, "FILE002")
return
}
// get the file extension
ext := filepath.Ext(name)
if !validExtensions[ext] {
utils.Error("GetBackground: Invalid file extension - " + ext, nil)
utils.HTTPError(w, "Invalid file extension", http.StatusBadRequest, "FILE001")
return
}
if(req.Method == "GET") {
// get the background image
bg, err := ioutil.ReadFile(utils.CONFIGFOLDER + "/uploads/" + name)
if err != nil {
utils.Error("GetBackground: Error reading image - " + name, err)
utils.HTTPError(w, "Error reading image", http.StatusInternalServerError, "FILE003")
return
}
// return a response to the client
w.Header().Set("Content-Type", "image/" + ext)
w.Write(bg)
} else {
utils.Error("GetBackground: Method not allowed - " + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
func imageCleanUp() {
utils.Log("Image cleanup")
config := utils.GetMainConfig()
images := map[string]bool{}
images[config.HomepageConfig.Background] = true
for _, route := range config.HTTPConfig.ProxyConfig.Routes {
if(route.Icon != "") {
images[route.Icon] = true
}
}
// get containers
containers, err := docker.ListContainers()
if err != nil {
utils.Error("Image cleanup: Error getting containers", err)
return
}
for _, container := range containers {
if(container.Labels["cosmos-icon"] != "") {
images[container.Labels["cosmos-icon"]] = true
}
}
fmt.Println(images)
// if the uploads directory does not exist, return
if _, err := os.Stat(utils.CONFIGFOLDER + "/uploads"); os.IsNotExist(err) {
return
}
// get the files in the uploads directory
files, err := ioutil.ReadDir(utils.CONFIGFOLDER + "/uploads")
if err != nil {
utils.Error("Image cleanup: Error reading directory", err)
return
}
// loop through the files
base := "/cosmos/api/image/"
for _, f := range files {
if(!images[base + f.Name()]) {
err := os.Remove(utils.CONFIGFOLDER + "/uploads/" + f.Name())
if err != nil {
utils.Error("Image cleanup: Error removing file", err)
}
}
}
}
func MigratePre013() {
// read background from config.json
config := utils.ReadConfigFromFile()
bg := config.HomepageConfig.Background
utils.Debug("MigratePre013: background is" + bg)
// if the background start with /cosmos/api/background/
if(strings.HasPrefix(bg, "/cosmos/api/background/")) {
// get the background name
ext := strings.TrimPrefix(bg, "/cosmos/api/background/")
bgPath := utils.CONFIGFOLDER + "/background." + ext
utils.Debug("MigratePre013: background path is" + bgPath)
// if the background file exists
if _, err := os.Stat(bgPath); err == nil {
// move the background file to uploads
err := os.Rename(bgPath, utils.CONFIGFOLDER + "/uploads/background." + ext)
if err != nil {
utils.Error("MigratePre013: Error moving background file", err)
}
// update the background path
config.HomepageConfig.Background = "/cosmos/api/image/background." + ext
utils.SetBaseMainConfig(config)
}
}
}