1
0
mirror of synced 2024-11-22 04:26:01 +03:00

Merge pull request #82 from Neur0toxine/master

[fix] convert from webp only if actual webp present
This commit is contained in:
Alex Lushpai 2019-09-06 13:39:45 +03:00 committed by GitHub
commit e5b29580d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 13 deletions

1
go.mod
View File

@ -24,6 +24,7 @@ require (
github.com/google/go-cmp v0.2.0 // indirect github.com/google/go-cmp v0.2.0 // indirect
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c // 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/h2non/gock v1.0.10
github.com/jessevdk/go-flags v1.4.0 github.com/jessevdk/go-flags v1.4.0
github.com/jinzhu/gorm v1.9.1 github.com/jinzhu/gorm v1.9.1

2
go.sum
View File

@ -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/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 h1:16eHWuMGvCjSfgRJKqIzapE78onvvTbdi1rMkU00lZw=
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= 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 h1:EzHYzKKSLN4xk0w193uAy3tp8I3+L1jmaI2Mjg4lCgU=
github.com/h2non/gock v1.0.10/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE= github.com/h2non/gock v1.0.10/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=

View File

@ -1,10 +1,12 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"image/png" "image/png"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -12,6 +14,8 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" 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" v5 "github.com/retailcrm/api-client-go/v5"
v1 "github.com/retailcrm/mg-transport-api-client-go/v1" v1 "github.com/retailcrm/mg-transport-api-client-go/v1"
"golang.org/x/image/webp" "golang.org/x/image/webp"
@ -1127,27 +1131,43 @@ func convertAndUploadImage(client *v1.MgClient, url string) (v1.Item, error) {
return item, err return item, err
} }
img, err := webp.Decode(res.Body) imgByte, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return item, err return item, err
} }
pReader, pWriter := io.Pipe() if kind, err := filetype.Match(imgByte); err != nil {
return item, err
go func() { } else if kind == filetypes.TypeWebp {
defer pWriter.Close() img, err := webp.Decode(res.Body)
err = png.Encode(pWriter, img)
if err != nil { if err != nil {
logger.Info(item, err.Error()) return item, err
} }
}()
data, _, err := client.UploadFile(pReader) pReader, pWriter := io.Pipe()
if err != nil {
return item, err 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 return item, nil
} }