]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Fix 表情代码包含文件名非法字符
authorqydysky <qydysky@foxmail.com>
Sun, 28 Jul 2024 09:42:42 +0000 (17:42 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 28 Jul 2024 09:42:42 +0000 (17:42 +0800)
Reply/F.go
Reply/F/danmuEmotes/danmuEmotes.go

index 76f1c1e5cf1064ed3917f5e3eabaa37b43d72f0a..02acea734034dce217f3f46a5b9c704476b25202 100644 (file)
@@ -30,6 +30,7 @@ import (
        c "github.com/qydysky/bili_danmu/CV"
        F "github.com/qydysky/bili_danmu/F"
        _ "github.com/qydysky/bili_danmu/Reply/F"
+       danmuemotes "github.com/qydysky/bili_danmu/Reply/F/danmuEmotes"
        "github.com/qydysky/bili_danmu/Reply/F/danmuXml"
        "github.com/qydysky/bili_danmu/Reply/F/keepMedalLight"
        videoInfo "github.com/qydysky/bili_danmu/Reply/F/videoInfo"
@@ -1318,7 +1319,7 @@ func init() {
                        }
 
                        if strings.HasPrefix(p, "emots/") {
-                               //
+                               p, _ = danmuemotes.Hashr.Run(context.Background(), p)
                        } else {
                                p = "html/artPlayer/" + p
                        }
index 81e7e7004bfa2a0a806fb572c4502f10cbc224f6..1b3d45489221cd87745f9918b74750c1595a4d45 100644 (file)
@@ -9,13 +9,32 @@ import (
        comp "github.com/qydysky/part/component"
        file "github.com/qydysky/part/file"
        log "github.com/qydysky/part/log"
+       ppool "github.com/qydysky/part/pool"
        reqf "github.com/qydysky/part/reqf"
+       pslice "github.com/qydysky/part/slice"
 )
 
 // path
 var (
        SaveEmote = comp.NewComp(saveEmote)
-       replace   = strings.NewReplacer(`| `, `\ `, `/ `, `: `, `* `, `? `, `" `, `< `, `> `, `| `)
+       Hashr     = comp.NewComp(func(ctx context.Context, s string) (r string, e error) {
+               return hashr(s), nil
+       })
+       danmuPool = ppool.New(ppool.PoolFunc[pslice.Buf[byte]]{
+               New: func() *pslice.Buf[byte] {
+                       return pslice.New[byte]()
+               },
+               InUse: func(b *pslice.Buf[byte]) bool {
+                       return !b.IsEmpty()
+               },
+               Reuse: func(b *pslice.Buf[byte]) *pslice.Buf[byte] {
+                       return b
+               },
+               Pool: func(b *pslice.Buf[byte]) *pslice.Buf[byte] {
+                       b.Reset()
+                       return b
+               },
+       }, 100)
 )
 
 type Danmu struct {
@@ -33,10 +52,11 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
                if url, ok := m[`url`].(string); ok {
                        if !strings.Contains(*ptr.Msg, "[") {
                                if emoticon_unique, ok := m[`emoticon_unique`].(string); ok {
-                                       *ptr.Msg = "[" + *ptr.Msg + emoticon_unique + "]"
+                                       *ptr.Msg = "[" + hashr(*ptr.Msg+emoticon_unique) + "]"
                                }
                        }
-                       savePath := "emots/" + replace.Replace(*ptr.Msg) + ".png"
+
+                       savePath := "emots/" + *ptr.Msg + ".png"
                        if !file.New(savePath, 0, true).IsExist() {
                                go func() {
                                        req := c.C.ReqPool.Get()
@@ -71,6 +91,7 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
                                if e := json.Unmarshal([]byte(extrab), &E); e != nil {
                                        return nil, e
                                } else {
+                                       *ptr.Msg = hashr(*ptr.Msg)
                                        for k, v := range E.Emots {
                                                m, ok := v.(map[string]any)
                                                if !ok {
@@ -82,11 +103,10 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
                                                        continue
                                                }
 
-                                               savePath := "emots/" + replace.Replace(k) + ".png"
+                                               savePath := hashr("emots/" + k + ".png")
                                                if file.New(savePath, 0, true).IsExist() {
                                                        continue
                                                }
-
                                                go func() {
                                                        req := c.C.ReqPool.Get()
                                                        defer c.C.ReqPool.Put(req)
@@ -116,3 +136,44 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
        }
        return
 }
+
+func hashr(s string) (r string) {
+       buf := danmuPool.Get()
+       defer danmuPool.Put(buf)
+
+       emoteB := false
+       for i := 0; i < len(s); i++ {
+               if !emoteB {
+                       _ = buf.Append([]byte{s[i]})
+                       emoteB = s[i] == '['
+                       continue
+               } else if s[i] == ']' {
+                       _ = buf.Append([]byte{s[i]})
+                       emoteB = false
+                       continue
+               }
+               switch s[i] {
+               case '\\':
+                       _ = buf.Append([]byte("_1"))
+               case '/':
+                       _ = buf.Append([]byte("_2"))
+               case '*':
+                       _ = buf.Append([]byte("_3"))
+               case '<':
+                       _ = buf.Append([]byte("_4"))
+               case '>':
+                       _ = buf.Append([]byte("_5"))
+               case '|':
+                       _ = buf.Append([]byte("_6"))
+               case ':':
+                       _ = buf.Append([]byte(":"))
+               case '?':
+                       _ = buf.Append([]byte("?"))
+               case '"':
+                       _ = buf.Append([]byte("“"))
+               default:
+                       _ = buf.Append([]byte{s[i]})
+               }
+       }
+       return string(buf.GetCopyBuf())
+}