-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (140 loc) · 3.74 KB
/
main.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
package main
import (
"errors"
"fmt"
limits "github.com/gin-contrib/size"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid/v5"
"github.com/minecrafthopper/pasteforhelp/static"
"github.com/minecrafthopper/pasteforhelp/webtemplates"
"github.com/spf13/cast"
"html/template"
"io"
"net/http"
"os"
)
var storageDirPath, _ = os.LookupEnv("STORAGE_DIR")
var storageSystem FileServer
var maxFileSizeEnv, _ = os.LookupEnv("MAX_FILE_SIZE")
var maxFileSize int64 = 1024 /*b*/ * 1024 /*kb*/ * 100 /*mb*/
func main() {
if i := cast.ToInt64(maxFileSizeEnv); i > 0 {
maxFileSize = i
}
var err error
storageSystem, err = NewFileServer(storageDirPath)
if err != nil {
panic(err)
}
defer Close(storageSystem)
r := gin.Default()
r.Use(limits.RequestSizeLimiter(maxFileSize))
r.StaticFS("/static", http.FS(static.AssetFiles))
indexTemplate := template.Must(template.ParseFS(webtemplates.WebFiles, "base.html", "index.html"))
uploadTemplate := template.Must(template.ParseFS(webtemplates.WebFiles, "base.html", "upload.html"))
r.GET("/", func(c *gin.Context) {
c.Status(http.StatusOK)
err := indexTemplate.ExecuteTemplate(c.Writer, "index.html", gin.H{})
if err != nil {
fmt.Printf("%s\n", err)
}
})
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.AssetFiles))
r.GET("/upload", func(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/")
})
r.GET("/upload/:id", func(c *gin.Context) {
id := c.Param("id")
file, err := storageSystem.OpenFile(id, os.O_RDONLY, 0644)
if err != nil {
if os.IsNotExist(err) {
//c.AbortWithStatus(http.StatusNotFound)
c.Redirect(http.StatusTemporaryRedirect, "/")
} else {
fmt.Printf("%s\n", err)
c.AbortWithStatus(http.StatusInternalServerError)
}
return
}
defer Close(file)
reader := io.LimitReader(file, maxFileSize)
data, err := io.ReadAll(reader)
if err != nil {
fmt.Printf("%s\n", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
err = uploadTemplate.ExecuteTemplate(c.Writer, "upload.html", map[string]interface{}{
"id": id,
"content": string(data),
})
if err != nil {
fmt.Printf("%s\n", err)
}
})
r.POST("/upload", func(c *gin.Context) {
if len(c.Errors) > 0 {
return
}
id := RandomString()
var input = c.PostForm("content")
file, err := storageSystem.OpenFile(id, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
if os.IsNotExist(err) {
c.Redirect(http.StatusTemporaryRedirect, "/")
return
}
fmt.Printf("%s\n", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
defer Close(file)
_, err = file.WriteString(input)
if err != nil {
fmt.Printf("%s\n", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Redirect(http.StatusFound, "/upload/"+id)
})
r.GET("/raw/:id", func(c *gin.Context) {
id := c.Param("id")
file, err := storageSystem.OpenFile(id, os.O_RDONLY, 0644)
if err != nil {
if os.IsNotExist(err) {
c.AbortWithStatus(http.StatusNotFound)
} else {
fmt.Printf("%s\n", err)
c.AbortWithStatus(http.StatusInternalServerError)
}
return
}
defer Close(file)
reader := io.LimitReader(file, maxFileSize)
data, err := io.ReadAll(reader)
if err != nil {
fmt.Printf("%s\n", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
c.Data(200, "text/plain; charset=utf-8", data)
})
err = r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
if err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}
func Close(c io.Closer) {
if c != nil {
_ = c.Close()
}
}
func RandomString() string {
gen, err := uuid.NewV4()
if err != nil {
panic(err)
}
return gen.String()[:8]
}