diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-07-03 23:13:04 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-07-03 23:13:04 +0200 |
commit | c2d666b43477ea7042b574ad940c508216cb0e83 (patch) | |
tree | a459b552374114c4ef9ebe9966ed6ec83bb9a11d /pkg/fileop | |
parent | 6e84441dab0a2b89869e33d7e89d14189d9b67c0 (diff) | |
download | lens-c2d666b43477ea7042b574ad940c508216cb0e83.tar.gz lens-c2d666b43477ea7042b574ad940c508216cb0e83.tar.bz2 lens-c2d666b43477ea7042b574ad940c508216cb0e83.zip |
fix: Fix content type
Content type was always being set to `text/html`.
Also swap lib for processing thumbnail for something that accepts HEIC.
Diffstat (limited to 'pkg/fileop')
-rw-r--r-- | pkg/fileop/file.go | 5 | ||||
-rw-r--r-- | pkg/fileop/thumbnail.go | 49 |
2 files changed, 26 insertions, 28 deletions
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 - } |