forked from TruthHun/CloudStore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
62 lines (51 loc) · 1.01 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
package cloudstore
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/hex"
"encoding/json"
"io/ioutil"
"os"
"strings"
)
var (
sevenDays int64 = 7 * 24 * 3600
)
// 绝对路径,abs => absolute
func objectAbs(object string) string {
return "/" + strings.TrimLeft(object, " ./")
}
// 相对路径 rel => relative
func objectRel(object string) string {
return strings.TrimLeft(object, " ./")
}
// MD5 Crypt
func MD5Crypt(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
func CompressByGzip(tmpFile, saveFile string) (err error) {
var (
input []byte
buf bytes.Buffer
)
input, err = ioutil.ReadFile(tmpFile)
if err != nil {
return
}
writer, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
defer writer.Close()
writer.Write(input)
writer.Flush()
err = ioutil.WriteFile(saveFile, buf.Bytes(), os.ModePerm)
return
}
func toJSON(v interface{}) (jsonStr string) {
p, err := json.Marshal(v)
if err != nil {
return
}
return string(p)
}