/
main.go
115 lines (111 loc) · 3.24 KB
/
main.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
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/jondavidcody1/rtgo"
"github.com/satori/go.uuid"
"github.com/vincent-petithory/dataurl"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"log"
"os"
"time"
)
var app *rtgo.App
func UploadHandler(conn *rtgo.Conn, data []byte, msg *rtgo.Message) {
schema := make(map[string]interface{})
err := json.Unmarshal(msg.Payload, &schema)
if err != nil {
log.Println(err)
return
}
topic := schema["topic"].(string)
now := time.Now()
schema["timestamp"] = fmt.Sprintf("%d/%d/%d(%s)%d:%d:%d", now.Month(), now.Day(), now.Year(), now.Weekday().String()[:3], now.Hour(), now.Minute(), now.Second())
schema["uuid"] = uuid.NewV4().String()
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s%s", schema["timestamp"], schema["uuid"]))))[:9]
schema["hash"] = hash
if file, ok := schema["file"].(string); ok {
file_path := fmt.Sprintf("./static/images/uploads/%s", schema["file_name"])
fdata, err := dataurl.DecodeString(file)
if err != nil {
log.Println(err)
return
}
ioutil.WriteFile(file_path, fdata.Data, 0775)
saved_file, err := os.Open(file_path)
if err != nil {
log.Println(err)
return
}
var config image.Config
if schema["file_mime"] == "image/jpeg" {
config, err = jpeg.DecodeConfig(saved_file)
} else if schema["file_mime"] == "image/png" {
config, err = png.DecodeConfig(saved_file)
} else if schema["file_mime"] == "image/gif" {
config, err = gif.DecodeConfig(saved_file)
}
schema["file_dimensions"] = fmt.Sprintf("%dx%d", config.Width, config.Height)
delete(schema, "file")
}
if schema["type"] == "thread" {
if err := app.DB.InsertObj(topic, hash, schema); err != nil {
return
}
} else if schema["type"] == "reply" {
thread := schema["thread"].(string)
if obj, err := app.DB.GetObj(topic, thread); err == nil {
thisobj := obj.(map[string]interface{})
replies := thisobj["replies"].(map[string]interface{})
replies[hash] = schema
tagging := schema["tagging"].([]interface{})
for _, tag := range tagging {
if tag.(string) == thread {
thisobj["taggedBy"] = append(thisobj["taggedBy"].([]interface{}), hash)
} else if _, ok := replies[tag.(string)]; ok {
reply := replies[tag.(string)].(map[string]interface{})
reply["taggedBy"] = append(reply["taggedBy"].([]interface{}), hash)
}
}
if err := app.DB.InsertObj(topic, thread, thisobj); err != nil {
log.Println(err)
return
}
} else {
log.Println(err)
return
}
}
payload, err := json.Marshal(&schema)
if err != nil {
log.Println(err)
return
}
response := &rtgo.Message{
RoomLength: len(schema["topic"].(string)),
Room: schema["topic"].(string),
EventLength: len("new-" + schema["type"].(string)),
Event: "new-" + schema["type"].(string),
DstLength: 0,
Dst: "",
SrcLength: len(conn.Id),
Src: conn.Id,
PayloadLength: len(payload),
Payload: payload,
}
binary_response := rtgo.MessageToBytes(response)
conn.Emit(binary_response, response)
conn.Send <- binary_response
}
func main() {
app = rtgo.NewApp()
app.Parse("./config.json")
app.Emitter.On("new-thread", UploadHandler)
app.Emitter.On("new-reply", UploadHandler)
app.Start()
}