perbaikan decompres

This commit is contained in:
2025-09-10 18:08:31 +07:00
parent e61eee5f76
commit 7d03b5d5ef
4 changed files with 386 additions and 47 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"
"api-service/internal/config"
@@ -16,6 +17,18 @@ import (
"github.com/rs/zerolog/log"
)
// cleanResponse removes invalid characters and BOM from the response string
func cleanResponse(resp string) string {
// Remove UTF-8 BOM
resp = strings.TrimPrefix(resp, "\xef\xbb\xbf")
resp = strings.TrimPrefix(resp, "\ufeff")
// Remove null characters
resp = strings.ReplaceAll(resp, "\x00", "")
// Trim whitespace
resp = strings.TrimSpace(resp)
return resp
}
// VClaimService interface for VClaim operations
type VClaimService interface {
Get(ctx context.Context, endpoint string, result interface{}) error
@@ -172,6 +185,12 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTOVclaim, error)
return finalResp, nil
}
// Check if response looks like HTML or error message (don't try to decrypt)
if strings.HasPrefix(respMentah.Response, "<") || strings.Contains(respMentah.Response, "error") {
finalResp.Response = respMentah.Response
return finalResp, nil
}
// Decrypt response
consID, secretKey, _, tstamp, _ := s.config.SetHeader()
decryptionKey := consID + secretKey + tstamp
@@ -190,6 +209,8 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTOVclaim, error)
// Try to unmarshal decrypted response as JSON
if respDecrypt != "" {
// Clean the decrypted response
respDecrypt = cleanResponse(respDecrypt)
if err := json.Unmarshal([]byte(respDecrypt), &finalResp.Response); err != nil {
// If JSON unmarshal fails, store as string
log.Warn().Err(err).Msg("Failed to unmarshal decrypted response, storing as string")