Skip to content

Commit

Permalink
feat(img.pool): adapt to ntqq
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed May 1, 2024
1 parent 083e678 commit 9500bfe
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 13 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.19

require (
github.com/FloatTech/ZeroBot-Plugin-Webui v1.0.1-0.20230412164529-cfe878675931
github.com/FloatTech/floatbox v0.0.0-20231107124407-e38535efa2a2
github.com/FloatTech/floatbox v0.0.0-20240304064110-78a83f1af9a6
github.com/FloatTech/gg v1.1.2
github.com/FloatTech/imgfactory v0.2.2-0.20230315152233-49741fc994f9
github.com/FloatTech/rendercard v0.0.10-0.20230223064326-45d29fa4ede9
github.com/FloatTech/sqlite v1.6.3
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b
github.com/FloatTech/zbpctrl v1.6.0
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e
github.com/FloatTech/zbpctrl v1.6.1-0.20240501122621-27fbe564d42a
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
github.com/disintegration/imaging v1.6.2
github.com/fumiama/cron v1.3.0
Expand All @@ -27,7 +27,7 @@ require (
github.com/swaggo/gin-swagger v1.5.3
github.com/swaggo/swag v1.8.10
github.com/tidwall/gjson v1.14.4
github.com/wdvxdr1123/ZeroBot v1.7.5-0.20231009162356-57f71b9f5258
github.com/wdvxdr1123/ZeroBot v1.7.5-0.20240501122138-fdbe4e1d21b4
)

require (
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/FloatTech/ZeroBot-Plugin-Webui v1.0.1-0.20230412164529-cfe878675931 h1:jTc1mb2sG1krnxpgD75SMojeOMIl6bGX5t3YGIRKkHI=
github.com/FloatTech/ZeroBot-Plugin-Webui v1.0.1-0.20230412164529-cfe878675931/go.mod h1:Se2A9vnC4eSYF7vR2LNSayV7rvbFcpgVs1ZN4sEYMyg=
github.com/FloatTech/floatbox v0.0.0-20231107124407-e38535efa2a2 h1:O4kptIzgYzNwZlBARZFv8EkA40yB6M5LGxxIF7NKLR8=
github.com/FloatTech/floatbox v0.0.0-20231107124407-e38535efa2a2/go.mod h1:TeTlp+hTxpJti4JSdmUqzxGEr4wUBOVct9YWBepilpc=
github.com/FloatTech/floatbox v0.0.0-20240304064110-78a83f1af9a6 h1:Vaj8ulVbN8vTD4W7aj+BYeT6x8iKFneDnkgBCezomDY=
github.com/FloatTech/floatbox v0.0.0-20240304064110-78a83f1af9a6/go.mod h1:TeTlp+hTxpJti4JSdmUqzxGEr4wUBOVct9YWBepilpc=
github.com/FloatTech/gg v1.1.2 h1:YolgOYg3uDHc1+g0bLtt6QuRA/pvLn+b9IBCIhOOX88=
github.com/FloatTech/gg v1.1.2/go.mod h1:uzPzAeT35egARdRuu+1oyjU3CmTwCceoq3Vvje7LpcI=
github.com/FloatTech/imgfactory v0.2.2-0.20230315152233-49741fc994f9 h1:IzZLuM/fgKclyMaU/Qb1qlLdGrs2FTietkqOWhh07Gw=
Expand All @@ -13,16 +13,16 @@ github.com/FloatTech/sqlite v1.6.3 h1:MQkqBNlkPuCoKQQgoNLuTL/2Ci3tBTFAnVYBdD0Wy4
github.com/FloatTech/sqlite v1.6.3/go.mod h1:zFbHzRfB+CJ+VidfjuVbrcin3DAz283F7hF1hIeHzpY=
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b h1:tvciXWq2nuvTbFeJGLDNIdRX3BI546D3O7k7vrVueZw=
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
github.com/FloatTech/zbpctrl v1.6.0 h1:BWg9aRR4bUCmNNKj6GPH0TmzFRWYImIi6rQcQTTYRs4=
github.com/FloatTech/zbpctrl v1.6.0/go.mod h1:i3GGM5K4HiDsXzvmXQSYoH1QT3tsSaAHjRzHwKGsHG0=
github.com/FloatTech/zbpctrl v1.6.1-0.20240501122621-27fbe564d42a h1:9HPKT4WuO5nYVxVRie7OTn2kV9cwaktulkZbBbCqXkg=
github.com/FloatTech/zbpctrl v1.6.1-0.20240501122621-27fbe564d42a/go.mod h1:RxqNENMwHUUZz5IazDxhnmyejGV0xQthYpScMU9cWbw=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e h1:wR3MXQ3VbUlPKOOUwLOYgh/QaJThBTYtsl673O3lqSA=
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 h1:bBmmB7he0iVN4m5mcehfheeRUEer/Avo4ujnxI3uCqs=
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5/go.mod h1:0UcFaCkhp6vZw6l5Dpq0Dp673CoF9GdvA8lTfst0GiU=
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
Expand Down Expand Up @@ -170,8 +170,8 @@ github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/wdvxdr1123/ZeroBot v1.7.5-0.20231009162356-57f71b9f5258 h1:Q0dKoj9SHrR8WjjlcX+eyYBjQKqBn/x1pdJJO1IIOxQ=
github.com/wdvxdr1123/ZeroBot v1.7.5-0.20231009162356-57f71b9f5258/go.mod h1:y29UIOy0RD3P+0meDNIWRhcJF3jtWPN9xP9hgt/AJAU=
github.com/wdvxdr1123/ZeroBot v1.7.5-0.20240501122138-fdbe4e1d21b4 h1:9no48IDhvVzY9BjmSALMQfw/QiV9MFeT0vk069aTRdM=
github.com/wdvxdr1123/ZeroBot v1.7.5-0.20240501122138-fdbe4e1d21b4/go.mod h1:J6uHaXS/Am2VsLxF9TcU6il19PbOeC4SvgxHJ1E2jaE=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
36 changes: 35 additions & 1 deletion img/pool/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"net/http"
"regexp"
"strings"

"github.com/sirupsen/logrus"
Expand All @@ -24,6 +25,10 @@ var (
ErrGetMsg = errors.New("get msg error")
)

var (
oldimgre = regexp.MustCompile(`^[0-9A-F-]+$`)
)

// Image 图片数据
type Image struct {
*item
Expand Down Expand Up @@ -84,7 +89,14 @@ func (m *Image) String() string {
if m.item == nil {
return m.f
}
return fmt.Sprintf(cacheurl, m.item.u)
if oldimgre.MatchString(m.item.u) {
return fmt.Sprintf(cacheurl, m.item.u)
}
nu, err := unpack(m.item.u)
if err != nil {
return m.f
}
return string(nu)
}

// SetFile f
Expand All @@ -109,6 +121,28 @@ func (m *Image) Push(send ctxext.NoCtxSendMsg, get ctxext.NoCtxGetMsg) (hassent
for _, e := range msg.Elements {
if e.Type == "image" {
u := e.Data["url"]
if ntcachere.MatchString(u) { // is NTQQ
raw := ""
raw, err = nturl(u).pack()
if err != nil {
logrus.Errorln("[imgpool] pack nturl err:", err)
err = nil
return
}
m.item, err = newItem(m.n, raw)
if err != nil {
logrus.Errorln("[imgpool] get newItem err:", err)
err = nil
return
}
logrus.Debugln("[imgpool] 缓存:", m.n, "url:", u)
err = m.item.push("minamoto")
if err != nil {
logrus.Errorln("[imgpool] item.push err:", err)
err = nil
}
return
}
i := strings.LastIndex(u, "/")
if i <= 0 {
break
Expand Down
69 changes: 69 additions & 0 deletions img/pool/nt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package pool

import (
"encoding/base64"
"errors"
"fmt"
"regexp"

"github.com/FloatTech/floatbox/binary"
)

const (
ntcacheurlprefix = "https://multimedia.nt.qq.com.cn/download?appid=1407&fileid="
ntcacheurl = ntcacheurlprefix + "%s&spec=0&rkey=%s"
ntappidlen = 60
ntrkeylen = 46
ntrawlen = ntappidlen + ntrkeylen
)

var ntcachere = regexp.MustCompile(`^https://multimedia.nt.qq.com.cn/download\?appid=1407&fileid=([0-9a-zA-Z_-]+)&spec=0&rkey=([0-9a-zA-Z_-]+)$`)

var (
ErrInvalidNTURL = errors.New("invalid nt url")
ErrInvalidNTRaw = errors.New("invalid nt raw")
)

type nturl string

func unpack(raw string) (nturl, error) {
if len(raw) != ntrawlen {
return "", ErrInvalidNTRaw
}
rb := binary.StringToBytes(raw)
b := rb[ntappidlen-1]
fileid := base64.RawURLEncoding.EncodeToString(rb[:59])
if len(fileid) < int(b) {
return "", ErrInvalidNTRaw
}
fileid = fileid[:b]
rkey := base64.RawURLEncoding.EncodeToString(rb[60:])
b = rb[ntrawlen-1]
if len(rkey) < int(b) {
return "", ErrInvalidNTRaw
}
rkey = rkey[:b]
return nturl(fmt.Sprintf(ntcacheurl, fileid, rkey)), nil
}

// pack url into pool
func (nu nturl) pack() (string, error) {
subs := ntcachere.FindStringSubmatch(string(nu))
if len(subs) != 3 {
return "", ErrInvalidNTURL
}
var buf [ntrawlen]byte
fileid := subs[1]
rkey := subs[2]
_, err := base64.RawURLEncoding.AppendDecode(buf[:0], binary.StringToBytes(fileid))

Check failure on line 58 in img/pool/nt.go

View workflow job for this annotation

GitHub Actions / lint

base64.RawURLEncoding.AppendDecode undefined (type *base64.Encoding has no field or method AppendDecode)
if err != nil {
return "", err
}
buf[ntappidlen-1] = byte(len(fileid))
_, err = base64.RawURLEncoding.AppendDecode(buf[60:60], binary.StringToBytes(rkey))

Check failure on line 63 in img/pool/nt.go

View workflow job for this annotation

GitHub Actions / lint

base64.RawURLEncoding.AppendDecode undefined (type *base64.Encoding has no field or method AppendDecode) (typecheck)
if err != nil {
return "", err
}
buf[ntrawlen-1] = byte(len(rkey))
return binary.BytesToString(buf[:]), nil
}
28 changes: 28 additions & 0 deletions img/pool/nt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package pool

import (
"encoding/hex"
"fmt"
"testing"

"github.com/FloatTech/floatbox/binary"
)

func TestNTPacker(t *testing.T) {
rkeyb64 := "CAESKBkcro_MGujokpPszCPSJtCwBMcJAkNIEqxA0gVXuTCaxQLbnGx4yk4"
idb64 := "CgoyNzE1NDM3MTQwEhS2wXeeCxuBUgynUmsZ7oYX2r80ARiO468GIP8KKIGuxu_A7IUDUIC9owE"
nuurl := nturl(fmt.Sprintf(ntcacheurl, idb64, rkeyb64))
t.Log(nuurl)
raw, err := nuurl.pack()
if err != nil {
t.Fatal(err)
}
t.Log(hex.EncodeToString(binary.StringToBytes(raw)))
upknt, err := unpack(raw)
if err != nil {
t.Fatal(err)
}
if upknt != nuurl {
t.Fatal("expected", nuurl, "but got", upknt)
}
}

0 comments on commit 9500bfe

Please sign in to comment.