Files
zt-closet-backend/.codebuddy/plans/调整_COS_图片存储路径结构_5eea0fe3.md
T
2026-09-11 01:41:52 +08:00

61 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
name: 调整 COS 图片存储路径结构
overview: 将 COS 图片对象键从 {userID}/{uuid.png} 改为 {衣物UUID}/{userID}/{uuid.png}(uuid/userid/图片 三层结构),同步调整临时文件保存目录保持一致。
todos:
- id: adjust-cos-path
content: 修改 upload.go 的 FileSaveTmpBytes 签名(userID 改为 subDir)与 clothes.go 的 processImageAsync(subDir/cosKey 三层结构)
status: completed
- id: verify-build
content: 执行 go build ./... 验证编译通过
status: completed
dependencies:
- adjust-cos-path
---
## 用户需求
修改 COS 图片的存储位置,将当前的对象键与本地临时目录结构从 `{userID}/{uuid.png}` 两层改为 `{衣物UUID}/{userID}/{uuid.png}` 三层(即用户表述的「uuid/userid/图片」)。
## 核心变更
- 抠图结果图片在 COS 上的对象键、以及后端本地临时文件目录,统一调整为 `{衣物UUID}/{userID}/{图片文件名}`。
- 衣物详情返回的 `processed_image` 字段仍存该 COS 对象键,前端获取图片 URL 的链路不变。
## 影响范围
- 仅涉及图片上传落盘与 COS 对象键拼接,不影响上传/抠图/分类/额度等其它业务逻辑。
## 技术方案
### 现状
- `internal/service/clothes.go` 的 `processImageAsync`(约 137-145 行)当前生成:
- `processedName := genUUID() + ".png"`
- `cosKey := fmt.Sprintf("%d/%s", userID, processedName)`,即 `{userID}/{uuid.png}`。
- `pkg/upload/upload.go` 的 `FileSaveTmpBytes(ctx, data, fileName, userID)` 当前把临时文件保存到 `TmpSavePath/{userID}/{fileName}`。
- `FileUploadCos(ctx, cosKey)` 以同一个 `cosKey` 作为本地相对路径与 COS 对象键,因此只要 `cosKey` 结构变了,本地读取与 COS 上传会同步适配。
### 修改点
#### 1. `pkg/upload/upload.go` — `FileSaveTmpBytes`
- 将第 4 个参数 `userID int64` 改为 `subDir string`。
- 目录拼接由 `filepath.Join(TmpSavePath, fmt.Sprint(userID))` 改为 `filepath.Join(TmpSavePath, subDir)`。
- 函数职责从「按 userID 建目录」泛化为「按调用方传入的子目录建目录」,保持通用。
#### 2. `internal/service/clothes.go` — `processImageAsync`
- 新增 `subDir := fmt.Sprintf("%s/%d", clothesUUID, userID)`。
- 保存调用改为 `upload.FileSaveTmpBytes(ctx, processedData, processedName, subDir)`。
- `cosKey := fmt.Sprintf("%s/%s", subDir, processedName)`,最终结构为 `{clothesUUID}/{userID}/{uuid.png}`。
- 同步更新该处「对象键含 userID 前缀」的注释,改为三层结构说明。
### 关键设计
- `FileUploadCos` 与 `GetCosUrl` 的签名、逻辑均不变,它们以 `cosKey` 为输入自动适配新结构,改动面最小。
- `FileSaveTmpBytes` 仅 `clothes.go` 一处调用点,修改签名无连锁影响。
### 注意事项
- 旧数据兼容:历史 `processed_image` 中存的是旧两层 key,新代码下 `GetCosUrl` 会拼出 COS 上不存在的对象路径。联调阶段建议清库重传;如需兼容旧数据,需另做数据迁移,本次不处理。
- 验证:改动后执行 `go build ./...` 确保编译通过。