feat(): Aplicado função que irá efetuar a descomp
This commit is contained in:
parent
ec0b383c35
commit
c00c5cab3c
2 changed files with 52 additions and 2 deletions
49
actions/compress_decompress/compress_decompress.py
Normal file
49
actions/compress_decompress/compress_decompress.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
@staticmethod
|
||||
def decompress(vf_string):
|
||||
"""
|
||||
Descomprime e decodifica texto compactado ou codificado.
|
||||
|
||||
Agora:
|
||||
- Tenta automaticamente Base64 decode ANTES de tudo.
|
||||
- Depois detecta zlib.
|
||||
- Decodifica ISO-8859-1 se não for zlib.
|
||||
"""
|
||||
|
||||
if not vf_string:
|
||||
return ""
|
||||
|
||||
# Caso seja stream (ex.: BLOB Firebird)
|
||||
if hasattr(vf_string, "read"):
|
||||
vf_string = vf_string.read()
|
||||
|
||||
# Garante bytes
|
||||
if isinstance(vf_string, str):
|
||||
vf_bytes = vf_string.encode("latin1", errors="ignore")
|
||||
else:
|
||||
vf_bytes = vf_string
|
||||
|
||||
# --- NOVO: TENTATIVA DE BASE64 DECODE ANTES ---
|
||||
try:
|
||||
# Remove quebras e espaços — comum em JSON, Firebird, Delphi
|
||||
cleaned = vf_bytes.strip().replace(b"\n", b"").replace(b"\r", b"")
|
||||
decoded = base64.b64decode(cleaned, validate=True)
|
||||
vf_bytes = decoded
|
||||
except Exception:
|
||||
# Não era base64, segue normalmente
|
||||
pass
|
||||
|
||||
# Detecta assinatura zlib
|
||||
is_zlib = len(vf_bytes) > 2 and vf_bytes[0] == 0x78 and vf_bytes[1] in (0x9C, 0xDA)
|
||||
|
||||
if is_zlib:
|
||||
try:
|
||||
text = zlib.decompress(vf_bytes).decode("iso-8859-1", errors="ignore")
|
||||
return text
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Caso não seja zlib, tenta apenas decodificar ISO-8859-1
|
||||
try:
|
||||
return vf_bytes.decode("iso-8859-1", errors="ignore")
|
||||
except Exception:
|
||||
return str(vf_string)
|
||||
|
|
@ -2,6 +2,7 @@ from typing import Optional, Dict, Any, List
|
|||
from sqlalchemy import func
|
||||
from fastapi import HTTPException, status
|
||||
from database.mysql import SessionLocal, get_database_settings
|
||||
from actions.compress_decompress.compress_decompress import decompress
|
||||
from packages.v1.administrativo.models.ato_principal_model import AtoPrincipal
|
||||
from packages.v1.administrativo.models.ato_parte_model import AtoParte
|
||||
from packages.v1.administrativo.models.ato_documento_model import AtoDocumento
|
||||
|
|
@ -267,8 +268,8 @@ class ShowAtosRepository:
|
|||
|
||||
# === 5. Montar JSON final ===
|
||||
return {
|
||||
"inteiro_teor": (
|
||||
ato.inteiro_teor.decode("utf-8") if ato.inteiro_teor else None
|
||||
"inteiro_teor": decompress(
|
||||
(ato.inteiro_teor.decode("utf-8") if ato.inteiro_teor else None)
|
||||
),
|
||||
"identificacao_pedido_na_cgj": ato.identificacao_pedido_cgj,
|
||||
"tipo_de_ato": ato.tipo_ato,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue