-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
159 lines (129 loc) · 3.62 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
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
//go:generate rice embed-go
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/GeertJohan/go.rice"
"github.com/fatih/color"
"github.com/gosuri/uiprogress"
)
var (
listen = flag.String("listen", ":8080", "The address to run the web service on.")
path = flag.String("path", ".", "The path files are saved to. The file name is provided by the client.")
disableColor = flag.Bool("no-color", false, "Disable color output.")
disableProgress = flag.Bool("no-progress", false, "Disable progress bars.")
)
var bars *uiprogress.Progress
func main() {
flag.Parse()
if *disableColor {
color.NoColor = true
}
// Setup progress bars
if !*disableProgress {
bars = uiprogress.New()
bars.Start()
}
// Setup Server
mux := http.NewServeMux()
// Serve static files with rice for portability
staticFiles := rice.MustFindBox("frontend").HTTPBox()
mux.Handle("/", http.FileServer(staticFiles))
// Handle form posts
mux.HandleFunc("/send", func(rw http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
fileName := r.Header.Get("X-File-Name")
if fileName == "" {
log.Printf(color.RedString("File name not provided"))
rw.WriteHeader(http.StatusBadRequest)
return
}
outFile, err := os.Create(fileName)
defer outFile.Close()
if err != nil {
log.Printf(color.RedString("Failed to create file: %v"), err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
bar := bars.AddBar(int(r.ContentLength))
progWriter := &ProgressWriter{
Length: r.ContentLength,
FileName: fileName,
BytesWritten: 0,
Bar: bar,
Writer: outFile,
}
bar.AppendFunc(progWriter.Append())
bar.PrependFunc(progWriter.Prepend())
written, err := io.Copy(progWriter, r.Body)
if err != nil {
log.Printf(color.RedString("Failed to copy file: %v"), err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
if *disableProgress {
log.Printf("Wrote "+color.CyanString("%d")+" bytes to file "+color.CyanString("%s"), written, fileName)
}
})
s := &http.Server{
Addr: *listen,
Handler: mux,
}
err := os.Chdir(*path)
if err != nil {
log.Printf(color.RedString("Failed to change to %s, %v"), *path, err)
return
}
log.Printf("Starting server on %s", color.CyanString(*listen))
log.Printf("Saving files to %s", color.CyanString(*path))
log.Fatal(s.ListenAndServe())
}
type ProgressWriter struct {
Length int64
FileName string
BytesWritten int64
Bar *uiprogress.Bar
io.Writer
}
func (writer *ProgressWriter) Write(bytes []byte) (int, error) {
bars.RefreshInterval = time.Millisecond * 300
n, err := writer.Writer.Write(bytes)
writer.BytesWritten += int64(n)
writer.Bar.Set(int(writer.BytesWritten))
if err == io.EOF {
// Slow down, end of a progress bar.
bars.RefreshInterval = time.Second * 10
}
return n, err
}
func (writer *ProgressWriter) Prepend() func(*uiprogress.Bar) string {
return func(bar *uiprogress.Bar) string {
return writer.FileName
}
}
func (writer *ProgressWriter) Append() func(*uiprogress.Bar) string {
total := byteUnitStr(writer.Length)
return func(bar *uiprogress.Bar) string {
completed := byteUnitStr(writer.BytesWritten)
return bar.CompletedPercentString() + " " + completed + "/" + total
}
}
var byteUnits = []string{"B", "KB", "MB", "GB", "TB", "PB"}
// https://github.com/mitchellh/ioprogress/blob/master/draw.go#L91
func byteUnitStr(n int64) string {
var unit string
size := float64(n)
for i := 1; i < len(byteUnits); i++ {
if size < 1000 {
unit = byteUnits[i-1]
break
}
size = size / 1000
}
return fmt.Sprintf("%.3g %s", size, unit)
}