saas_api/actions/data/text.py

128 lines
3.9 KiB
Python

# Importa a biblioteca nativa 'zlib' usada para compressão/descompressão de dados binários.
import zlib
# Importa a função 'rtf_to_text' da biblioteca 'striprtf',
# responsável por converter documentos RTF em texto plano legível.
from striprtf.striprtf import rtf_to_text
# Define uma classe utilitária chamada 'Text', contendo apenas métodos estáticos.
# Essa abordagem permite o uso direto sem necessidade de instanciar a classe.
class Text:
@staticmethod
def decompress(vf_string):
"""
Descomprime e decodifica texto de origem WPTools/Firebird.
Versão segura contra BLOB inválido (Firebird).
"""
if vf_string is None:
return ""
# ===============================
# 1) Tentativa segura de leitura
# ===============================
if hasattr(vf_string, "read"):
try:
vf_string = vf_string.read()
except Exception:
# BLOB inválido, conexão fechada ou handle perdido
return ""
if not vf_string:
return ""
# ===============================
# 2) Garantir bytes
# ===============================
if isinstance(vf_string, str):
vf_bytes = vf_string.encode("latin1", errors="ignore")
else:
try:
vf_bytes = bytes(vf_string)
except Exception:
return ""
# ===============================
# 3) Detectar zlib
# ===============================
is_zlib = (
len(vf_bytes) > 2
and vf_bytes[0] == 0x78
and vf_bytes[1] in (0x01, 0x9C, 0xDA)
)
# ===============================
# 4) Descompactar se necessário
# ===============================
if is_zlib:
try:
return zlib.decompress(vf_bytes).decode("iso-8859-1", errors="ignore")
except Exception:
# Se falhar, tenta como texto puro
pass
# ===============================
# 5) Texto puro
# ===============================
try:
return vf_bytes.decode("iso-8859-1", errors="ignore")
except Exception:
return ""
@staticmethod
def compress(text, *, encoding: str = "iso-8859-1"):
"""
Comprime texto/dados com zlib SEM Base64.
Parâmetros:
text: str | bytes | stream (com .read())
encoding: encoding usado quando 'text' for str (padrão: ISO-8859-1)
Retorno:
- bytes comprimidos (zlib)
Observações:
- Ideal para armazenamento direto em BLOB (Firebird, PostgreSQL, etc.)
- Evita overhead e custo do Base64
- Totalmente compatível com 'decompress'
"""
if text is None or text == "":
return b""
# Se for stream (ex.: BLOB do Firebird)
if hasattr(text, "read"):
raw = text.read()
else:
raw = text
# Garante bytes
if isinstance(raw, str):
raw_bytes = raw.encode(encoding, errors="ignore")
else:
raw_bytes = bytes(raw)
# Comprime com zlib e retorna bytes
return zlib.compress(raw_bytes)
@staticmethod
def to_text(raw_text: str) -> str:
"""
Converte o conteúdo RTF em texto simples e retorna como string.
Finalidade:
- Detectar automaticamente se o conteúdo está em formato RTF.
- Converter para texto plano usando a função 'rtf_to_text'.
- Retornar uma string limpa e pronta para uso.
"""
if not raw_text:
return ""
# Detecta cabeçalho RTF
if raw_text.strip().startswith("{\\rtf"):
try:
return rtf_to_text(raw_text).strip()
except Exception:
return raw_text
return raw_text.strip()