Skip to content

Commit

Permalink
feat: return the total cover and page count
Browse files Browse the repository at this point in the history
  • Loading branch information
CrescentKohana committed Jan 8, 2024
1 parent 2f748b4 commit 5c08a68
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pkg/cache/processing_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ type scanResult struct {

type thumbnailResult struct {
Running bool
TotalCovers int
TotalPages int
GeneratedCovers int
GeneratedPages int
Errors []processingError
}

type metadataResult struct {
// TODO: more information like progress and which sources are being used
Running bool
Errors []processingError
}
Expand Down Expand Up @@ -78,6 +81,11 @@ func (s *ProcessingStatus) SetThumbnailsRunning(running bool) {
s.Thumbnails.Running = running
}

func (s *ProcessingStatus) SetTotalCoversAndPages(coverCount int, pageCount int) {
ProcessingStatusCache.Thumbnails.TotalCovers = coverCount
ProcessingStatusCache.Thumbnails.TotalPages = pageCount
}

func (s *ProcessingStatus) AddThumbnailGeneratedCover() {
s.Thumbnails.GeneratedCovers++
}
Expand Down Expand Up @@ -116,6 +124,8 @@ func (s *ProcessingStatus) Reset() {
}
s.Thumbnails = thumbnailResult{
Running: false,
TotalCovers: 0,
TotalPages: 0,
GeneratedCovers: 0,
GeneratedPages: 0,
Errors: make([]processingError, 0),
Expand Down
21 changes: 21 additions & 0 deletions pkg/db/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,24 @@ func DeleteGallery(galleryUUID string) bool {
rowsAffected, _ := res.RowsAffected()
return rowsAffected > int64(0)
}

func CountAllImages(skipWithPageThumbnails bool) (int, int, error) {
stmt := SELECT(COUNT(Gallery.UUID).AS("CoverCount"), SUM(Gallery.ImageCount).AS("ImageCount")).
FROM(Gallery.Table)

if skipWithPageThumbnails {
stmt = stmt.WHERE(Gallery.PageThumbnails.GT(Int32(0)))
}

var counts struct {
CoverCount int
ImageCount int
}

err := stmt.Query(db(), &counts)
if err != nil {
return 0, 0, err
}

return counts.CoverCount, counts.ImageCount, nil
}
20 changes: 13 additions & 7 deletions pkg/library/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"image"
"io/fs"
"os"
"path"
"strings"
"sync"

"github.com/Mangatsu/server/internal/config"
"github.com/Mangatsu/server/pkg/cache"
"github.com/Mangatsu/server/pkg/constants"
Expand All @@ -21,13 +14,26 @@ import (
"github.com/disintegration/imaging"
"github.com/mholt/archiver/v4"
"go.uber.org/zap"
"image"
"io/fs"
"os"
"path"
"strings"
"sync"
)

// GenerateThumbnails generates thumbnails for covers and pages in parallel. // TODO: ignore generated files or rewrite existing cache option
func GenerateThumbnails(pages bool, force bool) {
var wg sync.WaitGroup

cache.ProcessingStatusCache.SetThumbnailsRunning(true)
coverCount, pageCount, err := db.CountAllImages(false)
if err != nil {
log.Z.Error("could not get image count for thumbnail generation", zap.String("err", err.Error()))
} else {
log.Z.Info("thumbnail generation started", zap.Int("covers", coverCount), zap.Int("pages", pageCount))
cache.ProcessingStatusCache.SetTotalCoversAndPages(coverCount, pageCount)
}

wg.Add(1)
go func() {
Expand Down

0 comments on commit 5c08a68

Please sign in to comment.