From f46bc3e14159abf188da31cfd5fa0dbfcd1ff332 Mon Sep 17 00:00:00 2001 From: "MC51 (Michael)" Date: Sun, 19 Jun 2022 01:19:42 +0200 Subject: [PATCH] add more supported img formats: jpeg, gif, ico, bmp --- clipster/utils.go | 15 +++++++++++++-- go.mod | 2 ++ go.sum | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/clipster/utils.go b/clipster/utils.go index 9afb9f3..b9e5e3e 100644 --- a/clipster/utils.go +++ b/clipster/utils.go @@ -6,12 +6,18 @@ import ( "encoding/base64" "errors" "image" + _ "image/gif" + _ "image/jpeg" "image/png" "io/ioutil" "log" + "net/http" "regexp" "strings" + _ "github.com/biessek/golang-ico" + _ "golang.org/x/image/bmp" + "github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" @@ -30,10 +36,15 @@ func BytesToPixbuf(img []byte) *gdk.Pixbuf { // BytesToImage reads bytes and returns image.Image. If bytes are not a valid Image // return a default "file not found" Image func BytesToImage(img []byte) (image.Image, error) { - img_decoded, _, err := image.Decode(bytes.NewReader(img)) + + mimeType := http.DetectContentType(img) + log.Printf("BytesToImage mimeType: %s", mimeType) + + img_decoded, format, err := image.Decode(bytes.NewReader(img)) + log.Printf("BytesToImage Decode Format: %s", format) if err != nil { log.Println("Error BytesToImage:", err) - log.Println("Returning 'missing file' image instead") + log.Println("Returning 'missing file' image instead", format) img_decoded, _, err = image.Decode(bytes.NewReader(PNG_BYTES_IMAGE_NOTFOUND)) } return img_decoded, err diff --git a/go.mod b/go.mod index 4343881..3bb8ef7 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( ) require ( + github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670 // indirect github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect @@ -29,6 +30,7 @@ require ( github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c // indirect github.com/gopherjs/gopherwasm v1.1.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect github.com/magiconair/properties v1.8.5 // indirect github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect diff --git a/go.sum b/go.sum index ddaef55..49aab53 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670 h1:FQPKKjDhzG0T4ew6dm6MGrXb4PRAi8ZmTuYuxcF62BM= +github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670/go.mod h1:iRWAFbKXMMkVQyxZ1PfGlkBr1TjATx1zy2MRprV7A3Q= github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa h1:Wg+722vs7a2zQH5lR9QWYsVbplKeffaQFIs5FTdfNNo= github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa/go.mod h1:6Arca19mRx58CA7OWEd7Wu1NpC1rd3uDnNs6s1pj/DI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -40,6 +42,8 @@ github.com/gotk3/gotk3 v0.6.1 h1:GJ400a0ecEEWrzjBvzBzH+pB/esEMIGdB9zPSmBdoeo= github.com/gotk3/gotk3 v0.6.1/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e h1:LvL4XsI70QxOGHed6yhQtAU34Kx3Qq2wwBzGFKY8zKk= +github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=