forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
112 lines (86 loc) · 1.74 KB
/
utils.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
package launcher
import (
"archive/zip"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"time"
"github.com/go-rod/rod/lib/utils"
)
var inContainer = utils.InContainer
type progresser struct {
size int
count int
logger utils.Logger
last time.Time
}
func (p *progresser) Write(b []byte) (n int, err error) {
n = len(b)
if p.count == 0 {
p.logger.Println("Progress:")
}
p.count += n
if p.count == p.size {
p.logger.Println("100%")
return
}
if time.Since(p.last) < time.Second {
return
}
p.last = time.Now()
p.logger.Println(fmt.Sprintf("%02d%%", p.count*100/p.size))
return
}
func toHTTP(u url.URL) *url.URL {
newURL := u
if newURL.Scheme == "ws" {
newURL.Scheme = "http"
} else if newURL.Scheme == "wss" {
newURL.Scheme = "https"
}
return &newURL
}
func toWS(u url.URL) *url.URL {
newURL := u
if newURL.Scheme == "http" {
newURL.Scheme = "ws"
} else if newURL.Scheme == "https" {
newURL.Scheme = "wss"
}
return &newURL
}
func unzip(logger utils.Logger, from, to string) (err error) {
defer func() {
if e := recover(); e != nil {
err = e.(error)
}
}()
logger.Println("Unzip to:", to)
zr, err := zip.OpenReader(from)
utils.E(err)
size := 0
for _, f := range zr.File {
size += int(f.FileInfo().Size())
}
progress := &progresser{size: size, logger: logger}
for _, f := range zr.File {
p := filepath.Join(to, f.Name)
_ = utils.Mkdir(filepath.Dir(p))
if f.FileInfo().IsDir() {
err := os.Mkdir(p, f.Mode())
utils.E(err)
continue
}
r, err := f.Open()
utils.E(err)
dst, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE|os.O_TRUNC, f.Mode())
utils.E(err)
_, err = io.Copy(io.MultiWriter(dst, progress), r)
utils.E(err)
err = dst.Close()
utils.E(err)
}
return zr.Close()
}