Skip to content

Commit cf01ff6

Browse files
Sky_slienceSky_slience
authored andcommitted
feat(thumbnail): add configurable thumbnail size setting and update thumbnail generation
1 parent f3d6230 commit cf01ff6

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

drivers/local/driver.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/alist-org/alist/v3/internal/driver"
1919
"github.com/alist-org/alist/v3/internal/errs"
2020
"github.com/alist-org/alist/v3/internal/model"
21+
"github.com/alist-org/alist/v3/internal/op"
2122
"github.com/alist-org/alist/v3/internal/sign"
2223
"github.com/alist-org/alist/v3/pkg/utils"
2324
"github.com/alist-org/alist/v3/server/common"
@@ -31,6 +32,7 @@ type Local struct {
3132
model.Storage
3233
Addition
3334
mkdirPerm int32
35+
thumbSize int
3436

3537
// zero means no limit
3638
thumbConcurrency int
@@ -71,6 +73,17 @@ func (d *Local) Init(ctx context.Context) error {
7173
return err
7274
}
7375
}
76+
d.thumbSize = 144
77+
if item, err := op.GetSettingItemByKey(conf.ThumbnailSize); err == nil && item != nil && strings.TrimSpace(item.Value) != "" {
78+
v, err := strconv.ParseUint(item.Value, 10, 32)
79+
if err != nil {
80+
return fmt.Errorf("invalid setting %s value: %s, err: %s", conf.ThumbnailSize, item.Value, err)
81+
}
82+
if v == 0 {
83+
return fmt.Errorf("invalid setting %s value: %s, the value must be a positive integer", conf.ThumbnailSize, item.Value)
84+
}
85+
d.thumbSize = int(v)
86+
}
7487
if d.ThumbConcurrency != "" {
7588
v, err := strconv.ParseUint(d.ThumbConcurrency, 10, 32)
7689
if err != nil {

drivers/local/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func readDir(dirname string) ([]fs.FileInfo, error) {
111111
func (d *Local) getThumb(file model.Obj) (*bytes.Buffer, *string, error) {
112112
fullPath := file.GetPath()
113113
thumbPrefix := "alist_thumb_"
114-
thumbName := thumbPrefix + utils.GetMD5EncodeStr(fullPath) + ".png"
114+
thumbName := thumbPrefix + utils.GetMD5EncodeStr(fmt.Sprintf("%s:%d", fullPath, d.thumbSize)) + ".png"
115115
if d.ThumbCacheFolder != "" {
116116
// skip if the file is a thumbnail
117117
if strings.HasPrefix(file.GetName(), thumbPrefix) {
@@ -142,7 +142,7 @@ func (d *Local) getThumb(file model.Obj) (*bytes.Buffer, *string, error) {
142142
if err != nil {
143143
return nil, nil, err
144144
}
145-
thumbImg := imaging.Resize(image, 144, 0, imaging.Lanczos)
145+
thumbImg := imaging.Resize(image, d.thumbSize, 0, imaging.Lanczos)
146146
var buf bytes.Buffer
147147
err = imaging.Encode(&buf, thumbImg, imaging.PNG)
148148
if err != nil {

internal/bootstrap/data/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func InitialSettings() []model.SettingItem {
146146
{Key: "audio_cover", Value: "https://jsd.nn.ci/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.PREVIEW},
147147
{Key: conf.AudioAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
148148
{Key: conf.VideoAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
149+
{Key: conf.ThumbnailSize, Value: "144", Type: conf.TypeNumber, Group: model.PREVIEW, Help: "Thumbnail width in pixels. Height is scaled proportionally."},
149150
{Key: conf.PreviewArchivesByDefault, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
150151
{Key: conf.ReadMeAutoRender, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
151152
{Key: conf.FilterReadMeScripts, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},

internal/conf/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
ProxyIgnoreHeaders = "proxy_ignore_headers"
3434
AudioAutoplay = "audio_autoplay"
3535
VideoAutoplay = "video_autoplay"
36+
ThumbnailSize = "thumbnail_size"
3637
PreviewArchivesByDefault = "preview_archives_by_default"
3738
ReadMeAutoRender = "readme_autorender"
3839
FilterReadMeScripts = "filter_readme_scripts"

0 commit comments

Comments
 (0)