This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.go
302 lines (280 loc) · 6.19 KB
/
manager.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package warplib
import (
"encoding/gob"
"errors"
"net/http"
"os"
"sync"
)
var __USERDATA_FILE_NAME = ConfigDir + "/userdata.warp"
type Manager struct {
items ItemsMap
f *os.File
mu *sync.RWMutex
wg *sync.WaitGroup
// flush-mutex
fmu *sync.RWMutex
}
func InitManager() (m *Manager, err error) {
m = &Manager{
items: make(ItemsMap),
mu: new(sync.RWMutex),
wg: new(sync.WaitGroup),
fmu: new(sync.RWMutex),
}
m.f, err = os.OpenFile(
__USERDATA_FILE_NAME,
os.O_RDWR|os.O_CREATE,
os.ModePerm,
)
if err != nil {
m = nil
return
}
_ = gob.NewDecoder(m.f).Decode(&m.items)
m.populateMemPart()
return
}
type AddDownloadOpts struct {
IsHidden bool
IsChildren bool
Child *Downloader
AbsoluteLocation string
}
func (m *Manager) populateMemPart() {
for _, item := range m.items {
if item.memPart == nil {
item.memPart = make(map[string]int64)
}
for ioff, part := range item.Parts {
item.memPart[part.Hash] = ioff
}
}
}
func (m *Manager) AddDownload(d *Downloader, opts *AddDownloadOpts) (err error) {
m.fmu.RLock()
defer m.fmu.RUnlock()
if opts == nil {
opts = &AddDownloadOpts{}
}
cHash := ""
if opts.Child != nil {
cHash = opts.Child.hash
}
item, err := newItem(
m.mu,
d.fileName,
d.url,
d.dlLoc,
d.hash,
d.contentLength,
&itemOpts{
AbsoluteLocation: opts.AbsoluteLocation,
Child: opts.IsChildren,
Hide: opts.IsHidden,
ChildHash: cHash,
Headers: d.headers,
},
)
if err != nil {
return err
}
// item.dAlloc = d
m.UpdateItem(item)
m.wg.Add(1)
m.patchHandlers(d, item)
return
}
func (m *Manager) patchHandlers(d *Downloader, item *Item) {
oSPH := d.handlers.SpawnPartHandler
d.handlers.SpawnPartHandler = func(hash string, ioff, foff int64) {
item.addPart(hash, ioff, foff)
m.UpdateItem(item)
oSPH(hash, ioff, foff)
}
oRPH := d.handlers.RespawnPartHandler
d.handlers.RespawnPartHandler = func(hash string, partIoff, ioffNew, foffNew int64) {
item.addPart(hash, partIoff, foffNew)
m.UpdateItem(item)
oRPH(hash, partIoff, ioffNew, foffNew)
}
oPH := d.handlers.DownloadProgressHandler
d.handlers.DownloadProgressHandler = func(hash string, nread int) {
item.Downloaded += ContentLength(nread)
m.UpdateItem(item)
oPH(hash, nread)
}
oCCH := d.handlers.CompileCompleteHandler
d.handlers.CompileCompleteHandler = func(hash string, tread int64) {
off, part := item.getPart(hash)
if part == nil {
d.handlers.ErrorHandler(hash, errors.New("manager part item is nil"))
return
}
part.Compiled = true
item.savePart(off, part)
oCCH(hash, tread)
}
oDCH := d.handlers.DownloadCompleteHandler
d.handlers.DownloadCompleteHandler = func(hash string, tread int64) {
if hash != MAIN_HASH {
return
}
defer m.wg.Done()
item.Parts = nil
item.Downloaded = item.TotalSize
m.UpdateItem(item)
oDCH(hash, tread)
}
}
func (m *Manager) encode(e any) (err error) {
m.mu.Lock()
m.f.Seek(0, 0)
defer m.mu.Unlock()
return gob.NewEncoder(m.f).Encode(e)
}
func (m *Manager) mapItem(item *Item) {
m.mu.Lock()
defer m.mu.Unlock()
m.items[item.Hash] = item
}
func (m *Manager) deleteItem(hash string) {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.items, hash)
}
func (m *Manager) UpdateItem(item *Item) {
m.mapItem(item)
m.encode(m.items)
}
func (m *Manager) GetItems() []*Item {
m.mu.RLock()
defer m.mu.RUnlock()
items := make([]*Item, len(m.items))
var i int
for _, item := range m.items {
items[i] = item
i++
}
return items
}
func (m *Manager) GetPublicItems() []*Item {
var items = []*Item{}
for _, item := range m.GetItems() {
if item.Children {
continue
}
items = append(items, item)
}
return items
}
func (m *Manager) GetIncompleteItems() []*Item {
var items = []*Item{}
for _, item := range m.GetItems() {
if item.TotalSize == item.Downloaded {
continue
}
items = append(items, item)
}
return items
}
func (m *Manager) GetCompletedItems() []*Item {
var items = []*Item{}
for _, item := range m.GetItems() {
if item.TotalSize != item.Downloaded {
continue
}
items = append(items, item)
}
return items
}
func (m *Manager) GetItem(hash string) (item *Item) {
m.mu.RLock()
defer m.mu.RUnlock()
item = m.items[hash]
if item == nil {
return
}
item.memPart = make(map[string]int64)
item.mu = m.mu
return
}
type ResumeDownloadOpts struct {
ForceParts bool
// MaxConnections sets the maximum number of parallel
// network connections to be used for the downloading the file.
MaxConnections int
// MaxSegments sets the maximum number of file segments
// to be created for the downloading the file.
MaxSegments int
Headers Headers
Handlers *Handlers
}
func (m *Manager) ResumeDownload(client *http.Client, hash string, opts *ResumeDownloadOpts) (item *Item, err error) {
m.fmu.RLock()
defer m.fmu.RUnlock()
if opts == nil {
opts = &ResumeDownloadOpts{}
}
item = m.GetItem(hash)
if item == nil {
err = ErrDownloadNotFound
return
}
if item.Headers == nil {
item.Headers = make(Headers, 0)
}
if opts.Headers != nil {
for i, ih := range item.Headers {
for _, oh := range opts.Headers {
if ih != oh {
continue
}
item.Headers[i] = oh
}
}
}
d, er := initDownloader(client, hash, item.Url, item.TotalSize, &DownloaderOpts{
ForceParts: opts.ForceParts,
MaxConnections: opts.MaxConnections,
MaxSegments: opts.MaxSegments,
Handlers: opts.Handlers,
FileName: item.Name,
DownloadDirectory: item.DownloadLocation,
Headers: item.Headers,
})
if er != nil {
err = er
return
}
m.wg.Add(1)
m.patchHandlers(d, item)
item.dAlloc = d
// m.UpdateItem(item)
return
}
func (m *Manager) Flush() error {
m.wg.Wait()
m.fmu.Lock()
defer m.fmu.Unlock()
m.items = make(ItemsMap)
m.encode(m.items)
return os.RemoveAll(DlDataDir)
}
// TODO: make FlushOne safe for flushing while the item is being downloaded
func (m *Manager) FlushOne(hash string) error {
m.fmu.RLock()
defer m.fmu.RUnlock()
m.mu.RLock()
_, found := m.items[hash]
m.mu.RUnlock()
if !found {
return ErrFlushHashNotFound
}
m.deleteItem(hash)
m.encode(m.items)
return os.RemoveAll(GetPath(DlDataDir, hash))
}
func (m *Manager) Close() error {
return m.f.Close()
}