-
Notifications
You must be signed in to change notification settings - Fork 2
/
uploader.go
102 lines (85 loc) · 2.34 KB
/
uploader.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
package main
import (
"bytes"
"html/template"
"io"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/sontags/env"
)
const (
form = `<!DOCTYPE html>
<html lang="en">
<head>
<title>File Upload</title>
</head>
<body>
<div class="container">
<h1>File Upload</h1>
<div class="message">{{.}}</div>
<form class="form-signin" method="post" action="/" enctype="multipart/form-data">
<fieldset>
<input type="file" name="myfiles" id="myfiles" multiple="multiple">
<input type="submit" name="submit" value="Submit">
</fieldset>
</form>
</div>
</body>
</html>`
)
//Display the named template
func displayHandler(templates *template.Template, message interface{}) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
var out bytes.Buffer
templates.ExecuteTemplate(&out, "Form", message)
c.Data(http.StatusOK, "text/html", out.Bytes())
})
}
func uploadHandler(templates *template.Template, dest string) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
reader, err := c.Request.MultipartReader()
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
return
}
//copy each part to destination.
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
//if part.FileName() is empty, skip this iteration.
if part.FileName() == "" {
continue
}
dst, err := os.Create(dest + part.FileName())
defer dst.Close()
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(dst, part); err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
return
}
}
var out bytes.Buffer
templates.ExecuteTemplate(&out, "Form", "Uploaded successfully...")
c.Data(http.StatusOK, "text/html", out.Bytes())
})
}
func main() {
var port, dest string
env.Var(&port, "PORT", "8989", "Port that should be binded")
env.Var(&dest, "DEST", "/tmp/", "Where uploaded files will be placed")
env.Parse("U")
templates := template.Must(template.New("Form").Parse(form))
log.Println("listening on port", port)
log.Println("writing data to", dest)
r := gin.Default()
r.GET("/", displayHandler(templates, nil))
r.POST("/", uploadHandler(templates, dest))
r.Run(":" + port)
}