Skip to content

Commit

Permalink
fix: enhance WEBP support in thumbnail generation and improve error h…
Browse files Browse the repository at this point in the history
…andling for image decoding
  • Loading branch information
PlusOne committed Dec 31, 2024
1 parent deaa318 commit 04c4343
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2863,7 +2863,7 @@ func monitorDirectoryChanges(dir string) {

func generateThumbnail(originalPath, size string) error {
// Check if thumbnail generation is enabled
if !conf.Thumbnails.Enabled {
if (!conf.Thumbnails.Enabled) {
log.Debug("Thumbnail generation is disabled")
return nil
}
Expand Down Expand Up @@ -2949,33 +2949,42 @@ func generateThumbnailWithImaging(originalPath, thumbnailPath string, width, hei
ext := strings.ToLower(filepath.Ext(originalPath))

if ext == ".webp" {
// Open WEBP image using webp package
file, err := os.Open(originalPath)
if err != nil {
return fmt.Errorf("failed to open WEBP image: %w", err)
}
defer file.Close()
// Handle WEBP format
file, err := os.Open(originalPath)
if err != nil {
return fmt.Errorf("failed to open WEBP image: %w", err)
}
defer file.Close()

img, err = webp.Decode(file)
if err != nil {
return fmt.Errorf("failed to decode WEBP image: %w", err)
}
} else {
// Open other image formats using imaging
img, err = imaging.Open(originalPath)
if err != nil {
return fmt.Errorf("failed to open image: %w", err)
}
}
img, err = webp.Decode(file)
if err != nil {
return fmt.Errorf("failed to decode WEBP image: %w", err)
}
} else {
// Handle other formats using image.Decode
file, err := os.Open(originalPath)
if err != nil {
return fmt.Errorf("failed to open image: %w", err)
}
defer file.Close()

// Resize the image using Lanczos filter
thumb := imaging.Resize(img, width, height, imaging.Lanczos)
img, _, err = image.Decode(file)
if err != nil {
log.Errorf("Image decoding failed for %s: %v", originalPath, err)
return fmt.Errorf("failed to decode image: %w", err)
}
}

// Resize the image
thumbnail := imaging.Resize(img, width, height, imaging.Lanczos)

// Save the thumbnail
err = imaging.Save(thumb, thumbnailPath)
err = imaging.Save(thumbnail, thumbnailPath)
if err != nil {
return fmt.Errorf("failed to save thumbnail: %v", err)
return fmt.Errorf("failed to save thumbnail: %w", err)
}

log.Infof("Thumbnail generated successfully for %s", originalPath)
return nil
}

Expand Down

0 comments on commit 04c4343

Please sign in to comment.