-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (75 loc) · 1.8 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
package main
import (
"log"
"net/http"
"os"
)
//Need to be set to 8*1024*1024 = 8mb
const MAX_CHUNK_SIZE = 8 * 1024 * 1024
func upload(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("file")
if err != nil {
log.Println(err)
return
}
defer file.Close()
var buffersize int64
var parts int
log.Println("FileSize:", handler.Size)
if filesize := handler.Size; filesize < MAX_CHUNK_SIZE {
buffersize = filesize
parts = 1
} else {
buffersize = MAX_CHUNK_SIZE
parts = Roundup(buffersize, filesize)
}
/*
key: int; part number, e.g. 1 means the 1st part
value: []byte; a fix-sized chunk of data read from file bytestream
*/
fileChunks := make(map[int][]byte)
log.Println("Chunks:", parts)
for i := 1; i <= parts; i++ {
buf := make([]byte, buffersize)
b, err := file.Read(buf)
if err != nil {
log.Println(err)
return
}
// TODO: send fileChunks[i] through codec.msg
fileChunks[i] = buf[:b]
//log.Println("Length: ", len(fileChunks[i]))
//log.Println("Index: ", i)
//log.Println("Value: ", fileChunks[i])
}
//log.Println(fileChunks)
makeFile(fileChunks, handler.Size, handler.Filename, parts)
}
//private testing function
func makeFile(fileChunks map[int][]byte, filesize int64, fileName string, parts int) {
dirpath := "./target/"
os.Mkdir(dirpath, 0777)
filepath := dirpath + fileName
newfile, _ := os.OpenFile(filepath, os.O_CREATE|os.O_WRONLY, 0666)
var buffer []byte
for i := 1; i <= parts; i++ {
for k, v := range fileChunks {
if k == i {
buffer = append(buffer, v...)
}
}
}
//log.Println(buffer)
newfile.Write(buffer)
newfile.Close()
}
func Roundup(a int64, b int64) int {
if b%a > 0 {
return int(b/a) + 1
}
return int(b / a)
}
func main() {
http.HandleFunc("/upload", upload)
log.Fatal(http.ListenAndServe(":8080", nil))
}