forked from Dri0m/flashpoint-submission-system
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resumableuploadservice.go
252 lines (201 loc) · 5.98 KB
/
resumableuploadservice.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package resumableuploadservice
import (
"fmt"
"io"
"io/ioutil"
"os"
"github.com/FlashpointProject/flashpoint-submission-system/utils"
)
type ResumableUploadService struct {
path string
}
func (rsu *ResumableUploadService) getChunkFilename(uid int64, fileID string, chunkNumber int) string {
if len(fileID) > 64 {
fileID = fileID[:64]
}
return fmt.Sprintf("%s/data-%d-%s-%d", rsu.path, uid, fileID, chunkNumber)
}
func New(path string) (*ResumableUploadService, error) {
err := os.MkdirAll(path, os.ModeDir)
if err != nil {
return nil, err
}
rsu := &ResumableUploadService{
path: path,
}
return rsu, nil
}
// Close stops what needs to be stopped
func (rsu *ResumableUploadService) Close() {
}
// PutChunk stores chunk, overwrites if exists.
func (rsu *ResumableUploadService) PutChunk(uid int64, fileID string, chunkNumber int, chunk []byte) error {
if uid == 0 || len(fileID) == 0 || chunkNumber == 0 || len(chunk) == 0 {
panic("invalid arguments provided")
}
chunkFilename := rsu.getChunkFilename(uid, fileID, chunkNumber)
chunkFilenameTmp := chunkFilename + ".part"
if err := os.WriteFile(chunkFilenameTmp, chunk, 0644); err != nil {
return err
}
if err := os.Rename(chunkFilenameTmp, chunkFilename); err != nil {
return err
}
return nil
}
// TestChunk returns true if the chunk is already received
func (rsu *ResumableUploadService) TestChunk(uid int64, fileID string, chunkNumber int, chunkSize int64) (bool, error) {
if uid == 0 || len(fileID) == 0 || chunkNumber == 0 || chunkSize == 0 {
panic("invalid arguments provided")
}
chunkFilename := rsu.getChunkFilename(uid, fileID, chunkNumber)
if !utils.FileExists(chunkFilename) {
return false, nil
}
fi, err := os.Stat(chunkFilename)
if err != nil {
return false, err
}
if fi.Size() != chunkSize {
return false, nil
}
return true, nil
}
// IsUploadFinished compares the total size of stored chunks a to provided size
func (rsu *ResumableUploadService) IsUploadFinished(uid int64, fileID string, chunkCount int, expectedSize int64) (bool, error) {
if uid == 0 || len(fileID) == 0 {
panic("invalid arguments provided")
}
var totalSize int64
for i := 1; i <= chunkCount; i++ {
chunkFilename := rsu.getChunkFilename(uid, fileID, i)
if !utils.FileExists(chunkFilename) {
return false, nil
}
fi, err := os.Stat(chunkFilename)
if err != nil {
return false, err
}
totalSize += fi.Size()
}
return totalSize == expectedSize, nil
}
// DeleteFile deletes the whole file bucket
func (rsu *ResumableUploadService) DeleteFile(uid int64, fileID string, chunkCount int) error {
if uid == 0 || len(fileID) == 0 {
panic("invalid arguments provided")
}
for i := 1; i <= chunkCount; i++ {
chunkFilename := rsu.getChunkFilename(uid, fileID, i)
err := os.Remove(chunkFilename)
if err != nil {
return err
}
}
return nil
}
type ReadCloserInformer interface {
Read(buf []byte) (n int, err error)
Close() error
GetFractionRead() float32
}
type ReadCloserInformerProvider interface {
GetReadCloserInformer() (ReadCloserInformer, error)
}
type fileReadCloserInformer struct {
uid int64
fileID string
rsu *ResumableUploadService
currentChunkNumber int
currentChunkData []byte
currentChunkOffset int
chunkCount int
}
// NewFileReader returns a reader that reconstructs the file from the chunks on the fly. It does not check if the file is complete.
func (rsu *ResumableUploadService) NewFileReader(uid int64, fileID string, chunkCount int) (ReadCloserInformer, error) {
if uid == 0 || len(fileID) == 0 {
panic("invalid arguments provided")
}
return &fileReadCloserInformer{
uid: uid,
fileID: fileID,
rsu: rsu,
currentChunkNumber: 0,
currentChunkData: nil,
currentChunkOffset: 0,
chunkCount: chunkCount,
}, nil
}
func (fr *fileReadCloserInformer) Read(buf []byte) (n int, err error) {
// case 0: init the reader
if fr.currentChunkNumber == 0 {
fr.currentChunkNumber++
fr.currentChunkData, err = fr.rsu.getChunk(fr.uid, fr.fileID, fr.currentChunkNumber)
if err != nil {
return
}
}
for {
// buffer full
if len(buf[n:]) == 0 {
return
}
remainingChunkBytes := len(fr.currentChunkData) - fr.currentChunkOffset
// case 1: reader has more data available than the caller wants
if remainingChunkBytes >= len(buf[n:]) {
low := fr.currentChunkOffset
high := fr.currentChunkOffset + len(buf[n:])
copy(buf[n:], fr.currentChunkData[low:high])
fr.currentChunkOffset = high
n += high - low
return
}
// case 2: caller wants more data than the reader currently has
if remainingChunkBytes < len(buf[n:]) {
// use up the rest of the chunk
if remainingChunkBytes > 0 {
low := fr.currentChunkOffset
high := len(fr.currentChunkData)
copy(buf[n:], fr.currentChunkData[low:high])
fr.currentChunkOffset = high
n += high - low
}
// fetch new chunk
fr.currentChunkNumber++
if fr.currentChunkNumber > fr.chunkCount {
return n, io.EOF
}
fr.currentChunkOffset = 0
fr.currentChunkData, err = fr.rsu.getChunk(fr.uid, fr.fileID, fr.currentChunkNumber)
if err != nil {
return
}
// no more chunks, terminate
if fr.currentChunkData == nil {
return n, io.EOF
}
}
}
}
// Close is just a dummy close in case it's needed later
func (fr *fileReadCloserInformer) Close() error {
return nil
}
func (fr *fileReadCloserInformer) GetFractionRead() float32 {
return float32(fr.currentChunkNumber) / float32(fr.chunkCount)
}
// getChunk returns chunk
func (rsu *ResumableUploadService) getChunk(uid int64, fileID string, chunkNumber int) ([]byte, error) {
if uid == 0 || len(fileID) == 0 || chunkNumber == 0 {
panic("invalid arguments provided")
}
chunkFilename := rsu.getChunkFilename(uid, fileID, chunkNumber)
if !utils.FileExists(chunkFilename) {
return nil, fmt.Errorf("file %s does not exist", chunkFilename)
}
chunk, err := ioutil.ReadFile(chunkFilename)
if err != nil {
return nil, err
}
return chunk, nil
}