-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (130 loc) · 3.43 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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"code.cloudfoundry.org/bytefmt"
)
var (
root string
appVersion = "unknown"
startTime int64
)
type progressReader struct {
reader io.Reader
total int64
count int64
}
func (pr *progressReader) Read(p []byte) (int, error) {
n, err := pr.reader.Read(p)
pr.count += int64(n)
duration := time.Now().Unix() - startTime
if duration != 0 {
speed := bytefmt.ByteSize(uint64(pr.count / duration))
output := fmt.Sprintf("Uploaded %d %% %s of %s, speed:%s duration:%s \r",
(pr.count * 100 / pr.total), bytefmt.ByteSize(uint64(pr.count)), bytefmt.ByteSize(uint64(pr.total)), speed, time.Duration(duration*1000*1000*1000))
fmt.Println(output)
fmt.Fprint(*writer, output)
}
return n, err
}
var writer *http.ResponseWriter
func upload(w http.ResponseWriter, r *http.Request) {
//filename := strings.Replace(r.URL.Path, "/upload/", "", 1)
filename := r.URL.Path
log.Println("new upload file name", filename)
file, err := os.Create(root + filename)
if err != nil {
log.Println("create error", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
reader := &progressReader{
reader: r.Body,
total: r.ContentLength,
}
writer = &w
startTime = time.Now().Unix()
_, err = io.Copy(file, reader)
if err != nil {
log.Println("copy error", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("")
fmt.Println("upload over")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "File uploaded successfully!")
}
func delete(w http.ResponseWriter, r *http.Request) {
//filename := strings.Replace(r.URL.Path, "/delete/", "", 1)
filename := r.URL.Path
err := os.Remove(root + filename)
if err != nil {
log.Println("delete error: ", filename, err.Error())
fmt.Fprintf(w, "delete %s error: %s", filename, err.Error())
return
}
//todo delete file operation
fmt.Fprintf(w, " %s delted success", filename)
}
func get(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Path
if filename == "/" {
files, err := ioutil.ReadDir(root)
if err != nil {
w.Write([]byte(err.Error()))
return
}
for _, file := range files {
w.Write([]byte(file.Name()))
}
} else {
content, err := ioutil.ReadFile(root + filename)
if err != nil {
w.Write([]byte("file not exist " + err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(content)
}
}
func dispatcher(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" {
delete(w, r)
} else if r.Method == "PUT" {
upload(w, r)
} else if r.Method == "GET" {
get(w, r)
}
}
func main() {
showVersion := false
var address string
flag.BoolVar(&showVersion, "v", false, "show verison")
flag.StringVar(&address, "a", "10.10.10.3:80", "bind address and port")
flag.StringVar(&root, "p", "./", " serving directory")
flag.Parse()
if showVersion {
fmt.Println("version:", appVersion)
return
}
//check path
if _, err := os.Stat(root); err != nil {
log.Fatalf(" check direct stat error:%s", err.Error())
}
log.Printf(" server info{port:%s directory:%s}", address, root)
// http.HandleFunc("/upload/", upload)
// http.HandleFunc("/delete/", delete)
http.HandleFunc("/", dispatcher)
//http.Handle("/", http.FileServer(http.Dir(root)))
log.Println("server is running")
log.Fatal(http.ListenAndServe(address, nil))
}