/
fileUploaderv3.go
138 lines (117 loc) · 3.24 KB
/
fileUploaderv3.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
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", homePage)
http.HandleFunc("/upload", uploadFile)
http.HandleFunc("/upload-multiple", uploadMultipleFiles)
fmt.Println("Server started at http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
func homePage(w http.ResponseWriter, r *http.Request) {
html := `<html>
<head>
<title>File Upload</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
<input type="file" name="myFile" />
<input type="submit" value="Upload Single File" />
</form>
<form enctype="multipart/form-data" action="/upload-multiple" method="post">
<input type="file" name="myFiles" multiple />
<input type="submit" value="Upload Multiple Files" />
</form>
</body>
</html>`
fmt.Fprint(w, html)
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
file, handler, err := r.FormFile("myFile")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
return
}
defer file.Close()
dst, err := os.Create(handler.Filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
counter := &WriteCounter{}
if _, err = io.Copy(dst, io.TeeReader(file, counter)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Successfully Uploaded File\n")
}
func uploadMultipleFiles(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// The request's ParseMultipartForm method must be called before accessing file data
if r.MultipartForm == nil {
if err := r.ParseMultipartForm(32 << 20); err != nil { // Adjust the size as needed
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
files := r.MultipartForm.File["myFiles"]
for i, fileHeader := range files {
fmt.Printf("Uploading file %d/%d: %s\n", i+1, len(files), fileHeader.Filename)
file, err := fileHeader.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
dst, err := os.Create(fileHeader.Filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
counter := &WriteCounter{}
if _, err = io.Copy(dst, io.TeeReader(file, counter)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Printf("Finished uploading file %d/%d: %s\n", i+1, len(files), fileHeader.Filename)
}
fmt.Fprintf(w, "Successfully Uploaded Multiple Files\n")
}
type WriteCounter struct {
Total uint64
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.PrintProgress()
return n, nil
}
func (wc *WriteCounter) PrintProgress() {
fmt.Printf("\r%s", bytesToHuman(wc.Total))
}
func bytesToHuman(b uint64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := unit, 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
}