aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/ext/middleware.go4
-rw-r--r--pkg/fileop/file.go5
-rw-r--r--pkg/fileop/thumbnail.go49
-rw-r--r--pkg/worker/list_processor.go13
-rw-r--r--pkg/worker/thumbnail_scanner.go7
5 files changed, 46 insertions, 32 deletions
diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go
index d255c6d..c83b998 100644
--- a/pkg/ext/middleware.go
+++ b/pkg/ext/middleware.go
@@ -12,7 +12,9 @@ import (
func HTML(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
- ctx.Response.Header.SetContentType("text/html")
+ if len(ctx.Request.Header.ContentType()) > 0 {
+ ctx.Response.Header.SetContentType("text/html")
+ }
next(ctx)
}
}
diff --git a/pkg/fileop/file.go b/pkg/fileop/file.go
index 07c08e5..10e2202 100644
--- a/pkg/fileop/file.go
+++ b/pkg/fileop/file.go
@@ -12,6 +12,9 @@ func GetHashFromPath(path string) string {
}
func IsMimeTypeSupported(mimetype string) bool {
- return strings.HasPrefix(mimetype, "video") &&
+ if mimetype == "image/svg+xml" {
+ return false
+ }
+ return strings.HasPrefix(mimetype, "video") ||
strings.HasPrefix(mimetype, "image")
}
diff --git a/pkg/fileop/thumbnail.go b/pkg/fileop/thumbnail.go
index 32f6064..fcdfa12 100644
--- a/pkg/fileop/thumbnail.go
+++ b/pkg/fileop/thumbnail.go
@@ -1,60 +1,55 @@
package fileop
import (
- "image"
- "image/jpeg"
- "os"
+ "bytes"
+ "fmt"
"os/exec"
+ "strconv"
- "github.com/disintegration/imaging"
+ "github.com/h2non/bimg"
)
func EncodeImageThumbnail(inputPath string, outputPath string, width, height int) error {
- inputImage, err := imaging.Open(inputPath, imaging.AutoOrientation(true))
+ buffer, err := bimg.Read(inputPath)
if err != nil {
return err
}
- thumbImage := imaging.Fit(inputImage, width, height, imaging.Lanczos)
- if err = encodeImageJPEG(thumbImage, outputPath, 60); err != nil {
- return err
- }
-
- return nil
-}
-
-func encodeImageJPEG(image image.Image, outputPath string, jpegQuality int) error {
- photo_file, err := os.Create(outputPath)
- if err != nil {
- return err
+ options := bimg.Options{
+ Width: width,
+ Height: height,
+ Embed: true,
+ Type: bimg.JPEG,
+ StripMetadata: true,
}
- defer photo_file.Close()
- err = jpeg.Encode(photo_file, image, &jpeg.Options{Quality: jpegQuality})
+ newImage, err := bimg.NewImage(buffer).Process(options)
if err != nil {
return err
}
- return nil
+ return bimg.Write(outputPath, newImage)
}
-func EncodeVideoThumbnail(inputPath string, outputPath string, width, height int) error {
+func EncodeVideoThumbnail(inputPath string, outputPath string, width, _ int) error {
args := []string{
"-i",
inputPath,
- "-vframes", "1", // output one frame
- "-an", // disable audio
- "-vf", "scale='min(1024,iw)':'min(1024,ih)':force_original_aspect_ratio=decrease:force_divisible_by=2",
- "-vf", "select=gte(n\\,100)",
+ "-y",
+ "-vframes", "1",
+ "-q:v", "1",
+ "-vf", "thumbnail,scale=" + strconv.Itoa(width) + ":-1",
outputPath,
}
cmd := exec.Command("ffmpeg", args...)
+ var b bytes.Buffer
+ cmd.Stderr = &b
+
if err := cmd.Run(); err != nil {
- return err
+ return fmt.Errorf("%s; %w", b.String(), err)
}
return nil
-
}
diff --git a/pkg/worker/list_processor.go b/pkg/worker/list_processor.go
index 0a07085..c060583 100644
--- a/pkg/worker/list_processor.go
+++ b/pkg/worker/list_processor.go
@@ -16,6 +16,10 @@ type (
Process(context.Context, T) error
}
+ OnFail[T any] interface {
+ OnFail(context.Context, T, error)
+ }
+
BatchProcessor[T any] interface {
Query(context.Context) ([]T, error)
Process(context.Context, T) error
@@ -77,6 +81,12 @@ func (l *batchProcessorWorker[T]) Start(ctx context.Context) error {
var wg sync.WaitGroup
for _, v := range values {
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ default:
+ }
+
wg.Add(1)
l.scheduler.Take()
go func(v T) {
@@ -84,6 +94,9 @@ func (l *batchProcessorWorker[T]) Start(ctx context.Context) error {
defer wg.Done()
if err := l.batchProcessor.Process(ctx, v); err != nil && !errors.Is(err, context.Canceled) {
l.logrus.WithError(err).Error("Error processing batch")
+ if failure, ok := l.batchProcessor.(OnFail[T]); ok {
+ failure.OnFail(ctx, v, err)
+ }
}
}(v)
}
diff --git a/pkg/worker/thumbnail_scanner.go b/pkg/worker/thumbnail_scanner.go
index cc201b8..168abef 100644
--- a/pkg/worker/thumbnail_scanner.go
+++ b/pkg/worker/thumbnail_scanner.go
@@ -2,6 +2,7 @@ package worker
import (
"context"
+ "fmt"
"math"
"os"
"path"
@@ -47,12 +48,12 @@ func (t *ThumbnailScanner) Process(ctx context.Context, media *repository.Media)
if media.IsVideo() {
err := fileop.EncodeVideoThumbnail(media.Path, output, 1080, 1080)
if err != nil {
- return err
+ return fmt.Errorf("Error thumbnail video %d; %w", media.ID, err)
}
} else {
- err := fileop.EncodeImageThumbnail(media.Path, output, 1080, math.MaxInt)
+ err := fileop.EncodeImageThumbnail(media.Path, output, 1080, math.MinInt32)
if err != nil {
- return err
+ return fmt.Errorf("Error thumbnail image %d; %w", media.ID, err)
}
}