1
0
mirror of synced 2024-11-24 20:56:04 +03:00

convert from webp only if actual webp present

This commit is contained in:
Pavel 2019-09-02 10:27:01 +03:00
parent 4edda99f81
commit fc9d192fff
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-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

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/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=

View File

@ -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,6 +1131,14 @@ func convertAndUploadImage(client *v1.MgClient, url string) (v1.Item, error) {
return item, err
}
imgByte, err := ioutil.ReadAll(res.Body)
if err != nil {
return item, err
}
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 {
return item, err
@ -1148,6 +1160,14 @@ func convertAndUploadImage(client *v1.MgClient, url string) (v1.Item, error) {
}
item.ID = data.ID
} else {
data, _, err := client.UploadFile(bytes.NewReader(imgByte))
if err != nil {
return item, err
}
item.ID = data.ID
}
return item, nil
}