MCPcopy Create free account
hub / github.com/53AI/53AIHub / CacheBotIconWithUploadFile

Method CacheBotIconWithUploadFile

api/service/coze_provider.go:367–439  ·  view source on GitHub ↗

CacheBotIconWithUploadFile 使用UploadFile表缓存bot图标

(botID string, iconURL string, eid int64)

Source from the content-addressed store, hash-verified

365
366// CacheBotIconWithUploadFile 使用UploadFile表缓存bot图标
367func (ser *CozeService) CacheBotIconWithUploadFile(botID string, iconURL string, eid int64) (string, error) {
368 // 清理URL,只保留?之前的部分(去除查询参数)
369 cleanedIconURL := iconURL
370 if idx := strings.Index(iconURL, "?"); idx != -1 {
371 cleanedIconURL = iconURL[:idx]
372 }
373
374 // 计算清理后URL的哈希值
375 urlHash := fmt.Sprintf("%x", md5.Sum([]byte(cleanedIconURL)))
376
377 // 检查UploadFile表中是否已存在该URL的记录
378 existingFile := ser.findUploadFileByHash(urlHash)
379 if existingFile != nil {
380 // 已存在,直接返回预览URL
381 return existingFile.GetPreviewFullUrl(), nil
382 }
383
384 // 不存在,需要下载并保存图标
385 var iconData []byte
386 var err error
387 for i := 0; i < 3; i++ {
388 iconData, err = ser.downloadIcon(iconURL)
389 if err == nil {
390 break
391 }
392 logger.SysLogf("下载图标失败,第 %d 次重试: %v", i+1, err)
393 time.Sleep(200 * time.Millisecond)
394 }
395 if err != nil {
396 return "", fmt.Errorf("下载图标失败: %w", err)
397 }
398
399 // 获取文件扩展名
400 ext := path.Ext(cleanedIconURL)
401 if ext == "" {
402 // 尝试从Content-Type获取扩展名
403 // 这里简化处理,直接默认使用png
404 ext = ".png"
405 }
406
407 // 生成previewKey
408 previewKey, err := db_model.GetPreviewKey(urlHash, ext, eid)
409 if err != nil {
410 return "", fmt.Errorf("生成预览键失败: %w", err)
411 }
412
413 // 保存新文件到存储系统
414 fileKey := db_model.GetFileKey(fmt.Sprintf("coze_bot_%s_icon%s", botID, ext), 0, 0)
415 if err := storage.StorageInstance.Save(iconData, fileKey); err != nil {
416 return "", fmt.Errorf("保存图标文件失败: %w", err)
417 }
418
419 // 创建UploadFile记录
420 uploadFile := &db_model.UploadFile{
421 FileName: fmt.Sprintf("coze_bot_%s_icon%s", botID, ext),
422 Key: fileKey,
423 Eid: eid,
424 UserID: 0, // 系统文件

Callers 1

GetCozeAllBotsFunction · 0.95

Calls 7

findUploadFileByHashMethod · 0.95
GetPreviewFullUrlMethod · 0.95
downloadIconMethod · 0.95
SaveMethod · 0.95
ErrorfMethod · 0.80
SaveMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected