-
Notifications
You must be signed in to change notification settings - Fork 13
/
mech.go
73 lines (67 loc) · 1.28 KB
/
mech.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
package mech
// github.com/89z
import (
"bytes"
"encoding/json"
"mime"
"strconv"
"strings"
)
func ExtensionByType(typ string) (string, error) {
media, _, err := mime.ParseMediaType(typ)
if err != nil {
return "", err
}
switch media {
case "audio/mpeg":
return ".mp3", nil
case "audio/mp4":
return ".m4a", nil
case "audio/webm":
return ".weba", nil
case "video/mp4":
return ".m4v", nil
case "video/webm":
return ".webm", nil
}
return "", notFound{typ}
}
type notFound struct {
value string
}
func (n notFound) Error() string {
var buf []byte
buf = strconv.AppendQuote(buf, n.value)
buf = append(buf, " is not found"...)
return string(buf)
}
func Clean(path string) string {
fn := func(r rune) rune {
switch r {
case
'"',
'*',
'/',
':',
'<',
'>',
'?',
'\\',
'|',
'’': // github.com/PowerShell/PowerShell/issues/16084
return -1
}
return r
}
return strings.Map(fn, path)
}
func Encode[T any](value T) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
err := enc.Encode(value)
if err != nil {
return nil, err
}
return buf, nil
}