From 773ab96bd00d7374526e5ddea47942bfdde90fd8 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 3 Sep 2024 22:09:02 +0800 Subject: [PATCH] chore: update thumbnail generator --- server/router/api/v1/resource_service.go | 37 ++++++++++-------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/server/router/api/v1/resource_service.go b/server/router/api/v1/resource_service.go index bca5360a..36d34ba3 100644 --- a/server/router/api/v1/resource_service.go +++ b/server/router/api/v1/resource_service.go @@ -5,14 +5,12 @@ import ( "context" "encoding/binary" "fmt" - "image" "io" "log/slog" "os" "path/filepath" "regexp" "strings" - "sync/atomic" "time" "github.com/disintegration/imaging" @@ -409,6 +407,11 @@ func (s *APIV1Service) GetResourceBlob(resource *store.Resource) ([]byte, error) return blob, nil } +const ( + // thumbnailMaxWidth is the maximum width of the thumbnail image. + thumbnailMaxWidth = 700 +) + // getOrGenerateThumbnail returns the thumbnail image of the resource. func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte, error) { thumbnailCacheFolder := filepath.Join(s.Profile.Data, ThumbnailCacheFolder) @@ -421,16 +424,7 @@ func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte, return nil, errors.Wrap(err, "failed to check thumbnail image stat") } - var availableGeneratorAmount int32 = 32 - if atomic.LoadInt32(&availableGeneratorAmount) <= 0 { - return nil, errors.New("not enough available generator amount") - } - atomic.AddInt32(&availableGeneratorAmount, -1) - defer func() { - atomic.AddInt32(&availableGeneratorAmount, 1) - }() - - // Otherwise, generate and save the thumbnail image. + // If thumbnail image does not exist, generate and save the thumbnail image. blob, err := s.GetResourceBlob(resource) if err != nil { return nil, errors.Wrap(err, "failed to get resource blob") @@ -440,29 +434,28 @@ func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte, return nil, errors.Wrap(err, "failed to decode thumbnail image") } - thumbnailMaxWidth := 700 // equal to home/explore screen image max width - var thumbnailImage image.Image - if img.Bounds().Max.X > thumbnailMaxWidth { - thumbnailImage = imaging.Resize(img, thumbnailMaxWidth, 0, imaging.Lanczos) - } else { - thumbnailImage = img + // If the image is smaller than the thumbnailMaxWidth, return the original image. + if img.Bounds().Max.X < thumbnailMaxWidth { + return blob, nil } + // Resize the image to the thumbnailMaxWidth. + thumbnailImage := imaging.Resize(img, thumbnailMaxWidth, 0, imaging.Lanczos) if err := imaging.Save(thumbnailImage, filePath); err != nil { return nil, errors.Wrap(err, "failed to save thumbnail file") } } - dstFile, err := os.Open(filePath) + thumbnailFile, err := os.Open(filePath) if err != nil { return nil, errors.Wrap(err, "failed to open thumbnail file") } - defer dstFile.Close() - dstBlob, err := io.ReadAll(dstFile) + defer thumbnailFile.Close() + blob, err := io.ReadAll(thumbnailFile) if err != nil { return nil, errors.Wrap(err, "failed to read thumbnail file") } - return dstBlob, nil + return blob, nil } var fileKeyPattern = regexp.MustCompile(`\{[a-z]{1,9}\}`)