From fc9d192fffa0f633388a873868250049fecbe973 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 2 Sep 2019 10:27:01 +0300 Subject: [PATCH] convert from webp only if actual webp present --- go.mod | 1 + go.sum | 2 ++ src/routing.go | 46 +++++++++++++++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 38b5b20..2f7d683 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/google/go-cmp v0.2.0 // indirect github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c // indirect + github.com/h2non/filetype v1.0.10 github.com/h2non/gock v1.0.10 github.com/jessevdk/go-flags v1.4.0 github.com/jinzhu/gorm v1.9.1 diff --git a/go.sum b/go.sum index d42591d..3129f42 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,8 @@ github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 h1:zLTLjkaOF github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c h1:16eHWuMGvCjSfgRJKqIzapE78onvvTbdi1rMkU00lZw= github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/h2non/filetype v1.0.10 h1:z+SJfnL6thYJ9kAST+6nPRXp1lMxnOVbMZHNYHMar0s= +github.com/h2non/filetype v1.0.10/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU= github.com/h2non/gock v1.0.10 h1:EzHYzKKSLN4xk0w193uAy3tp8I3+L1jmaI2Mjg4lCgU= github.com/h2non/gock v1.0.10/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE= github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= diff --git a/src/routing.go b/src/routing.go index be48f96..50af878 100644 --- a/src/routing.go +++ b/src/routing.go @@ -1,10 +1,12 @@ package main import ( + "bytes" "encoding/json" "fmt" "image/png" "io" + "io/ioutil" "net/http" "strconv" "strings" @@ -12,6 +14,8 @@ import ( "github.com/gin-gonic/gin" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/h2non/filetype" + filetypes "github.com/h2non/filetype/matchers" v5 "github.com/retailcrm/api-client-go/v5" v1 "github.com/retailcrm/mg-transport-api-client-go/v1" "golang.org/x/image/webp" @@ -1127,27 +1131,43 @@ func convertAndUploadImage(client *v1.MgClient, url string) (v1.Item, error) { return item, err } - img, err := webp.Decode(res.Body) + imgByte, err := ioutil.ReadAll(res.Body) if err != nil { return item, err } - pReader, pWriter := io.Pipe() - - go func() { - defer pWriter.Close() - err = png.Encode(pWriter, img) + if kind, err := filetype.Match(imgByte); err != nil { + return item, err + } else if kind == filetypes.TypeWebp { + img, err := webp.Decode(res.Body) if err != nil { - logger.Info(item, err.Error()) + return item, err } - }() - data, _, err := client.UploadFile(pReader) - if err != nil { - return item, err + pReader, pWriter := io.Pipe() + + go func() { + defer pWriter.Close() + err = png.Encode(pWriter, img) + if err != nil { + logger.Info(item, err.Error()) + } + }() + + data, _, err := client.UploadFile(pReader) + if err != nil { + return item, err + } + + item.ID = data.ID + } else { + data, _, err := client.UploadFile(bytes.NewReader(imgByte)) + if err != nil { + return item, err + } + + item.ID = data.ID } - item.ID = data.ID - return item, nil }