-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(crypt): 支持按需生成加密存储的图片缩略图 #2317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| package crypt | ||
|
|
||
| import "github.com/OpenListTeam/OpenList/v4/internal/model" | ||
|
|
||
| type thumbObject struct { | ||
| model.Object | ||
| thumbPath string | ||
| sourceObj model.Obj | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,29 @@ | ||
| package crypt | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| stdpath "path" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/OpenListTeam/OpenList/v4/internal/conf" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/model" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/op" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/stream" | ||
| "github.com/OpenListTeam/OpenList/v4/pkg/http_range" | ||
| "github.com/OpenListTeam/OpenList/v4/pkg/utils" | ||
| "github.com/disintegration/imaging" | ||
| ffmpeg "github.com/u2takey/ffmpeg-go" | ||
| ) | ||
|
|
||
| const ( | ||
| thumbDirName = ".thumbnails" | ||
| thumbExt = ".webp" | ||
| thumbWidth = 144 | ||
| ) | ||
|
|
||
| // will give the best guessing based on the path | ||
|
|
@@ -27,3 +47,134 @@ func (d *Crypt) encryptPath(path string, isFolder bool) string { | |
| dir, fileName := filepath.Split(path) | ||
| return stdpath.Join(d.cipher.EncryptDirName(dir), d.cipher.EncryptFileName(fileName)) | ||
| } | ||
|
|
||
| func isThumbPath(path string) bool { | ||
| path = utils.FixAndCleanPath(path) | ||
| return stdpath.Base(stdpath.Dir(path)) == thumbDirName && strings.HasSuffix(stdpath.Base(path), thumbExt) | ||
| } | ||
|
|
||
| func thumbSourcePath(path string) (string, bool) { | ||
| path = utils.FixAndCleanPath(path) | ||
| if !isThumbPath(path) { | ||
| return "", false | ||
| } | ||
| name := strings.TrimSuffix(stdpath.Base(path), thumbExt) | ||
| if name == "" { | ||
| return "", false | ||
| } | ||
| parentDir := stdpath.Dir(stdpath.Dir(path)) | ||
| return stdpath.Join(parentDir, name), true | ||
| } | ||
|
|
||
| func thumbTargetDir(path string) string { | ||
| return stdpath.Dir(utils.FixAndCleanPath(path)) | ||
| } | ||
|
|
||
| func (d *Crypt) newThumbObject(path string, sourceObj model.Obj) *thumbObject { | ||
| path = utils.FixAndCleanPath(path) | ||
| return &thumbObject{ | ||
| Object: model.Object{ | ||
| Path: stdpath.Join(d.RemotePath, d.encryptPath(path, false)), | ||
| Name: stdpath.Base(path), | ||
| Modified: sourceObj.ModTime(), | ||
| Ctime: sourceObj.CreateTime(), | ||
| }, | ||
| thumbPath: path, | ||
| sourceObj: sourceObj, | ||
| } | ||
| } | ||
|
|
||
| func (d *Crypt) ensureThumb(ctx context.Context, thumb *thumbObject) error { | ||
| if _, err := d.getActual(ctx, thumb.thumbPath); err == nil { | ||
| return nil | ||
| } | ||
| _, err, _ := d.thumbGroup.Do(thumb.thumbPath, func() (struct{}, error) { | ||
| if _, err := d.getActual(ctx, thumb.thumbPath); err == nil { | ||
| return struct{}{}, nil | ||
| } | ||
| buf, err := d.buildThumb(ctx, thumb.sourceObj) | ||
| if err != nil { | ||
| return struct{}{}, err | ||
| } | ||
| 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 | ||
| } | ||
|
Comment on lines
+99
to
+110
|
||
| if _, err := d.getActual(ctx, thumb.thumbPath); err != nil { | ||
| return struct{}{}, err | ||
| } | ||
| return struct{}{}, nil | ||
| }) | ||
| return err | ||
| } | ||
|
|
||
| func (d *Crypt) buildThumb(ctx context.Context, sourceObj model.Obj) (*bytes.Buffer, error) { | ||
| sourceFile, err := d.openSourceTempFile(ctx, sourceObj) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { | ||
| _ = sourceFile.Close() | ||
| _ = os.Remove(sourceFile.Name()) | ||
| }() | ||
|
|
||
| image, err := imaging.Decode(sourceFile, imaging.AutoOrientation(true)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| thumbImg := imaging.Resize(image, thumbWidth, 0, imaging.Lanczos) | ||
|
|
||
| tmpPNG, err := os.CreateTemp(conf.Conf.TempDir, "crypt-thumb-*.png") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| tmpPNGPath := tmpPNG.Name() | ||
| defer func() { | ||
| _ = tmpPNG.Close() | ||
| _ = os.Remove(tmpPNGPath) | ||
| }() | ||
| if err := imaging.Encode(tmpPNG, thumbImg, imaging.PNG); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := tmpPNG.Close(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| buf := bytes.NewBuffer(nil) | ||
| cmd := ffmpeg.Input(tmpPNGPath). | ||
| Output("pipe:", ffmpeg.KwArgs{"vcodec": "libwebp", "f": "webp"}). | ||
| GlobalArgs("-loglevel", "error"). | ||
| Silent(true). | ||
| WithOutput(buf, io.Discard) | ||
| if err := cmd.Run(); err != nil { | ||
| return nil, fmt.Errorf("encode webp failed: %w", err) | ||
| } | ||
| if buf.Len() == 0 { | ||
| return nil, fmt.Errorf("encode webp failed: empty output") | ||
| } | ||
| return buf, nil | ||
| } | ||
|
|
||
| func (d *Crypt) openSourceTempFile(ctx context.Context, sourceObj model.Obj) (*os.File, error) { | ||
| link, err := d.Link(ctx, sourceObj, model.LinkArgs{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer link.Close() | ||
|
|
||
| reader, err := link.RangeReader.RangeRead(ctx, http_range.Range{Length: -1}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer reader.Close() | ||
|
|
||
| return utils.CreateTempFile(reader, sourceObj.GetSize()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package crypt | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestIsThumbPath(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cases := []struct { | ||
| path string | ||
| want bool | ||
| }{ | ||
| {path: "/photos/.thumbnails/cat.jpg.webp", want: true}, | ||
| {path: "/.thumbnails/cat.jpg.webp", want: true}, | ||
| {path: "/photos/cat.jpg.webp", want: false}, | ||
| {path: "/photos/.thumbnails/cat.jpg.png", want: false}, | ||
| {path: "/photos/.thumbnails/", want: false}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| if got := isThumbPath(tc.path); got != tc.want { | ||
| t.Fatalf("isThumbPath(%q) = %v, want %v", tc.path, got, tc.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestThumbSourcePath(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cases := []struct { | ||
| path string | ||
| want string | ||
| ok bool | ||
| }{ | ||
| {path: "/photos/.thumbnails/cat.jpg.webp", want: "/photos/cat.jpg", ok: true}, | ||
| {path: "/.thumbnails/cat.jpg.webp", want: "/cat.jpg", ok: true}, | ||
| {path: "/photos/.thumbnails/cat.jpg.png", ok: false}, | ||
| {path: "/photos/cat.jpg.webp", ok: false}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| got, ok := thumbSourcePath(tc.path) | ||
| if ok != tc.ok { | ||
| t.Fatalf("thumbSourcePath(%q) ok = %v, want %v", tc.path, ok, tc.ok) | ||
| } | ||
| if got != tc.want { | ||
| t.Fatalf("thumbSourcePath(%q) = %q, want %q", tc.path, got, tc.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestThumbTargetDir(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| if got := thumbTargetDir("/photos/.thumbnails/cat.jpg.webp"); got != "/photos/.thumbnails" { | ||
| t.Fatalf("thumbTargetDir() = %q, want %q", got, "/photos/.thumbnails") | ||
| } | ||
| } |
There was a problem hiding this comment.
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
Thumbnailsetting in List, but Get will still synthesize a thumbObject (and Link will generate thumbnails) whenever a.thumbnails/*.webppath is accessed. This makes the feature effectively enabled via direct access even whenThumbnailis false. Consider checkingd.Thumbnailbefore entering the thumbnail fallback path in Get.