@@ -15,16 +15,17 @@ import (
15
15
//go:embed web
16
16
var webDir embed.FS
17
17
18
+ // tmpFile struct
18
19
type tmpFile struct {
19
- T string `json:"t"`
20
- N string `json:"n"`
21
- Ns string `json:"ns"`
20
+ T string `json:"t"` // 0: normal file
21
+ N string `json:"n"` // file url
22
+ Ns string `json:"ns"` // file name
22
23
}
23
24
24
25
var (
25
26
sData string
26
- imgList []string
27
- fileList []tmpFile
27
+ imgList []string // img url list
28
+ fileList []tmpFile // file list
28
29
)
29
30
30
31
func runServer () error {
@@ -37,14 +38,14 @@ func runServer() error {
37
38
38
39
setRouter (r )
39
40
40
- // Set memory limit for multipart forms (default is 32 MiB)
41
- r .MaxMultipartMemory = 50 << 20
41
+ loadOldFiles ()
42
+
43
+ // Set memory limit for multipart forms
44
+ r .MaxMultipartMemory = 20 << 20 // 20 MiB
42
45
43
46
ads := address + ":" + port
44
47
fmt .Println ("server started at http://" + ads )
45
48
46
- loadOldFiles ()
47
-
48
49
return r .Run (ads )
49
50
50
51
}
@@ -56,19 +57,21 @@ func setRouter(r *gin.Engine) {
56
57
57
58
r .StaticFS ("/static" , http .FS (webDir ))
58
59
r .GET ("/" , func (c * gin.Context ) {
59
- c .HTML (http .StatusOK , "index.html" , gin.H {"data" : sData , "imaList " : imgList , "fileList" : fileList })
60
+ c .HTML (http .StatusOK , "index.html" , gin.H {"data" : sData , "imgList " : imgList , "fileList" : fileList })
60
61
})
61
62
62
63
r .POST ("/" , func (c * gin.Context ) {
63
64
sData = c .PostForm ("sData" )
64
- c .HTML (http .StatusOK , "index.html" , gin.H {"data" : sData , "imaList " : imgList , "fileList" : fileList })
65
+ c .HTML (http .StatusOK , "index.html" , gin.H {"data" : sData , "imgList " : imgList , "fileList" : fileList })
65
66
})
66
67
67
68
r .POST ("/clearFile" , func (c * gin.Context ) {
69
+ clearTmpFile ()
68
70
fileList = nil
69
71
imgList = nil
70
- c .HTML (http .StatusOK , "index.html" , gin.H {"data" : sData , "imaList " : imgList , "fileList" : fileList })
72
+ c .HTML (http .StatusOK , "index.html" , gin.H {"data" : sData , "imgList " : imgList , "fileList" : fileList })
71
73
})
74
+
72
75
r .POST ("/upload" , func (c * gin.Context ) {
73
76
// Single file
74
77
file , err := c .FormFile ("file" )
@@ -77,7 +80,6 @@ func setRouter(r *gin.Engine) {
77
80
c .String (http .StatusBadRequest , err .Error ())
78
81
}
79
82
80
- // saveName := strconv.FormatInt(time.Now().UnixNano(), 10) + path.Ext(file.Filename)
81
83
saveName := file .Filename
82
84
83
85
err = c .SaveUploadedFile (file , tmpFileDir + "/" + saveName )
@@ -119,7 +121,17 @@ func loadOldFiles() {
119
121
fileList = append (fileList , tmpFile {T : "0" , N : fileUrl , Ns : f .Name ()})
120
122
}
121
123
}
124
+ }
122
125
126
+ func clearTmpFile () {
127
+ err := os .RemoveAll (tmpFileDir )
128
+ if err != nil {
129
+ log .Fatal (err )
130
+ }
131
+ err = os .MkdirAll (tmpFileDir , os .ModePerm )
132
+ if err != nil {
133
+ log .Fatal (err )
134
+ }
123
135
}
124
136
125
137
func isImgSimple (name string ) bool {
0 commit comments