/
files.go
93 lines (76 loc) · 2.1 KB
/
files.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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"time"
)
type UploadReq struct {
FileLocation string `form:"file_location" json:"file_location" binding:"required"`
DownloadOnLoad string `form:"download_on_load" json:"download_on_load"`
WebRoute string `form:"web_route" binding:"required" json:"web_route"`
LocationFromRoot string `form:"location_from_root" json:"location_from_route"`
}
func UploadFile(c *gin.Context) {
var form UploadReq
if err := c.ShouldBind(&form); err != nil {
c.HTML(http.StatusBadRequest, "500.html", gin.H{"error": err.Error()})
log.Println("400: " + err.Error())
return
}
f, err := c.FormFile("file")
if err != nil {
c.HTML(http.StatusBadRequest, "500.html", gin.H{"error": err.Error()})
log.Println("400 file: " + err.Error())
return
}
if form.LocationFromRoot != "on" {
form.FileLocation = UploadsFolder + "/" + form.FileLocation
}
file := IFile{
FileLocation: form.FileLocation,
DownloadOnLoad: form.DownloadOnLoad == "on",
WebRoute: form.WebRoute,
DateCreated: time.Now().Unix(),
}
if f != nil {
err := c.SaveUploadedFile(f, form.FileLocation)
if err != nil {
c.HTML(http.StatusInternalServerError, "500.html", gin.H{})
_, _ = fmt.Fprintf(os.Stderr, err.Error() + "\n")
return
}
}
StoreFile(&file)
c.Redirect(302, "/manage")
}
func ServeFile(c *gin.Context) {
if routeCache[c.Param("route")] == nil {
c.HTML(http.StatusNotFound, "404.html", gin.H{})
return
}
file := *routeCache[c.Param("route")]
// TODO file is not found
f, err := os.Open(file.FileLocation)
if err != nil {
log.Println(err.Error())
c.HTML(http.StatusInternalServerError, "500.html", gin.H{})
return
}
contentType, err := GetFileContentType(f)
if err != nil {
log.Println(err.Error())
c.HTML(http.StatusInternalServerError, "500.html", gin.H{})
return
}
//c.Header("Content-Transfer-Encoding", "binary")
if file.DownloadOnLoad {
c.Header("Content-Disposition", "attachment")
} else {
c.Header("Content-Disposition", "inline")
}
c.Header("Content-Type", contentType)
c.File(file.FileLocation)
}