修复图片卡密发送与自动化延迟调度
图片卡密改为下载 URL 内容后按账号正常发送;批量铺货支持发布强制间隔;持久化延迟动作改为秒级调度,保留原有生命周期收口。
This commit is contained in:
1 parent
cad43780af
commit
3c46dd910b
20 files changed
+498
-42
No files matched your search
@@ -162,8 +162,10 @@ func NewRuntimeBundle(store *db.Store, bm *browser.Manager, logger *slog.Logger)
|
||||
manager := accountmanager.NewManager(store, runtimeAdapter, logger)
|
||||
// notifier 是自动化与账号告警共用的通知出口,构造完成后不可替换。
|
||||
notifier := notify.New("", store, logger)
|
||||
// automationSenders 为自动化图片卡密注入“临时下载、平台上传、WebSocket 发送”链路,不在本地保存图片。
|
||||
automationSenders := NewAutomationImageSenderProvider(store, manager, func() mtop.Client { return mtop.NewClient() })
|
||||
// autoCenter 依赖已构造但尚未启动的 manager 与 adapter,避免运行期形成部分可用状态。
|
||||
autoCenter := automation.NewWithDependencies(store, manager, logger, automation.CenterDependencies{
|
||||
autoCenter := automation.NewWithDependencies(store, automationSenders, logger, automation.CenterDependencies{
|
||||
OrderDetailFetcher: runtimeAdapter,
|
||||
Notifier: notifier,
|
||||
})
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
package adapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
accountmanager "xianyu-go/internal/account"
|
||||
chatapp "xianyu-go/internal/application/chat"
|
||||
"xianyu-go/internal/automation"
|
||||
"xianyu-go/internal/db"
|
||||
"xianyu-go/internal/netguard"
|
||||
"xianyu-go/internal/xianyu/mtop"
|
||||
)
|
||||
|
||||
// automationImageMaxBytes 限制一次自动发货从远程 URL 读取的图片体积,避免无状态容器被单个卡密占满内存。
|
||||
const automationImageMaxBytes = 10 << 20
|
||||
|
||||
// automationImageDownloader 只在一次自动发货内下载图片内容;返回值不会写入本地文件或数据库。
|
||||
type automationImageDownloader func(ctx context.Context, rawURL string) (data []byte, contentType string, filename string, err error)
|
||||
|
||||
// automationImageUploader 定义自动化图片上传所需的最小平台能力,凭证始终封装在 adapter 内部。
|
||||
type automationImageUploader interface {
|
||||
// UploadChatImage 将内存中的图片上传为当前账号可发送的闲鱼图片地址。
|
||||
UploadChatImage(ctx context.Context, accountID, filename, contentType string, data []byte) (chatapp.ImageUpload, error)
|
||||
}
|
||||
|
||||
// automationImageSenderProvider 将在线账号发送器包装为图片卡密的下载、上传、发送流水线。
|
||||
type automationImageSenderProvider struct {
|
||||
// manager 按账号标识返回当前在线的 WebSocket 发送器。
|
||||
manager *accountmanager.Manager
|
||||
// uploader 负责以实际发货账号上传图片,并在需要时同步刷新后的凭证。
|
||||
uploader automationImageUploader
|
||||
// downloader 在每次发送前临时读取卡密 URL 指向的图片字节。
|
||||
downloader automationImageDownloader
|
||||
}
|
||||
|
||||
// automationImageSender 把单个账号的原始发送器与图片下载、上传能力绑定,避免自动化领域接触协议或凭证。
|
||||
type automationImageSender struct {
|
||||
// accountID 是本次自动化发货实际使用的平台账号标识。
|
||||
accountID string
|
||||
// sender 是账号运行时提供的最终 WebSocket 发送器。
|
||||
sender automation.MessageSender
|
||||
// uploader 将下载结果转换为闲鱼图片地址。
|
||||
uploader automationImageUploader
|
||||
// downloader 只在发送图片时执行远程读取。
|
||||
downloader automationImageDownloader
|
||||
}
|
||||
|
||||
// NewAutomationImageSenderProvider 创建自动化中心使用的图片发送器来源。
|
||||
// 图片 URL 不持久化为本地文件,下载和上传都限定在单次自动化动作的上下文内。
|
||||
func NewAutomationImageSenderProvider(store *db.Store, manager *accountmanager.Manager, clientProvider func() mtop.Client) automation.SenderProvider {
|
||||
// uploader 复用聊天图片上传适配器,确保凭证刷新写回和在线运行时同步保持一致。
|
||||
uploader := NewChatImageUploader(store, clientProvider, manager)
|
||||
return automationImageSenderProvider{
|
||||
manager: manager, uploader: uploader, downloader: downloadAutomationImage,
|
||||
}
|
||||
}
|
||||
|
||||
// Sender 返回指定在线账号的发送包装器;账号离线或初始化不完整时维持既有不可发送语义。
|
||||
func (p automationImageSenderProvider) Sender(accountID string) (automation.MessageSender, bool) {
|
||||
if p.manager == nil || p.uploader == nil || p.downloader == nil {
|
||||
return nil, false
|
||||
}
|
||||
// sender、ok 分别表示账号运行时的原始发送器和其在线可用状态。
|
||||
sender, ok := p.manager.GetInstance(accountID)
|
||||
if !ok || sender == nil {
|
||||
return nil, false
|
||||
}
|
||||
return automationImageSender{
|
||||
accountID: accountID, sender: sender, uploader: p.uploader, downloader: p.downloader,
|
||||
}, true
|
||||
}
|
||||
|
||||
// SendText 保持文本自动发货原有的直接 WebSocket 发送行为。
|
||||
func (s automationImageSender) SendText(ctx context.Context, chatID, toUserID, text string) error {
|
||||
if s.sender == nil {
|
||||
return fmt.Errorf("%w: 账号发送器未初始化", automation.ErrMessageNotSent)
|
||||
}
|
||||
return s.sender.SendText(ctx, chatID, toUserID, text)
|
||||
}
|
||||
|
||||
// SendImage 将图片卡密 URL 在内存中下载、上传到闲鱼图片服务后,再交给账号运行时发送。
|
||||
// 下载或上传失败发生在 WebSocket 写入之前,必须标记为确定未发送以允许自动化安全重试。
|
||||
func (s automationImageSender) SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64, _, _ int) error {
|
||||
if s.sender == nil || s.uploader == nil || s.downloader == nil {
|
||||
return fmt.Errorf("%w: 图片发送器未初始化", automation.ErrMessageNotSent)
|
||||
}
|
||||
// data、contentType、filename 和 err 分别保存远程图片的瞬时字节、媒体类型、上传名称和下载错误。
|
||||
data, contentType, filename, err := s.downloader(ctx, imageURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: 下载图片卡密失败: %v", automation.ErrMessageNotSent, err)
|
||||
}
|
||||
// uploaded、err 保存闲鱼图片服务返回的发送地址和上传阶段错误。
|
||||
uploaded, err := s.uploader.UploadChatImage(ctx, s.accountID, filename, contentType, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: 上传图片卡密失败: %v", automation.ErrMessageNotSent, err)
|
||||
}
|
||||
if strings.TrimSpace(uploaded.URL) == "" {
|
||||
return fmt.Errorf("%w: 上传图片卡密未返回地址", automation.ErrMessageNotSent)
|
||||
}
|
||||
return s.sender.SendImage(ctx, chatID, toUserID, uploaded.URL, cardID, uploaded.Width, uploaded.Height)
|
||||
}
|
||||
|
||||
// UpdateCookie 将账号运行时主动更新的 Cookie 透传给原始发送器,不改变既有凭证协调责任。
|
||||
func (s automationImageSender) UpdateCookie(cookieStr string) {
|
||||
if s.sender != nil {
|
||||
s.sender.UpdateCookie(cookieStr)
|
||||
}
|
||||
}
|
||||
|
||||
// downloadAutomationImage 从 HTTP(S) 图片 URL 读取有限大小的内存数据,拒绝内网、重定向降级和非图片响应。
|
||||
func downloadAutomationImage(ctx context.Context, rawURL string) ([]byte, string, string, error) {
|
||||
// parsed、err 分别保存规范化后的来源地址和解析错误。
|
||||
parsed, err := url.Parse(strings.TrimSpace(rawURL))
|
||||
if err != nil || parsed.Scheme == "" || parsed.Hostname() == "" || parsed.User != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") {
|
||||
return nil, "", "", fmt.Errorf("图片 URL 无效")
|
||||
}
|
||||
// request、err 保存绑定自动化取消上下文的下载请求及构造错误。
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, parsed.String(), nil)
|
||||
if err != nil {
|
||||
return nil, "", "", fmt.Errorf("创建图片下载请求失败")
|
||||
}
|
||||
// client 只允许访问公网地址,并在重定向时重复校验目标,避免卡密 URL 触发 SSRF。
|
||||
client := netguard.PublicHTTPClient(30 * time.Second)
|
||||
// response、err 保存远程图片响应及网络访问失败原因。
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, "", "", fmt.Errorf("下载图片失败")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
|
||||
return nil, "", "", fmt.Errorf("下载图片返回 HTTP %d", response.StatusCode)
|
||||
}
|
||||
// data、err 保存受最大体积限制的响应内容及读取错误。
|
||||
data, err := io.ReadAll(io.LimitReader(response.Body, automationImageMaxBytes+1))
|
||||
if err != nil {
|
||||
return nil, "", "", fmt.Errorf("读取图片失败")
|
||||
}
|
||||
if len(data) == 0 || len(data) > automationImageMaxBytes {
|
||||
return nil, "", "", fmt.Errorf("图片大小必须在 1 B 到 10 MiB 之间")
|
||||
}
|
||||
// contentType 保存去除参数后的响应媒体类型;缺失或通用类型时根据图片字节检测。
|
||||
contentType := strings.TrimSpace(strings.Split(response.Header.Get("Content-Type"), ";")[0])
|
||||
if contentType == "" || contentType == "application/octet-stream" {
|
||||
contentType = http.DetectContentType(data)
|
||||
}
|
||||
if !strings.HasPrefix(strings.ToLower(contentType), "image/") {
|
||||
return nil, "", "", fmt.Errorf("远程内容不是图片")
|
||||
}
|
||||
// filename 保存来源路径中的展示名称;缺失名称时使用与媒体类型无关的默认值。
|
||||
filename := filepath.Base(parsed.Path)
|
||||
if filename == "." || filename == "/" || filename == "" {
|
||||
filename = "image"
|
||||
}
|
||||
return data, contentType, filename, nil
|
||||
}
|
||||
|
||||
// 编译期确认自动化图片包装器保持自动化中心依赖的最小发送契约。
|
||||
var _ automation.MessageSender = automationImageSender{}
|
||||
@@ -0,0 +1,154 @@
|
||||
package adapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
chatapp "xianyu-go/internal/application/chat"
|
||||
"xianyu-go/internal/automation"
|
||||
)
|
||||
|
||||
// automationImageSenderStub 记录包装器最终交给账号运行时的发送参数。
|
||||
type automationImageSenderStub struct {
|
||||
// imageURL 保存最终写入 WebSocket 图片消息的平台地址。
|
||||
imageURL string
|
||||
// cardID 保存关联卡密组标识,确保包装不丢失自动化上下文。
|
||||
cardID int64
|
||||
// width 和 height 保存闲鱼上传返回的图片尺寸。
|
||||
width, height int
|
||||
// imageCalls 记录最终 WebSocket 图片发送次数。
|
||||
imageCalls int
|
||||
}
|
||||
|
||||
// SendText 满足自动化发送契约;本测试只验证图片链路。
|
||||
func (s *automationImageSenderStub) SendText(context.Context, string, string, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendImage 记录平台图片地址和尺寸,模拟账号运行时的 WebSocket 发送。
|
||||
func (s *automationImageSenderStub) SendImage(_ context.Context, _, _, imageURL string, cardID int64, width, height int) error {
|
||||
s.imageURL, s.cardID, s.width, s.height = imageURL, cardID, width, height
|
||||
s.imageCalls++
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateCookie 满足自动化发送契约;凭证写回由图片上传适配器自身负责。
|
||||
func (s *automationImageSenderStub) UpdateCookie(string) {}
|
||||
|
||||
// automationImageUploaderStub 记录上传输入并返回可控的平台图片结果。
|
||||
type automationImageUploaderStub struct {
|
||||
// accountID 保存上传时实际使用的发货账号。
|
||||
accountID string
|
||||
// filename 和 contentType 保存上传文件元数据。
|
||||
filename, contentType string
|
||||
// data 保存上传收到的图片字节。
|
||||
data []byte
|
||||
// result 和 err 分别控制上传成功结果与失败原因。
|
||||
result chatapp.ImageUpload
|
||||
err error
|
||||
}
|
||||
|
||||
// UploadChatImage 记录临时下载的图片并返回预设的闲鱼上传结果。
|
||||
func (u *automationImageUploaderStub) UploadChatImage(_ context.Context, accountID, filename, contentType string, data []byte) (chatapp.ImageUpload, error) {
|
||||
u.accountID, u.filename, u.contentType, u.data = accountID, filename, contentType, data
|
||||
return u.result, u.err
|
||||
}
|
||||
|
||||
// TestAutomationImageSenderUploadsURLContentBeforeSending 验证图片卡密 URL 先以内存形式下载并上传,再使用上传地址发送。
|
||||
func TestAutomationImageSenderUploadsURLContentBeforeSending(t *testing.T) {
|
||||
// sourceData 是模拟从卡密 URL 下载到的图片字节。
|
||||
sourceData := []byte("image-content")
|
||||
// sender 是记录最终 WebSocket 调用的账号运行时替身。
|
||||
sender := &automationImageSenderStub{}
|
||||
// uploader 返回闲鱼上传后的地址和实际图片尺寸。
|
||||
uploader := &automationImageUploaderStub{result: chatapp.ImageUpload{URL: "https://cdn.goofish.example/card.png", Width: 1280, Height: 720}}
|
||||
// imageSender 是待验证的自动化图片发送包装器。
|
||||
imageSender := automationImageSender{
|
||||
accountID: "account-1", sender: sender, uploader: uploader,
|
||||
downloader: func(_ context.Context, rawURL string) ([]byte, string, string, error) {
|
||||
if rawURL != "https://origin.example/card.png" {
|
||||
return nil, "", "", errors.New("unexpected source URL")
|
||||
}
|
||||
return sourceData, "image/png", "card.png", nil
|
||||
},
|
||||
}
|
||||
// err 保存完整下载、上传和发送流程的结果。
|
||||
err := imageSender.SendImage(context.Background(), "chat-1", "buyer-1", "https://origin.example/card.png", 42, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("SendImage: %v", err)
|
||||
}
|
||||
if uploader.accountID != "account-1" || uploader.filename != "card.png" || uploader.contentType != "image/png" || string(uploader.data) != string(sourceData) {
|
||||
t.Fatalf("上传参数错误: account=%q filename=%q type=%q data=%q", uploader.accountID, uploader.filename, uploader.contentType, uploader.data)
|
||||
}
|
||||
if sender.imageCalls != 1 || sender.imageURL != uploader.result.URL || sender.cardID != 42 || sender.width != 1280 || sender.height != 720 {
|
||||
t.Fatalf("发送参数错误: calls=%d url=%q card=%d size=%dx%d", sender.imageCalls, sender.imageURL, sender.cardID, sender.width, sender.height)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationImageSenderFailsBeforeWebSocketForPreparationErrors 验证下载、上传和取消失败都不会写入 WebSocket,并保持可安全重试的错误语义。
|
||||
func TestAutomationImageSenderFailsBeforeWebSocketForPreparationErrors(t *testing.T) {
|
||||
// cases 覆盖图片发送前的确定性准备失败场景。
|
||||
cases := []struct {
|
||||
// name 是子测试名称。
|
||||
name string
|
||||
// downloader 控制远程图片读取结果。
|
||||
downloader automationImageDownloader
|
||||
// uploaderError 控制闲鱼图片上传失败。
|
||||
uploaderError error
|
||||
}{
|
||||
{
|
||||
name: "download-failure",
|
||||
downloader: func(context.Context, string) ([]byte, string, string, error) {
|
||||
return nil, "", "", errors.New("source unavailable")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "upload-failure",
|
||||
downloader: func(context.Context, string) ([]byte, string, string, error) {
|
||||
return []byte("image"), "image/png", "card.png", nil
|
||||
},
|
||||
uploaderError: errors.New("upload rejected"),
|
||||
},
|
||||
{
|
||||
name: "cancelled-download",
|
||||
downloader: func(ctx context.Context, _ string) ([]byte, string, string, error) {
|
||||
return nil, "", "", ctx.Err()
|
||||
},
|
||||
},
|
||||
}
|
||||
// testCase 表示当前准备失败场景。
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
// sender 是确保准备失败时不会调用 WebSocket 的替身。
|
||||
sender := &automationImageSenderStub{}
|
||||
// uploader 是返回当前场景上传结果的替身。
|
||||
uploader := &automationImageUploaderStub{err: testCase.uploaderError}
|
||||
// imageSender 是当前失败场景使用的图片发送包装器。
|
||||
imageSender := automationImageSender{accountID: "account-1", sender: sender, uploader: uploader, downloader: testCase.downloader}
|
||||
// ctx、cancel 为取消场景提供已经结束的上下文。
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
if testCase.name == "cancelled-download" {
|
||||
cancel()
|
||||
} else {
|
||||
defer cancel()
|
||||
}
|
||||
// err 保存准备阶段失败返回的自动化错误。
|
||||
err := imageSender.SendImage(ctx, "chat-1", "buyer-1", "https://origin.example/card.png", 1, 0, 0)
|
||||
if !errors.Is(err, automation.ErrMessageNotSent) || sender.imageCalls != 0 {
|
||||
t.Fatalf("准备失败必须安全重试且不得发送: calls=%d err=%v", sender.imageCalls, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDownloadAutomationImageRejectsNonHTTPURL 验证图片卡密下载入口拒绝非 HTTP(S) 地址,不创建本地或网络副作用。
|
||||
func TestDownloadAutomationImageRejectsNonHTTPURL(t *testing.T) {
|
||||
// rawURL 表示当前非法图片来源地址。
|
||||
for _, rawURL := range []string{"", "file:///tmp/card.png", "ftp://example.com/card.png", "https://user:[email protected]/card.png"} {
|
||||
// _, _, _, err 丢弃非法 URL 的空结果,只验证校验错误。
|
||||
if _, _, _, err := downloadAutomationImage(context.Background(), rawURL); err == nil {
|
||||
t.Fatalf("非法图片 URL 未被拒绝: %q", rawURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user