-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortsdk.go
63 lines (54 loc) · 1.55 KB
/
shortsdk.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
package main
import (
"regexp"
"strconv"
"strings"
)
type LinkInfo struct {
URL string
URLID uint32
IP string
HitCounter uint
Paid bool
Resolved bool
ReturnStatus int
Title string
PreviewImage string
PreviewText string
}
var linksRoot = "http://127.0.0.1:12345/"
var linkRE = regexp.MustCompile(`(https?:\/\/\S+)`)
var shortLinkRE = regexp.MustCompile("(" + linksRoot + `\d+)`)
func linkReplace(src string) string {
b := []byte(src)
b = linkRE.ReplaceAllFunc(b, linkShortInplace)
return (string(b))
}
func inflateLinkWidgets(msg string) string {
b := []byte(msg)
b = shortLinkRE.ReplaceAllFunc(b, inflateLinkWidgetsInplace)
return (string(b))
}
func inflateLinkWidgetsInplace(in []byte) []byte {
str := string(in)
strParts := strings.Split(str, "/")
uid64, _ := strconv.ParseUint(strParts[3], 10, 32)
uid := uint32(uid64)
return ([]byte(shortLinkWidget(uid)))
}
func getItemPreview(item *LinkInfo) string {
if item.PreviewImage == "" {
return ""
}
return ("<img style=display:block src='" + item.PreviewImage + "' border=0>")
}
func shortLinkWidget(uid uint32) string {
link := shortLinkURL(uid)
return "<span title=status:" +
strconv.Itoa(int(linkhash[uid].ReturnStatus)) +
" style='color:cyan;border:1px dashed blue;border-radius:5px;background:green'><a style=color:cyan href=" + link + ">" +
linkhash[uid].Title +
"</a> (" +
strconv.Itoa(int(linkhash[uid].HitCounter)) +
")" + getItemPreview(linkhash[uid]) + "</span>"
}