Skip to content

feat(crypt): 支持按需生成加密存储的图片缩略图#2317

Open
ShawnRZ wants to merge 1 commit intoOpenListTeam:mainfrom
ShawnRZ:crypt/auto-gen-thum
Open

feat(crypt): 支持按需生成加密存储的图片缩略图#2317
ShawnRZ wants to merge 1 commit intoOpenListTeam:mainfrom
ShawnRZ:crypt/auto-gen-thum

Conversation

@ShawnRZ
Copy link
Copy Markdown

@ShawnRZ ShawnRZ commented Apr 6, 2026

Description / 描述

  • 新增缩略图相关类型定义和工具函数
  • 修改配置说明,明确缩略图按需生成特性
  • 实现缩略图检测、路径转换和生成逻辑
  • 添加缩略图生成时的单飞机制避免重复生成
  • 增加缩略图相关的单元测试

Motivation and Context / 背景

现在crypt无法自动生成缩略图,新增一个特性使得crypt可以像local一样自动生成缩略图

How Has This Been Tested? / 测试

启动一个装有ffmpeg的环境,在crypt中上传一张图片,访问时缩略图自动生成。

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it’s needed).
    我已相应更新了相关仓库(若适用)。

- 新增缩略图相关类型定义和工具函数
- 修改配置说明,明确缩略图按需生成特性
- 实现缩略图检测、路径转换和生成逻辑
- 添加缩略图生成时的单飞机制避免重复生成
- 增加缩略图相关的单元测试
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 为 crypt 存储驱动补齐了类似 local 的缩略图体验:当访问 .thumbnails/*.webp 时,如果缩略图不存在则自动生成并写回存储,从而实现“按需生成”。

Changes:

  • 新增缩略图路径识别、源/目标路径转换、缩略图对象与生成工具函数(含单飞机制)。
  • 调整 cryptGet/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.

Comment on lines +99 to +110
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
}
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +237 to +253
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
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants