/
utils.go
83 lines (73 loc) · 1.87 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
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package utils
import (
"bytes"
"compress/zlib"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
"strings"
)
func DomainFromID(id string) (string, error) {
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
return "", fmt.Errorf("invalid ID %q missing ':'", id)
}
return parts[1], nil
}
func NickNameFromID(id string) (string, error) {
s := strings.Index(id, "@")
if s < 0 {
return id, fmt.Errorf("invalid ID %q missing '@'", id)
}
s += len("@")
e := strings.Index(id[s:], ":")
if e < 0 {
return id, fmt.Errorf("invalid ID %q missing ':'", id)
}
return id[s : s+e], nil
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func GetStringHash(s string) string {
signByte := []byte(s)
hash := md5.New()
hash.Write(signByte)
return hex.EncodeToString(hash.Sum(nil))
}
func DoCompress(src []byte) []byte {
var in bytes.Buffer
w, _ := zlib.NewWriterLevel(&in, zlib.BestCompression)
w.Write(src)
w.Close()
return in.Bytes()
}
func DoUnCompress(compressSrc []byte) []byte {
b := bytes.NewReader(compressSrc)
var out bytes.Buffer
r, _ := zlib.NewReader(b)
io.Copy(&out, r)
return out.Bytes()
}