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
/
item.go
113 lines (100 loc) · 2.32 KB
/
item.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
package warplib
import (
"path/filepath"
"sync"
"time"
)
type Item struct {
Hash string
Name string
Url string
Headers Headers
DateAdded time.Time
TotalSize ContentLength
Downloaded ContentLength
DownloadLocation string
AbsoluteLocation string
ChildHash string
Hidden bool
Children bool
Parts map[int64]*ItemPart
mu *sync.RWMutex
dAlloc *Downloader
memPart map[string]int64
}
type ItemPart struct {
Hash string
FinalOffset int64
Compiled bool
}
type ItemsMap map[string]*Item
type itemOpts struct {
Hide, Child bool
ChildHash string
AbsoluteLocation string
Headers []Header
}
func newItem(mu *sync.RWMutex, name, url, dlloc, hash string, totalSize ContentLength, opts *itemOpts) (i *Item, err error) {
if opts == nil {
opts = &itemOpts{}
}
opts.AbsoluteLocation, err = filepath.Abs(
opts.AbsoluteLocation,
)
if err != nil {
return
}
i = &Item{
Hash: hash,
Name: name,
Url: url,
Headers: opts.Headers,
DateAdded: time.Now(),
TotalSize: totalSize,
DownloadLocation: dlloc,
AbsoluteLocation: opts.AbsoluteLocation,
ChildHash: opts.ChildHash,
Hidden: opts.Hide,
Children: opts.Child,
Parts: make(map[int64]*ItemPart),
memPart: make(map[string]int64),
mu: mu,
}
return
}
func (i *Item) addPart(hash string, ioff, foff int64) {
i.mu.Lock()
defer i.mu.Unlock()
i.Parts[ioff] = &ItemPart{
Hash: hash,
FinalOffset: foff,
}
i.memPart[hash] = ioff
}
func (i *Item) savePart(offset int64, part *ItemPart) {
i.mu.Lock()
defer i.mu.Unlock()
i.Parts[offset] = part
}
func (i *Item) getPart(hash string) (offset int64, part *ItemPart) {
i.mu.RLock()
defer i.mu.RUnlock()
offset = i.memPart[hash]
part = i.Parts[offset]
return
}
func (i *Item) GetPercentage() int64 {
p := (i.Downloaded * 100) / i.TotalSize
return p.v()
}
func (i *Item) GetSavePath() (svPath string) {
svPath = GetPath(i.DownloadLocation, i.Name)
return
}
func (i *Item) GetAbsolutePath() (aPath string) {
aPath = GetPath(i.AbsoluteLocation, i.Name)
return
}
func (i *Item) Resume() error {
return i.dAlloc.Resume(i.Parts)
}