forked from Dri0m/flashpoint-submission-system
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsiteservice_resumablereceiver.go
179 lines (141 loc) · 6.26 KB
/
siteservice_resumablereceiver.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
package service
import (
"context"
"fmt"
"net/http"
"time"
"github.com/FlashpointProject/flashpoint-submission-system/resumableuploadservice"
"github.com/FlashpointProject/flashpoint-submission-system/types"
"github.com/FlashpointProject/flashpoint-submission-system/utils"
"github.com/kofalt/go-memoize"
)
var resumableMemoizer = memoize.NewMemoizer(time.Hour*24, time.Hour*24)
func (ru resumableUpload) GetReadCloserInformer() (resumableuploadservice.ReadCloserInformer, error) {
return ru.rsu.NewFileReader(ru.uid, ru.fileID, ru.chunkCount)
}
func (s *SiteService) ReceiveSubmissionChunk(ctx context.Context, sid *int64, resumableParams *types.ResumableParams, chunk []byte) (*string, error) {
ctx = context.WithValue(ctx, utils.CtxKeys.Log, resumableLog(ctx, resumableParams))
uid := utils.UserID(ctx)
if uid == 0 {
utils.LogCtx(ctx).Panic("no user associated with request")
}
utils.LogCtx(ctx).Debug("storing submission chunk")
err := s.resumableUploadService.PutChunk(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableChunkNumber, chunk)
if err != nil {
utils.LogCtx(ctx).Error(err)
return nil, err
}
isComplete, err := s.resumableUploadService.IsUploadFinished(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableTotalChunks, resumableParams.ResumableTotalSize)
if err != nil {
utils.LogCtx(ctx).Error(err)
return nil, err
}
if isComplete {
// tempName is used as the ID of the submission while the submission is being processed, used by the client to poll for status
tempName := s.randomStringProvider.RandomString(32)
s.SSK.SetReceived(tempName)
go func() {
ctx := utils.ValueOnlyContext{Context: ctx}
utils.LogCtx(ctx).Debug("submission resumable upload finished")
processReceivedResumableSubmission := func() (interface{}, error) {
return nil, s.processReceivedResumableSubmission(ctx, uid, sid, resumableParams, tempName)
}
processReceivedResumableSubmissionKey := fmt.Sprintf("%d-%s", uid, resumableParams.ResumableIdentifier)
_, err, cached := resumableMemoizer.Memoize(processReceivedResumableSubmissionKey, processReceivedResumableSubmission)
utils.LogCtx(ctx).WithField("cached", utils.BoolToString(cached)).Debug("processed resumable submission upload")
if err != nil {
resumableMemoizer.Storage.Delete(processReceivedResumableSubmissionKey)
utils.LogCtx(ctx).Error(err)
}
if !cached {
utils.LogCtx(ctx).Debug("deleting the resumable file chunks")
if err := s.resumableUploadService.DeleteFile(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableTotalChunks); err != nil {
utils.LogCtx(ctx).Error(err)
}
utils.LogCtx(ctx).WithField("memoizerKey", processReceivedResumableSubmissionKey).Debug("deleting the memoized call")
resumableMemoizer.Storage.Delete(processReceivedResumableSubmissionKey)
}
}()
return &tempName, nil
}
return nil, nil
}
type resumableUpload struct {
uid int64
fileID string
chunkCount int
rsu *resumableuploadservice.ResumableUploadService
}
func newResumableUpload(uid int64, fileID string, chunkCount int, rsu *resumableuploadservice.ResumableUploadService) *resumableUpload {
return &resumableUpload{
uid: uid,
fileID: fileID,
chunkCount: chunkCount,
rsu: rsu,
}
}
func (s *SiteService) ReceiveFlashfreezeChunk(ctx context.Context, resumableParams *types.ResumableParams, chunk []byte) (*int64, error) {
ctx = context.WithValue(ctx, utils.CtxKeys.Log, resumableLog(ctx, resumableParams))
uid := utils.UserID(ctx)
if uid == 0 {
utils.LogCtx(ctx).Panic("no user associated with request")
}
utils.LogCtx(ctx).Debug("storing flashfreeze chunk")
err := s.resumableUploadService.PutChunk(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableChunkNumber, chunk)
if err != nil {
utils.LogCtx(ctx).Error(err)
return nil, err
}
isComplete, err := s.resumableUploadService.IsUploadFinished(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableTotalChunks, resumableParams.ResumableTotalSize)
if err != nil {
utils.LogCtx(ctx).Error(err)
return nil, err
}
if isComplete {
utils.LogCtx(ctx).Debug("flashfreeze resumable upload finished")
processReceivedResumableFlashfreeze := func() (interface{}, error) {
return s.processReceivedResumableFlashfreeze(ctx, uid, resumableParams)
}
processReceivedResumableFlashfreezeKey := fmt.Sprintf("%d-%s", uid, resumableParams.ResumableIdentifier)
ifid, err, cached := resumableMemoizer.Memoize(processReceivedResumableFlashfreezeKey, processReceivedResumableFlashfreeze)
utils.LogCtx(ctx).WithField("cached", utils.BoolToString(cached)).Debug("processed resumable flashfreeze upload")
if err != nil {
resumableMemoizer.Storage.Delete(processReceivedResumableFlashfreezeKey)
utils.LogCtx(ctx).Error(err)
return nil, err
}
if !cached {
utils.LogCtx(ctx).Debug("deleting the resumable file chunks")
if err := s.resumableUploadService.DeleteFile(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableTotalChunks); err != nil {
utils.LogCtx(ctx).Error(err)
}
utils.LogCtx(ctx).WithField("memoizerKey", processReceivedResumableFlashfreezeKey).Debug("deleting the memoized call")
resumableMemoizer.Storage.Delete(processReceivedResumableFlashfreezeKey)
}
return ifid.(*int64), nil
}
return nil, nil
}
///////////
func (s *SiteService) IsChunkReceived(ctx context.Context, resumableParams *types.ResumableParams) (bool, error) {
ctx = context.WithValue(ctx, utils.CtxKeys.Log, resumableLog(ctx, resumableParams))
uid := utils.UserID(ctx)
if uid == 0 {
utils.LogCtx(ctx).Panic("no user associated with request")
}
isComplete, err := s.resumableUploadService.IsUploadFinished(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableTotalChunks, resumableParams.ResumableTotalSize)
if err != nil {
utils.LogCtx(ctx).Error(err)
return false, err
}
if isComplete {
return false, perr("file already fully received", http.StatusConflict)
}
utils.LogCtx(ctx).Debug("testing chunk")
isReceived, err := s.resumableUploadService.TestChunk(uid, resumableParams.ResumableIdentifier, resumableParams.ResumableChunkNumber, resumableParams.ResumableCurrentChunkSize)
if err != nil {
utils.LogCtx(ctx).Error(err)
return false, err
}
return isReceived, nil
}