-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (91 loc) · 3.27 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
package main
import (
"flag"
"io"
"log"
"net/http"
"os"
"path"
)
var MAIN_PAGE = []byte(`
<html>
<head>
<title>Uploader</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/dropzone.css" integrity="sha256-0Z6mOrdLEtgqvj7tidYQnCYWG3G2GAIpatAWKhDx+VM=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/dropzone.js" integrity="sha256-vnXjg9TpLhXuqU0OcVO7x+DpR/H1pCeVLLSeQ/I/SUs=" crossorigin="anonymous"></script>
</head>
<body>
<div style="position: absolute; top: 25%; left: 25%; width: 50%; height: 50%; text-align: center; margin: 0 auto;">
<form action="/" method="post" class="dropzone" id="my-awesome-dropzone" enctype="multipart/form-data">
<!-- Nothing to see here. -->
</form>
</div>
</body>
</html>
`)
func saveUploadedFiles(w http.ResponseWriter, r *http.Request) {
log.Printf("Started uploading files from %s", r.RemoteAddr)
// Allow a maximum upload length of 64MB.
if err := r.ParseMultipartForm(1 << 16); err != nil {
log.Printf("Blocked attempted upload a file larger than 64MB from %s.", r.RemoteAddr)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Loop over the metadata for each uploaded file.
for _, upload := range r.MultipartForm.File["file"] {
log.Printf("Processing upload of '%s' from %s", upload.Filename, r.RemoteAddr)
source, err := upload.Open()
if err != nil {
log.Printf("Upload of '%s' from %s failed with error %+v", upload.Filename, r.RemoteAddr, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer source.Close()
destination, err := os.Create(path.Join(storage, upload.Filename))
if err != nil {
log.Printf("Saving of '%s' from %s failed with error %+v", upload.Filename, r.RemoteAddr, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer destination.Close()
if _, err := io.Copy(destination, source); err != nil {
log.Printf("Writing of '%s' from %s failed with error %+v", upload.Filename, r.RemoteAddr, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Upload of '%s' from %s completed successfully!", upload.Filename, r.RemoteAddr)
}
log.Printf("Finished uploading files from %s", r.RemoteAddr)
}
func showUploadPage(w http.ResponseWriter, r *http.Request) {
w.Write(MAIN_PAGE)
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
showUploadPage(w, r)
case "POST":
saveUploadedFiles(w, r)
}
}
var storage = "./data"
func main() {
// Allow overriding the data storage path on the command line.
flag.StringVar(&storage, "data", storage, "Data storage directory.")
flag.Parse()
// Attempt to create a the storage directory, or fail.
if _, err := os.Stat(storage); os.IsNotExist(err) {
if err := os.Mkdir(storage, os.ModePerm); err != nil {
log.Println("Storage directory does not exist and cannot be created!", err)
return
}
}
// Don't try to serve a favicon.
http.HandleFunc("/favicon.ico", http.NotFound)
// Handle uploads as normal.
http.HandleFunc("/", uploadHandler)
// Launch an HTTP server on :8080.
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("http.ListenAndServe: ", err)
}
}