-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
136 lines (109 loc) · 2.7 KB
/
service.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
package upload
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"sort"
"time"
)
type UploadService struct {
}
func (u UploadService) newUploadId() string {
return fmt.Sprintf("%d", time.Now().UnixMicro())
}
func (u UploadService) CreateUpload() CreateUploadResponse {
return CreateUploadResponse{
UploadId: u.newUploadId(),
}
}
func (u UploadService) chunkFilepath(sum string) string {
return filepath.Join("./uploads/chunks", sum)
}
func (u UploadService) finalFilepath(uploadId string) string {
return filepath.Join("./uploads/", uploadId)
}
// UploadChunk upload each 4MB chunk of a file
func (u UploadService) UploadChunk(req *http.Request) (*UploadChunkResponse, error) {
d, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
sum := md5.Sum(d)
sumHex := hex.EncodeToString(sum[:])
chunk, err := os.Create(u.chunkFilepath(sumHex))
defer chunk.Close()
if err != nil {
return nil, err
}
_, err = chunk.Write(d)
if err != nil {
return nil, err
}
// TODO save chunk record to database, generate ID for each chunk
// chk := Chunk{
// ID: "",
// Hash: sumHex,
// }
return &UploadChunkResponse{
ID: "",
Hash: sumHex,
}, nil
}
// ReassembleChunk put chunks together
func (u UploadService) ReassembleChunk(req ReassembleChunksRequest) (*ReassembleChunksResponse, error) {
sort.Slice(req.Chunks, func(i, j int) bool {
return req.Chunks[i].ChunkNumber < req.Chunks[j].ChunkNumber
})
finalFile, err := os.Create(u.finalFilepath(req.UploadId + req.Filename))
if err != nil {
return nil, err
}
defer finalFile.Close()
hash := md5.New()
for _, chunk := range req.Chunks {
chunkFilepath := u.chunkFilepath(chunk.Hash)
chunkData, e := os.ReadFile(chunkFilepath)
if e != nil {
return nil, e
}
hash.Write(chunkData)
_, err = finalFile.Write(chunkData)
if err != nil {
return nil, err
}
// Remove chunk data
os.RemoveAll(chunkFilepath)
}
sum := hash.Sum(nil)
resp := &ReassembleChunksResponse{
Hash: hex.EncodeToString(sum[:]),
Filename: req.Filename,
}
// TODO persist file record into database
return resp, nil
}
func (u UploadService) Download(req DownloadRequest, header http.Header, writer io.Writer) error {
// Retrieve file by uploadId
fileRecord := File{
UploadId: req.UploadId,
Filename: "",
}
stat, err := os.Stat(u.finalFilepath(req.UploadId))
if err != nil {
return err
}
filename := fileRecord.Filename
header.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
header.Set("Content-Length", fmt.Sprintf("%d", stat.Size()))
f, err := os.Open(u.finalFilepath(req.UploadId))
if err != nil {
return err
}
io.Copy(writer, f)
defer f.Close()
return nil
}