-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.go
107 lines (94 loc) · 1.99 KB
/
util.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
package main
import (
"fmt"
"io/ioutil"
"math"
"mime"
"net"
"net/http"
"os"
"strings"
)
func filename(resp *http.Response) string {
if dispos := resp.Header.Get("content-disposition"); dispos != "" {
if _, params, err := mime.ParseMediaType(dispos); err == nil {
if filename, ok := params["filename"]; ok {
return filename
}
}
}
return ""
}
func tosafeFileName(name string) string {
empty := " "
replacer := strings.NewReplacer(
`\`, empty,
`/`, empty,
`:`, empty,
`*`, empty,
`?`, empty,
`"`, empty,
`<`, empty,
`>`, empty,
`|`, empty,
)
return replacer.Replace(name)
}
func urlJoin(base string, pathes ...string) string {
for _, path := range pathes {
base = strings.Trim(base, "/")
path = strings.Trim(path, "/")
base = base + "/" + path
}
return base
}
func wrapperTimeOutError(err error) error {
if IsTimeOutError(err) {
return fmt.Errorf("timeout")
}
return err
}
func IsTimeOutError(err error) bool {
switch e := err.(type) {
case net.Error:
if e.Timeout() {
return true
}
default:
return false
}
return false
}
func IsValidFilename(fp string) bool {
// Check if file already exists
if _, err := os.Stat(fp); err == nil {
return true
}
// Attempt to create it
var d []byte
if err := ioutil.WriteFile(fp, d, 0644); err == nil {
os.Remove(fp) // And delete it
return true
}
return false
}
func Bytes(s uint64) string {
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
return humanateBytes(s, 1000, sizes)
}
func humanateBytes(s uint64, base float64, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%d B", s)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10
f := "%.0f %s"
if val < 10 {
f = "%.1f %s"
}
return fmt.Sprintf(f, val, suffix)
}
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}