修复不同类型图片提交的情况下排序的问题 (#294)

* 修复不同类型图片提交的情况下排序的问题

* fix:去掉索引,按顺序检查图片下载然后加入新的数组
This commit is contained in:
flippancy
2025-11-16 23:57:47 +08:00
committed by GitHub
parent 7dc1a731c7
commit 2f8aa1d7ee

View File

@@ -22,29 +22,25 @@ func NewImageProcessor() *ImageProcessor {
// 支持两种输入格式: // 支持两种输入格式:
// 1. URL格式 (http/https开头) - 自动下载到本地 // 1. URL格式 (http/https开头) - 自动下载到本地
// 2. 本地文件路径 - 直接使用 // 2. 本地文件路径 - 直接使用
// 保持原始图片顺序,如果下载失败直接返回错误
func (p *ImageProcessor) ProcessImages(images []string) ([]string, error) { func (p *ImageProcessor) ProcessImages(images []string) ([]string, error) {
var localPaths []string localPaths := make([]string, 0, len(images))
var urlsToDownload []string
// 分离URL和本地路径 // 按顺序处理每张图片
for _, image := range images { for _, image := range images {
if IsImageURL(image) { if IsImageURL(image) {
urlsToDownload = append(urlsToDownload, image) // URL图片立即下载失败直接返回错误
localPath, err := p.downloader.DownloadImage(image)
if err != nil {
return nil, fmt.Errorf("下载图片失败 %s: %w", image, err)
}
localPaths = append(localPaths, localPath)
} else { } else {
// 本地路径直接添加 // 本地路径直接使用
localPaths = append(localPaths, image) localPaths = append(localPaths, image)
} }
} }
// 批量下载URL图片
if len(urlsToDownload) > 0 {
downloadedPaths, err := p.downloader.DownloadImages(urlsToDownload)
if err != nil {
return nil, fmt.Errorf("failed to download images: %w", err)
}
localPaths = append(localPaths, downloadedPaths...)
}
if len(localPaths) == 0 { if len(localPaths) == 0 {
return nil, fmt.Errorf("no valid images found") return nil, fmt.Errorf("no valid images found")
} }