-
Notifications
You must be signed in to change notification settings - Fork 14
/
data_type.go
74 lines (66 loc) 路 1.65 KB
/
data_type.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
package handler
import (
"bytes"
"encoding/base64"
"html"
"github.com/number571/go-peer/cmd/hidden_lake/applications/messenger/internal/utils"
hlm_settings "github.com/number571/go-peer/cmd/hidden_lake/applications/messenger/pkg/settings"
)
func isText(pBytes []byte) bool {
if len(pBytes) == 0 {
return false
}
return pBytes[0] == hlm_settings.CIsText
}
func isFile(pBytes []byte) bool {
if len(pBytes) == 0 {
return false
}
return pBytes[0] == hlm_settings.CIsFile
}
func wrapText(pMsg string) []byte {
return bytes.Join([][]byte{
{hlm_settings.CIsText},
[]byte(pMsg),
}, []byte{})
}
func wrapFile(filename string, pBytes []byte) []byte {
return bytes.Join([][]byte{
{hlm_settings.CIsFile},
[]byte(filename),
{hlm_settings.CIsFile},
pBytes,
}, []byte{})
}
func unwrapText(pBytes []byte, pEscape bool) string {
if len(pBytes) == 0 { // need use first isText
panic("length of bytes = 0")
}
text := utils.ReplaceTextToEmoji(string(pBytes[1:]))
if pEscape {
return html.EscapeString(text)
}
return text
}
func unwrapFile(pBytes []byte, pEscape bool) (string, string) {
if len(pBytes) == 0 { // need use first isFile
panic("length of bytes = 0")
}
splited := bytes.Split(pBytes[1:], []byte{hlm_settings.CIsFile})
if len(splited) < 2 {
return "", ""
}
filename := string(splited[0])
if utils.HasNotWritableCharacters(filename) {
return "", ""
}
fileBytes := bytes.Join(splited[1:], []byte{hlm_settings.CIsFile})
if len(fileBytes) == 0 {
return "", ""
}
base64FileBytes := base64.StdEncoding.EncodeToString(fileBytes)
if pEscape {
return html.EscapeString(filename), base64FileBytes
}
return filename, base64FileBytes
}