diff --git a/.env.default b/.env.default index b336ca4..9f9a624 100644 --- a/.env.default +++ b/.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 diff --git a/.env.development b/.env.development index d2964f3..80d0bf5 100644 --- a/.env.development +++ b/.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 diff --git a/.env.production b/.env.production index e0374fa..ebf4100 100644 --- a/.env.production +++ b/.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 diff --git a/internal/whatsapp/whatsapp.go b/internal/whatsapp/whatsapp.go index 6d52ffd..2b3c4dd 100644 --- a/internal/whatsapp/whatsapp.go +++ b/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" { - // Decode WebP Image - fileWebP, err := webp.Decode(fileStream) - if err != nil { - return router.ResponseInternalError(c, err.Error()) + 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, "Error Decoding Image WebP Format") + } + + // Encode to PNG Image + filePNG := new(bytes.Buffer) + err = png.Encode(filePNG, fileWebP) + if err != nil { + return router.ResponseInternalError(c, "Error Encoding Image PNG Format") + } + + // Set File Stream Bytes and File Type + // To New Encoded PNG Image and File Type to "image/png" + fileBytes = filePNG.Bytes() + fileType = "image/png" } - - // Encode to JPEG Image - fileJPEG := new(bytes.Buffer) - err = jpeg.Encode(fileJPEG, fileWebP, &jpeg.Options{Quality: 95}) - if err != nil { - return router.ResponseInternalError(c, err.Error()) - } - - // Set File Stream Bytes and File Type - // To New Encoded JPEG Image and File Type to "image/jpeg" - fileBytes = fileJPEG.Bytes() - fileType = "image/jpeg" } else { // Convert File Stream in to Bytes // Since WhatsApp Proto for Media is only Accepting Bytes format