Browse Source

update webp format now converted to png as proposed by @Diden05 at issue #7

pull/28/head
Dimas Restu H 4 years ago
parent
commit
2029844004
  1. 2
      .env.default
  2. 2
      .env.development
  3. 2
      .env.production
  4. 26
      internal/whatsapp/whatsapp.go

2
.env.default

@ -28,6 +28,8 @@ HTTP_BASE_URL=/api/v1/whatsapp
WHATSAPP_DATASTORE_TYPE=sqlite
WHATSAPP_DATASTORE_URI=file:dbs/WhatsApp.db?_foreign_keys=on
WHATSAPP_MEDIA_IMAGE_CONVERT_WEBP=true
# WHATSAPP_VERSION_MAJOR=2
# WHATSAPP_VERSION_MINOR=2214
# WHATSAPP_VERSION_PATCH=9

2
.env.development

@ -28,6 +28,8 @@ AUTH_JWT_EXPIRED_HOUR=24
WHATSAPP_DATASTORE_TYPE=sqlite
WHATSAPP_DATASTORE_URI=file:dbs/WhatsApp.db?_foreign_keys=on
WHATSAPP_MEDIA_IMAGE_CONVERT_WEBP=true
WHATSAPP_VERSION_MAJOR=2
WHATSAPP_VERSION_MINOR=2214
WHATSAPP_VERSION_PATCH=9

2
.env.production

@ -28,6 +28,8 @@ AUTH_JWT_EXPIRED_HOUR=24
WHATSAPP_DATASTORE_TYPE=sqlite
WHATSAPP_DATASTORE_URI=file:dbs/WhatsApp.db?_foreign_keys=on
WHATSAPP_MEDIA_IMAGE_CONVERT_WEBP=true
WHATSAPP_VERSION_MAJOR=2
WHATSAPP_VERSION_MINOR=2214
WHATSAPP_VERSION_PATCH=9

26
internal/whatsapp/whatsapp.go

@ -2,7 +2,7 @@ package whatsapp
import (
"bytes"
"image/jpeg"
"image/png"
"io"
"mime/multipart"
"strconv"
@ -12,6 +12,7 @@ import (
"github.com/labstack/echo/v4"
"golang.org/x/image/webp"
"github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/env"
"github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/router"
pkgWhatsApp "github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/whatsapp"
@ -86,26 +87,31 @@ func sendMedia(c echo.Context, mediaType string) error {
// Issue #7 Old Version Client Cannot Render WebP Format
// If Media Type is "image" and MIME Type is "image/webp"
// Then Convert it as JPEG
// Then Convert it as PNG
var fileBytes []byte
if mediaType == "image" && fileType == "image/webp" {
isConvertMediaImageWebP := false
isConvertMediaImageWebP, _ = env.GetEnvBool("WHATSAPP_MEDIA_IMAGE_CONVERT_WEBP")
if isConvertMediaImageWebP {
// Decode WebP Image
fileWebP, err := webp.Decode(fileStream)
if err != nil {
return router.ResponseInternalError(c, err.Error())
return router.ResponseInternalError(c, "Error Decoding Image WebP Format")
}
// Encode to JPEG Image
fileJPEG := new(bytes.Buffer)
err = jpeg.Encode(fileJPEG, fileWebP, &jpeg.Options{Quality: 95})
// Encode to PNG Image
filePNG := new(bytes.Buffer)
err = png.Encode(filePNG, fileWebP)
if err != nil {
return router.ResponseInternalError(c, err.Error())
return router.ResponseInternalError(c, "Error Encoding Image PNG Format")
}
// Set File Stream Bytes and File Type
// To New Encoded JPEG Image and File Type to "image/jpeg"
fileBytes = fileJPEG.Bytes()
fileType = "image/jpeg"
// To New Encoded PNG Image and File Type to "image/png"
fileBytes = filePNG.Bytes()
fileType = "image/png"
}
} else {
// Convert File Stream in to Bytes
// Since WhatsApp Proto for Media is only Accepting Bytes format

Loading…
Cancel
Save