-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrud.go
150 lines (137 loc) · 3.07 KB
/
crud.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"hash/crc32"
"math"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
var pptter = PPTTER{
GroupNum: 1,
GroupName: "pptter",
GroupUser: make(map[*websocket.Conn]bool),
GroupDb: make(chan Message, 50),
SendMessage: make(chan []byte),
}
type PPTTER struct {
GroupNum int
GroupName string
GroupUser map[*websocket.Conn]bool
GroupDb chan Message
SendMessage chan []byte
Writelock sync.Mutex //锁我可能没用对,反正也没啥后果
}
type Message struct {
Name string
Time int64
Say string
FileName string
Filetype string
}
// 根据名字用crc32校验生成头像
func (m Message) Name2avatar() string {
//服了golang这个老六了,uint32逸出和前端半天对不上
v := crc32.ChecksumIEEE([]byte(m.Name))
return strconv.Itoa(int(v))[:2]
}
// 时间戳转日期
func (m Message) Timestr() string {
//tm := time.Unix(m.Time, 0)
//return tm.Format("01-02 15:04")
var byTime = []int64{365 * 24 * 60 * 60, 24 * 60 * 60, 60 * 60, 60, 1}
var unit = []string{"年前", "天前", "小时前", "分钟前", "秒钟前"}
now := time.Now().Unix()
ct := now - m.Time
if ct < 0 {
return "刚刚"
}
var res string
for i := 0; i < len(byTime); i++ {
if ct < byTime[i] {
continue
}
var temp = math.Floor(float64(ct / byTime[i]))
ct = ct % byTime[i]
if temp > 0 {
var tempStr string
tempStr = strconv.FormatFloat(temp, 'f', -1, 64)
res = MergeString(tempStr, unit[i])
}
break
}
return res
}
// 判断文件类型
func (m Message) Filetypecut() string {
cut := strings.Split(m.Filetype, "/")
return cut[0]
}
// 文本消息处理
func (t *PPTTER) crudtext(message []byte) {
m := Message{}
err := json.Unmarshal(message, &m)
if err != nil {
fmt.Println("Convert json message failed:", err)
return
}
if len(t.GroupDb) == 50 {
del := <-t.GroupDb
os.Remove(".tmp/" + del.FileName)
}
t.GroupDb <- m
mbyte, _ := json.Marshal(m)
t.SendMessage <- mbyte
}
// 文件消息处理
func (t *PPTTER) crudbin(message []byte, name string) {
filename := strconv.Itoa(int(crc32.ChecksumIEEE(message)))
m := Message{
Name: name,
Time: time.Now().Unix(),
Say: "",
FileName: filename,
Filetype: http.DetectContentType(message),
}
err := os.WriteFile(".tmp/"+filename, message, os.ModePerm)
if err != nil {
fmt.Println(err)
}
if len(t.GroupDb) == 50 {
del := <-t.GroupDb
os.Remove(".tmp/" + del.FileName)
}
t.GroupDb <- m
mbyte, _ := json.Marshal(m)
t.SendMessage <- mbyte
}
// 消息打包成切片,发给http渲染
func (t PPTTER) Gettmp() []Message {
tmp := make([]Message, 0, 50)
t.Writelock.TryLock()
for i := 0; i < len(t.GroupDb); i++ {
v := <-t.GroupDb
tmp = append(tmp, v)
t.GroupDb <- v
}
t.Writelock.Unlock()
return tmp
}
// 在线人数
func (t PPTTER) Usercount() int {
return len(t.GroupUser)
}
// 字符串拼接
func MergeString(args ...string) string {
buffer := bytes.Buffer{}
for i := 0; i < len(args); i++ {
buffer.WriteString(args[i])
}
return buffer.String()
}