support thumb url (#62)
This commit is contained in:
@@ -32,10 +32,10 @@ func (s *Service) initRouter() {
|
||||
router.StaticFileFS("/", "./index.htm", http.FS(staticDir))
|
||||
|
||||
// Media
|
||||
router.GET("/image/:key", s.GetImage)
|
||||
router.GET("/video/:key", s.GetVideo)
|
||||
router.GET("/file/:key", s.GetFile)
|
||||
router.GET("/voice/:key", s.GetVoice)
|
||||
router.GET("/image/*key", s.GetImage)
|
||||
router.GET("/video/*key", s.GetVideo)
|
||||
router.GET("/file/*key", s.GetFile)
|
||||
router.GET("/voice/*key", s.GetVoice)
|
||||
router.GET("/data/*path", s.GetMediaData)
|
||||
|
||||
// MCP Server
|
||||
@@ -281,30 +281,51 @@ func (s *Service) GetVoice(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (s *Service) GetMedia(c *gin.Context, _type string) {
|
||||
key := c.Param("key")
|
||||
key := strings.TrimPrefix(c.Param("key"), "/")
|
||||
if key == "" {
|
||||
errors.Err(c, errors.InvalidArg(key))
|
||||
return
|
||||
}
|
||||
|
||||
media, err := s.db.GetMedia(_type, key)
|
||||
if err != nil {
|
||||
errors.Err(c, err)
|
||||
keys := util.Str2List(key, ",")
|
||||
if len(keys) == 0 {
|
||||
errors.Err(c, errors.InvalidArg(key))
|
||||
return
|
||||
}
|
||||
|
||||
if c.Query("info") != "" {
|
||||
c.JSON(http.StatusOK, media)
|
||||
var _err error
|
||||
for _, k := range keys {
|
||||
if len(k) != 32 {
|
||||
absolutePath := filepath.Join(s.ctx.DataDir, k)
|
||||
if _, err := os.Stat(absolutePath); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
c.Redirect(http.StatusFound, "/data/"+k)
|
||||
return
|
||||
}
|
||||
media, err := s.db.GetMedia(_type, k)
|
||||
if err != nil {
|
||||
_err = err
|
||||
continue
|
||||
}
|
||||
if c.Query("info") != "" {
|
||||
c.JSON(http.StatusOK, media)
|
||||
return
|
||||
}
|
||||
switch media.Type {
|
||||
case "voice":
|
||||
s.HandleVoice(c, media.Data)
|
||||
return
|
||||
default:
|
||||
c.Redirect(http.StatusFound, "/data/"+media.Path)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if _err != nil {
|
||||
errors.Err(c, _err)
|
||||
return
|
||||
}
|
||||
|
||||
switch media.Type {
|
||||
case "voice":
|
||||
s.HandleVoice(c, media.Data)
|
||||
default:
|
||||
c.Redirect(http.StatusFound, "/data/"+media.Path)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *Service) GetMediaData(c *gin.Context) {
|
||||
@@ -353,7 +374,8 @@ func (s *Service) HandleDatFile(c *gin.Context, path string) {
|
||||
case "bmp":
|
||||
c.Data(http.StatusOK, "image/bmp", out)
|
||||
default:
|
||||
c.File(path)
|
||||
c.Data(http.StatusOK, "image/jpg", out)
|
||||
// c.File(path)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ type Image struct {
|
||||
}
|
||||
|
||||
type Video struct {
|
||||
Md5 string `xml:"md5,attr"`
|
||||
RawMd5 string `xml:"rawmd5,attr"`
|
||||
// Length string `xml:"length,attr"`
|
||||
// PlayLength string `xml:"playlength,attr"`
|
||||
|
||||
@@ -79,7 +79,12 @@ func (m *Message) ParseMediaInfo(data string) error {
|
||||
case 3:
|
||||
m.Contents["md5"] = msg.Image.MD5
|
||||
case 43:
|
||||
m.Contents["md5"] = msg.Video.RawMd5
|
||||
if msg.Video.Md5 != "" {
|
||||
m.Contents["md5"] = msg.Video.Md5
|
||||
}
|
||||
if msg.Video.RawMd5 != "" {
|
||||
m.Contents["rawmd5"] = msg.Video.RawMd5
|
||||
}
|
||||
case 49:
|
||||
m.SubType = int64(msg.App.Type)
|
||||
switch m.SubType {
|
||||
@@ -234,7 +239,23 @@ func (m *Message) PlainTextContent() string {
|
||||
case 1:
|
||||
return m.Content
|
||||
case 3:
|
||||
return fmt.Sprintf("", m.Contents["host"], m.Contents["md5"])
|
||||
keylist := make([]string, 0)
|
||||
if m.Contents["md5"] != nil {
|
||||
if md5, ok := m.Contents["md5"].(string); ok {
|
||||
keylist = append(keylist, md5)
|
||||
}
|
||||
}
|
||||
if m.Contents["imgfile"] != nil {
|
||||
if imgfile, ok := m.Contents["imgfile"].(string); ok {
|
||||
keylist = append(keylist, imgfile)
|
||||
}
|
||||
}
|
||||
if m.Contents["thumb"] != nil {
|
||||
if thumb, ok := m.Contents["thumb"].(string); ok {
|
||||
keylist = append(keylist, thumb)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("", m.Contents["host"], strings.Join(keylist, ","))
|
||||
case 34:
|
||||
if voice, ok := m.Contents["voice"]; ok {
|
||||
return fmt.Sprintf("[语音](http://%s/voice/%s)", m.Contents["host"], voice)
|
||||
@@ -243,10 +264,28 @@ func (m *Message) PlainTextContent() string {
|
||||
case 42:
|
||||
return "[名片]"
|
||||
case 43:
|
||||
if path, ok := m.Contents["path"]; ok {
|
||||
return fmt.Sprintf("", m.Contents["host"], path)
|
||||
keylist := make([]string, 0)
|
||||
if m.Contents["md5"] != nil {
|
||||
if md5, ok := m.Contents["md5"].(string); ok {
|
||||
keylist = append(keylist, md5)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("", m.Contents["host"], m.Contents["md5"])
|
||||
if m.Contents["rawmd5"] != nil {
|
||||
if rawmd5, ok := m.Contents["rawmd5"].(string); ok {
|
||||
keylist = append(keylist, rawmd5)
|
||||
}
|
||||
}
|
||||
if m.Contents["videofile"] != nil {
|
||||
if videofile, ok := m.Contents["videofile"].(string); ok {
|
||||
keylist = append(keylist, videofile)
|
||||
}
|
||||
}
|
||||
if m.Contents["thumb"] != nil {
|
||||
if thumb, ok := m.Contents["thumb"].(string); ok {
|
||||
keylist = append(keylist, thumb)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("", m.Contents["host"], strings.Join(keylist, ","))
|
||||
case 47:
|
||||
return "[动画表情]"
|
||||
case 49:
|
||||
|
||||
@@ -96,7 +96,7 @@ func (m *MessageV3) Wrap() *Message {
|
||||
if len(parts) > 1 {
|
||||
path = strings.Join(parts[1:], "/")
|
||||
}
|
||||
_m.Contents["path"] = path
|
||||
_m.Contents["videofile"] = path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@ package model
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -85,10 +88,14 @@ func (m *MessageV4) Wrap(talker string) *Message {
|
||||
if packedInfo := ParsePackedInfo(m.PackedInfoData); packedInfo != nil {
|
||||
// FIXME 尝试解决 v4 版本 xml 数据无法匹配到 hardlink 记录的问题
|
||||
if _m.Type == 3 && packedInfo.Image != nil {
|
||||
_m.Contents["md5"] = packedInfo.Image.Md5
|
||||
_talkerMd5Bytes := md5.Sum([]byte(talker))
|
||||
talkerMd5 := hex.EncodeToString(_talkerMd5Bytes[:])
|
||||
_m.Contents["imgfile"] = filepath.Join("msg", "attach", talkerMd5, _m.Time.Format("2006-01"), "Img", fmt.Sprintf("%s.dat", packedInfo.Image.Md5))
|
||||
_m.Contents["thumb"] = filepath.Join("msg", "attach", talkerMd5, _m.Time.Format("2006-01"), "Img", fmt.Sprintf("%s_t.dat", packedInfo.Image.Md5))
|
||||
}
|
||||
if _m.Type == 43 && packedInfo.Video != nil {
|
||||
_m.Contents["md5"] = packedInfo.Video.Md5
|
||||
_m.Contents["videofile"] = filepath.Join("msg", "video", _m.Time.Format("2006-01"), fmt.Sprintf("%s.mp4", packedInfo.Video.Md5))
|
||||
_m.Contents["thumb"] = filepath.Join("msg", "video", _m.Time.Format("2006-01"), fmt.Sprintf("%s_thumb.jpg", packedInfo.Video.Md5))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user