/
unencrypted.go
181 lines (161 loc) · 4.77 KB
/
unencrypted.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
package views
import (
"bytes"
"bufio"
"fmt"
"io"
"net/http"
"os"
"path"
"time"
"github.com/dchest/uniuri"
"github.com/gin-gonic/gin"
"github.com/Depado/goploader/server/conf"
"github.com/Depado/goploader/server/logger"
"github.com/Depado/goploader/server/models"
"github.com/Depado/goploader/server/utils"
)
// Create handles the multipart form upload and creates a file
func Create(c *gin.Context) {
var err error
var duration time.Duration
var once bool
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, conf.C.SizeLimit*utils.MegaByte)
once = c.PostForm("once") != ""
d := c.DefaultPostForm("duration", "1d")
if val, ok := models.DurationMap[d]; ok {
duration = val
} else {
logger.ErrC(c, "server", "Invalid duration", d)
c.String(http.StatusBadRequest, "Invalid duration\n")
c.AbortWithStatus(http.StatusBadRequest)
return
}
fd, h, err := c.Request.FormFile("file")
if err != nil {
logger.ErrC(c, "server", "Couldn't read file", err)
c.String(http.StatusRequestEntityTooLarge, "Entity is too large (Max : %v MB)\n", conf.C.SizeLimit)
c.AbortWithStatus(http.StatusRequestEntityTooLarge)
return
}
defer fd.Close()
u := uniuri.NewLen(conf.C.UniURILength)
file, err := os.Create(path.Join(conf.C.UploadDir, u))
if err != nil {
logger.ErrC(c, "server", "Couldn't create file", err)
c.String(http.StatusInternalServerError, "Something went wrong on the server side. Try again later.")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
defer file.Close()
wr, err := io.Copy(file, bufio.NewReaderSize(fd, 512))
if err != nil {
logger.ErrC(c, "server", "Couldn't write file", err)
c.String(http.StatusInternalServerError, "Something went wrong on the server side. Try again later.")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if conf.C.DiskQuota > 0 {
if models.S.CurrentSize+uint64(wr) > uint64(conf.C.DiskQuota*utils.GigaByte) {
logger.ErrC(c, "server", "Quota exceeded")
c.String(http.StatusBadRequest, "Not enough free space. Try again later.")
c.AbortWithStatus(http.StatusBadRequest)
os.Remove(path.Join(conf.C.UploadDir, u))
return
}
}
del := time.Now().Add(duration)
newres := &models.Resource{
Key: u,
Name: h.Filename,
Once: once,
DeleteAt: del,
UnixDeleteAt: del.Unix(),
Size: wr,
}
if err = newres.Save(); err != nil {
logger.ErrC(c, "server", "Couldn't save in database", err)
c.String(http.StatusInternalServerError, "Something went wrong on the server. Try again later.")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
newres.LogCreated(c)
ns := conf.C.NameServer
if conf.C.AppendPort {
ns = fmt.Sprintf("%s:%d", conf.C.NameServer, conf.C.Port)
}
c.String(http.StatusCreated, "%v://%s/v/%s\n", utils.DetectScheme(c), ns, u)
}
// HandleRequest responds to a View or Head request for unencrypted files
func HandleRequest(c *gin.Context, isView bool) {
var err error
id := c.Param("uniuri")
re := models.Resource{}
if err = re.Get(id); err != nil || re.Key == "" {
logger.InfoC(c, "server", "Not found", id)
c.AbortWithStatus(http.StatusNotFound)
return
}
re.LogFetched(c)
if conf.C.AlwaysDownload {
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=\""+re.Name+"\"")
} else {
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
}
file := path.Join(conf.C.UploadDir, re.Key)
if _, err := os.Stat(file); err != nil {
logger.ErrC(c, "server", fmt.Sprintf("Couldn't open %s", re.Key), err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.File(file)
if isView && re.Once {
re.Delete()
re.LogDeleted(c)
}
}
// View handles the file views
func View(c *gin.Context) {
HandleRequest(c, true)
}
// Head handles the head request
func Head(c *gin.Context) {
HandleRequest(c, false)
}
// ViewCode allows to see the file with syntax highliting and extra options
func ViewCode(c *gin.Context) {
var err error
id := c.Param("uniuri")
lang := c.Param("lang")
theme := c.DefaultQuery("theme", "dark")
lines := c.Query("lines") == "true"
re := models.Resource{}
if err = re.Get(id); err != nil || re.Key == "" {
logger.InfoC(c, "server", "Not found", id)
c.AbortWithStatus(http.StatusNotFound)
return
}
re.LogFetched(c)
f, err := os.Open(path.Join(conf.C.UploadDir, re.Key))
if err != nil {
logger.ErrC(c, "server", fmt.Sprintf("Couldn't open %s", re.Key), err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
buf := new(bytes.Buffer)
buf.ReadFrom(f)
bb := buf.Bytes()
c.HTML(http.StatusOK, "code.tmpl", gin.H{
"code": string(bb),
"lang": lang,
"theme": theme,
"lines": lines,
"name": re.Name,
})
if re.Once {
re.Delete()
re.LogDeleted(c)
}
}