-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
64 lines (53 loc) · 1.04 KB
/
state.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"path"
"time"
)
var stateFile = "state.json"
type state struct {
Path string `json:"path"`
Videos []video `json:"videos"`
}
func showProgress(file string, contentLen int64, done chan int64) {
var (
stop bool
toMB = float64(1024 * 1024)
)
for {
select {
case <-done:
stop = true
default:
size := fileSize(file)
fmt.Printf("\033[2K\r Downloading %s -----> Received %.1fM out of %.1fM", path.Base(file),
float64(size)/toMB, float64(contentLen)/toMB)
}
if stop {
break
}
time.Sleep(1 * time.Second)
}
}
func saveToDisk(reader io.Reader, writter io.Writer) int64 {
written, err := io.Copy(writter, reader)
if err != nil {
log.Fatalf("Error while CopyToDisk: %v", err)
}
return written
}
func (h *hunter) saveState() {
states := state{
Path: h.Path,
Videos: h.Videos,
}
data, err := json.MarshalIndent(states, " ", " ")
if err != nil {
log.Fatalf("Failed to save state(s): %v", err)
}
ioutil.WriteFile(stateFile, data, 0600)
}