-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress.go
More file actions
144 lines (120 loc) · 3.58 KB
/
progress.go
File metadata and controls
144 lines (120 loc) · 3.58 KB
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
package dl
import (
"encoding/json"
"fmt"
"os"
"sync/atomic"
"time"
)
type DownloadProgress struct {
Version int `json:"version"`
URI string `json:"uri"`
FileSize uint64 `json:"file_size"`
Filename string `json:"filename"`
Parts map[int]*PartProgress `json:"parts"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"last_updated"`
Completed bool `json:"completed"`
}
type PartProgress struct {
Index int `json:"index"`
StartByte uint64 `json:"start_byte"`
EndByte uint64 `json:"end_byte"`
Downloaded uint64 `json:"downloaded"`
Completed bool `json:"completed"`
LastModified time.Time `json:"last_modified"`
}
func (dl *Downloader) progressFilePath() string {
return fmt.Sprintf("%s%c.%s.dl_progress", dl.WorkingDir, os.PathSeparator, dl.Filename)
}
func (dl *Downloader) loadProgress() error {
progressPath := dl.progressFilePath()
data, err := os.ReadFile(progressPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to read progress file: %w", err)
}
var progress DownloadProgress
if err := json.Unmarshal(data, &progress); err != nil {
return fmt.Errorf("failed to parse progress file: %w", err)
}
if progress.URI != dl.URI || progress.FileSize != dl.fileSize {
fmt.Println("Progress file is for a different download, starting fresh")
return nil
}
dl.progress = &progress
return nil
}
func (dl *Downloader) saveProgress() error {
dl.progressMutex.Lock()
defer dl.progressMutex.Unlock()
if dl.progress == nil {
return nil
}
for i := range dl.partDownloaded {
if partProgress, ok := dl.progress.Parts[i]; ok {
partProgress.Downloaded = atomic.LoadUint64(&dl.partDownloaded[i].val)
partProgress.LastModified = time.Now()
}
}
dl.progress.LastUpdated = time.Now()
data, err := json.MarshalIndent(dl.progress, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal progress: %w", err)
}
tempPath := dl.progressFilePath() + ".tmp"
if err := os.WriteFile(tempPath, data, 0644); err != nil {
return fmt.Errorf("failed to write progress file: %w", err)
}
if err := os.Rename(tempPath, dl.progressFilePath()); err != nil {
return fmt.Errorf("failed to rename progress file: %w", err)
}
return nil
}
func (dl *Downloader) removeProgress() error {
return os.Remove(dl.progressFilePath())
}
func (dl *Downloader) initProgress() {
dl.progress = &DownloadProgress{
Version: 1,
URI: dl.URI,
FileSize: dl.fileSize,
Filename: dl.Filename,
Parts: make(map[int]*PartProgress),
Created: time.Now(),
LastUpdated: time.Now(),
}
for _, part := range dl.parts {
dl.progress.Parts[part.index] = &PartProgress{
Index: part.index,
StartByte: part.startByte,
EndByte: part.endByte,
Downloaded: 0,
Completed: false,
LastModified: time.Now(),
}
}
}
func (dl *Downloader) updatePartProgress(index int, downloaded uint64, completed bool) {
dl.progressMutex.Lock()
defer dl.progressMutex.Unlock()
if dl.progress != nil && dl.progress.Parts[index] != nil {
dl.progress.Parts[index].Downloaded = downloaded
dl.progress.Parts[index].Completed = completed
dl.progress.Parts[index].LastModified = time.Now()
}
}
func (dl *Downloader) getTotalDownloaded() uint64 {
dl.progressMutex.Lock()
defer dl.progressMutex.Unlock()
if dl.progress == nil {
return 0
}
var total uint64
for _, part := range dl.progress.Parts {
total += part.Downloaded
}
return total
}