-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
142 lines (114 loc) · 2.81 KB
/
http.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
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
//go:embed web
var webDir embed.FS
// tmpFile struct
type tmpFile struct {
T string `json:"t"` // 0: normal file
N string `json:"n"` // file url
Ns string `json:"ns"` // file name
}
var (
sData string
imgList []string // img url list
fileList []tmpFile // file list
)
func runServer() error {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
err := r.SetTrustedProxies(nil)
if err != nil {
panic(err)
}
setRouter(r)
loadOldFiles()
// Set memory limit for multipart forms
r.MaxMultipartMemory = 20 << 20 // 20 MiB
ads := address + ":" + port
fmt.Println("server started at http://" + ads)
return r.Run(ads)
}
func setRouter(r *gin.Engine) {
tempHtml := template.Must(template.New("").ParseFS(webDir, "web/*.html"))
r.SetHTMLTemplate(tempHtml)
r.StaticFS("/static", http.FS(webDir))
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imgList": imgList, "fileList": fileList})
})
r.POST("/", func(c *gin.Context) {
sData = c.PostForm("sData")
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imgList": imgList, "fileList": fileList})
})
r.POST("/clearFile", func(c *gin.Context) {
clearTmpFile()
fileList = nil
imgList = nil
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imgList": imgList, "fileList": fileList})
})
r.POST("/upload", func(c *gin.Context) {
// Single file
file, err := c.FormFile("file")
if err != nil {
fmt.Println(err)
c.String(http.StatusBadRequest, err.Error())
}
saveName := file.Filename
err = c.SaveUploadedFile(file, tmpFileDir+"/"+saveName)
if err != nil {
fmt.Println(err)
c.String(http.StatusBadRequest, err.Error())
}
fileUrl := "tFile/" + saveName
v, e := c.GetPostForm("isImg")
if !e {
v = "0"
}
if v == "1" {
imgList = append([]string{fileUrl}, imgList...)
} else {
fileList = append(fileList, tmpFile{T: "0", N: fileUrl, Ns: file.Filename})
}
c.String(http.StatusOK, fileUrl)
})
r.Static("/tFile", "./"+tmpFileDir)
}
// load files in tmpFileDir
func loadOldFiles() {
files, err := os.ReadDir(tmpFileDir)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
fileUrl := "tFile/" + f.Name()
if isImgSimple(f.Name()) {
imgList = append([]string{fileUrl}, imgList...)
} else {
fileList = append(fileList, tmpFile{T: "0", N: fileUrl, Ns: f.Name()})
}
}
}
func clearTmpFile() {
err := os.RemoveAll(tmpFileDir)
if err != nil {
log.Fatal(err)
}
err = os.MkdirAll(tmpFileDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}
}
func isImgSimple(name string) bool {
if filepath.Ext(name) == ".jpg" || filepath.Ext(name) == ".png" || filepath.Ext(name) == ".jpeg" {
return true
}
return false
}