From 5870f2077c12696f06afa932b487b96535de744d Mon Sep 17 00:00:00 2001 From: Alexander Renz Date: Tue, 31 Dec 2024 15:23:38 +0100 Subject: [PATCH] fix: enhance thumbnail generation by adding image repair logic and deduplication path check --- cmd/server/main.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 1d38409..b72eb04 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2870,10 +2870,6 @@ func generateThumbnail(originalPath, size string) error { log.Debugf("Generating thumbnail for: %s with size: %s", originalPath, size) - if !conf.Thumbnails.Enabled { - return nil - } - // Check if the file is an image if !isImageFile(originalPath) { log.Infof("File %s is not an image. Skipping thumbnail generation.", originalPath) @@ -2905,6 +2901,12 @@ func generateThumbnail(originalPath, size string) error { return fmt.Errorf("error checking thumbnail existence: %v", err) } + // Check if the original image is in the dedup directory + originalDedupPath := filepath.Join(conf.Deduplication.Directory, filepath.Base(originalPath)) + if _, err := os.Stat(originalDedupPath); err == nil { + originalPath = originalDedupPath + } + // Check if ffmpeg is installed if isFFmpegInstalled() { // Use ffmpeg to generate the thumbnail @@ -2920,11 +2922,25 @@ func generateThumbnail(originalPath, size string) error { // Use Go internal imaging function to generate the thumbnail err := generateThumbnailWithImaging(originalPath, thumbnailPath, width, height) if err != nil { - if conf.Thumbnails.SkipOnFailure { - log.Warnf("Failed to generate thumbnail with imaging for %s, skipping: %v", originalPath, err) - return nil + // Attempt to repair the image if it appears corrupt + err = fixCorruptedImage(originalPath) + if err != nil { + log.Warnf("Failed to repair image %s: %v", originalPath, err) + if conf.Thumbnails.SkipOnFailure { + return nil + } + return fmt.Errorf("failed to repair image: %v", err) + } + + // Retry generating the thumbnail after repair + err = generateThumbnailWithImaging(originalPath, thumbnailPath, width, height) + if err != nil { + if conf.Thumbnails.SkipOnFailure { + log.Warnf("Failed to generate thumbnail with imaging for %s, skipping: %v", originalPath, err) + return nil + } + return fmt.Errorf("failed to generate thumbnail with imaging: %v", err) } - return fmt.Errorf("failed to generate thumbnail with imaging: %v", err) } }