-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
111 lines (93 loc) · 2.36 KB
/
common.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
package common
import (
"crypto/md5"
"encoding/hex"
"io"
"math"
"strconv"
"github.com/klauspost/compress/zstd"
)
var suffixes = [6]string{"B", "KB", "MB", "GB", "TB", "PB"}
// HumanFileSize converts a int64 representing the number of bytes
// into a human readable string
func HumanFileSize(size int64) string {
floatSize := float64(size)
if floatSize <= 0 {
return "Empty File"
}
base := math.Log(floatSize) / math.Log(1024)
getSize := round(math.Pow(1024, base-math.Floor(base)), .5, 2)
suffixBase := int(math.Floor(base))
if suffixBase < 0 || suffixBase > 5 {
return "File Size is out of Index"
}
getSuffix := suffixes[int(math.Floor(base))]
return strconv.FormatFloat(getSize, 'f', -1, 64) + " " + string(getSuffix)
}
func round(val float64, roundOn float64, places int) float64 {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}
type Semaphore interface {
Acquire()
Release()
Close()
}
type semaphore struct {
semC chan struct{}
}
func NewSemaphore(maxConcurrency int) Semaphore {
return &semaphore{
semC: make(chan struct{}, maxConcurrency),
}
}
func (s *semaphore) Acquire() {
s.semC <- struct{}{}
}
func (s *semaphore) Release() {
<-s.semC
}
// Is this needed?
func (s *semaphore) Close() {
close(s.semC)
}
// ZstdDecompressReader decompresses the input data from the provided reader using Zstandard compression algorithm
// and writes the decompressed data to the provided writer.
// It returns an error if the decompression fails.
func ZstdDecompressReader(in io.Reader, out io.Writer) error {
d, err := zstd.NewReader(in)
if err != nil {
return err
}
defer d.Close()
// Copy content...
_, err = io.Copy(out, d)
return err
}
// ZstdCompressReader compresses the data from the input reader using Zstandard compression algorithm
// and writes the compressed data to the output writer.
// It returns an error if any error occurs during the compression process.
func ZstdCompressReader(in io.Reader, out io.Writer) error {
enc, err := zstd.NewWriter(out)
if err != nil {
return err
}
_, err = io.Copy(enc, in)
if err != nil {
enc.Close()
return err
}
return enc.Close()
}
func MD5Hash(data []byte) (hash string) {
h := md5.Sum(data)
return hex.EncodeToString(h[:])
}