-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
125 lines (109 loc) · 2.38 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
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
)
func FormatBytes(bytes int) string {
const (
B = 1
KiB = B * 1024
MiB = KiB * 1024
GiB = MiB * 1024
TiB = GiB * 1024
PiB = TiB * 1024
EiB = PiB * 1024
)
switch {
case bytes >= EiB:
return fmt.Sprintf("%.2f EiB", float64(bytes)/EiB)
case bytes >= PiB:
return fmt.Sprintf("%.2f PiB", float64(bytes)/PiB)
case bytes >= TiB:
return fmt.Sprintf("%.2f TiB", float64(bytes)/TiB)
case bytes >= GiB:
return fmt.Sprintf("%.2f GiB", float64(bytes)/GiB)
case bytes >= MiB:
return fmt.Sprintf("%.2f MiB", float64(bytes)/MiB)
case bytes >= KiB:
return fmt.Sprintf("%.2f KiB", float64(bytes)/KiB)
case bytes >= B+1:
return fmt.Sprintf("%d Bytes", bytes)
default:
return fmt.Sprintf("%d Byte", bytes)
}
}
func GetUniqueFilePath(path string) string {
dir := filepath.Dir(path)
ext := filepath.Ext(path)
base := strings.TrimSuffix(filepath.Base(path), ext)
uniquePath := path
for i := 1; ; i++ {
if _, err := os.Stat(uniquePath); os.IsNotExist(err) {
break
}
uniquePath = filepath.Join(dir, fmt.Sprintf("%s(%d)%s", base, i, ext))
}
return uniquePath
}
func Hyperlink(link string) string {
return fmt.Sprintf("\x1b]8;;file://%s\x1b\\%s\x1b]8;;\x1b\\", link, link)
}
func PrintHeader(header http.Header) {
fmt.Println("headers:")
for k, v := range header {
fmt.Printf("k: %v, v: %v\n", k, v)
}
}
func base64UrlDecode(str string) ([]byte, error) {
if str == "" {
return nil, errors.New("empty input")
}
str = strings.ReplaceAll(str, "-", "+")
str = strings.ReplaceAll(str, "_", "/")
eqs := (len(str) * 3) & 0x03
for i := 0; i < eqs; i++ {
str += "="
}
return base64.StdEncoding.DecodeString(str)
}
var sigChan = make(chan os.Signal, 1)
func catchSigs(ctx context.Context, cancel context.CancelFunc) {
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
for {
select {
case <-sigChan:
// 外部取消
cancel()
signal.Stop(sigChan)
close(sigChan)
return
case <-ctx.Done():
// 等待重试
continue
}
}
}
type Limiter struct {
Max int
sem chan struct{}
}
func NewLimiter(max int) *Limiter {
return &Limiter{
Max: max,
sem: make(chan struct{}, max),
}
}
func (l *Limiter) Acquire() {
l.sem <- struct{}{}
}
func (l *Limiter) Release() {
<-l.sem
}