Conversation
- 新增缩略图相关类型定义和工具函数 - 修改配置说明,明确缩略图按需生成特性 - 实现缩略图检测、路径转换和生成逻辑 - 添加缩略图生成时的单飞机制避免重复生成 - 增加缩略图相关的单元测试
There was a problem hiding this comment.
Pull request overview
该 PR 为 crypt 存储驱动补齐了类似 local 的缩略图体验:当访问 .thumbnails/*.webp 时,如果缩略图不存在则自动生成并写回存储,从而实现“按需生成”。
Changes:
- 新增缩略图路径识别、源/目标路径转换、缩略图对象与生成工具函数(含单飞机制)。
- 调整
crypt的Get/Link/List逻辑:对图片对象提供缩略图 URL,并在访问缺失缩略图时触发生成。 - 增加缩略图路径相关的单元测试,并更新配置项说明。
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| drivers/crypt/util.go | 新增缩略图路径判断/转换与生成、上传写回逻辑(含 singleflight 防重复生成) |
| drivers/crypt/util_test.go | 新增缩略图路径相关测试用例 |
| drivers/crypt/types.go | 新增用于“虚拟缩略图”的 thumbObject 类型 |
| drivers/crypt/meta.go | 更新 Thumbnail 配置项 help 文案,强调按需生成 |
| drivers/crypt/driver.go | 修改 List 仅对图片附加缩略图;Get/Link 支持缺失缩略图时返回虚拟对象并在 Link 时生成 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| file := &stream.FileStream{ | ||
| Obj: &model.Object{ | ||
| Name: stdpath.Base(thumb.thumbPath), | ||
| Size: int64(buf.Len()), | ||
| Modified: thumb.sourceObj.ModTime(), | ||
| }, | ||
| Reader: bytes.NewReader(buf.Bytes()), | ||
| Mimetype: "image/webp", | ||
| } | ||
| if err := op.Put(ctx, d, thumbTargetDir(thumb.thumbPath), file, nil); err != nil { | ||
| return struct{}{}, err | ||
| } |
There was a problem hiding this comment.
In ensureThumb, op.Put is called with storage = d and dstPath = /.../.thumbnails/<name>.webp. Because Crypt.Get synthesizes a thumbObject for missing thumbnail paths, op.Put’s preflight GetUnwrap(dstPath) will incorrectly report the destination file exists with size=0 and then try to delete/rename it, which can fail (e.g., underlying driver Remove on non-existent file) and prevent thumbnail generation. To avoid this, bypass op.Put’s existence-check path for thumbnail writes (e.g., ensure the target dir exists, then call Crypt.Put directly), or adjust the thumbnail-virtualization so internal writes don’t make dstPath look like an existing zero-length file.
| func (d *Crypt) Get(ctx context.Context, path string) (model.Obj, error) { | ||
| obj, err := d.getActual(ctx, path) | ||
| if err == nil { | ||
| return obj, nil | ||
| } | ||
| if !errs.IsObjectNotFound(err) || !isThumbPath(path) { | ||
| return nil, err | ||
| } | ||
| sourcePath, ok := thumbSourcePath(path) | ||
| if !ok { | ||
| return nil, err | ||
| } | ||
| sourceObj, sourceErr := op.Get(ctx, d, sourcePath) | ||
| if sourceErr != nil || sourceObj.IsDir() || utils.GetFileType(sourceObj.GetName()) != conf.IMAGE { | ||
| return nil, err | ||
| } | ||
| return d.newThumbObject(path, sourceObj), nil |
There was a problem hiding this comment.
Thumbnail auto-generation is gated by the Thumbnail setting in List, but Get will still synthesize a thumbObject (and Link will generate thumbnails) whenever a .thumbnails/*.webp path is accessed. This makes the feature effectively enabled via direct access even when Thumbnail is false. Consider checking d.Thumbnail before entering the thumbnail fallback path in Get.
Description / 描述
Motivation and Context / 背景
现在crypt无法自动生成缩略图,新增一个特性使得crypt可以像local一样自动生成缩略图
How Has This Been Tested? / 测试
启动一个装有ffmpeg的环境,在crypt中上传一张图片,访问时缩略图自动生成。
Checklist / 检查清单
我已阅读 CONTRIBUTING 文档。
go fmtor prettier.我已使用
go fmt或 prettier 格式化提交的代码。我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
我已相应更新了相关仓库(若适用)。