Skip to content

Commit

Permalink
chore: update thumbnail generator
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 3, 2024
1 parent 09586d0 commit 773ab96
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions server/router/api/v1/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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}\}`)
Expand Down

0 comments on commit 773ab96

Please sign in to comment.