diff --git a/tools/server/public_simplechat/local.tools/filemagic.py b/tools/server/public_simplechat/local.tools/filemagic.py index 8d2224e70f..054989ca39 100644 --- a/tools/server/public_simplechat/local.tools/filemagic.py +++ b/tools/server/public_simplechat/local.tools/filemagic.py @@ -4,21 +4,10 @@ import urllib.request import urllib.parse import debug +import toolcall as mTC from dataclasses import dataclass -@dataclass(frozen=True) -class Response: - """ - Used to return result wrt urlreq helper below. - """ - callOk: bool - statusCode: int - statusMsg: str = "" - contentType: str = "" - contentData: bytes = b"" - - def get_from_web(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]): """ @@ -52,9 +41,9 @@ def get_from_web(url: str, tag: str, inContentType: str, inHeaders: dict[str, st contentType = response.getheader('Content-Type') or inContentType print(f"DBUG:FM:GFW:Resp:{response.status}:{response.msg}") debug.dump({ 'op': 'FileMagic.GetFromWeb', 'url': req.full_url, 'req.headers': req.headers, 'resp.headers': response.headers, 'ctype': contentType }, { 'cdata': contentData }) - return Response(True, statusCode, statusMsg, contentType, contentData) + return mTC.TCOutResponse(True, statusCode, statusMsg, contentType, contentData) except Exception as exc: - return Response(False, 502, f"WARN:{tag}:Failed:{exc}") + return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}") def get_from_local(urlParts: urllib.parse.ParseResult, tag: str, inContentType: str): @@ -64,9 +53,9 @@ def get_from_local(urlParts: urllib.parse.ParseResult, tag: str, inContentType: try: fPdf = open(urlParts.path, 'rb') dPdf = fPdf.read() - return Response(True, 200, "", inContentType, dPdf) + return mTC.TCOutResponse(True, 200, "", inContentType, dPdf) except Exception as exc: - return Response(False, 502, f"WARN:{tag}:Failed:{exc}") + return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}") def get_file(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]={}): diff --git a/tools/server/public_simplechat/local.tools/tcpdf.py b/tools/server/public_simplechat/local.tools/tcpdf.py index 1d305f4f2b..da3e5aedfe 100644 --- a/tools/server/public_simplechat/local.tools/tcpdf.py +++ b/tools/server/public_simplechat/local.tools/tcpdf.py @@ -48,7 +48,7 @@ def process_pdftext(url: str, startPN: int, endPN: int): return mTC.TCOutResponse(False, gotVU.statusCode, gotVU.statusMsg) gotFile = mFile.get_file(url, "ProcessPdfText", "application/pdf", {}) if not gotFile.callOk: - return mTC.TCOutResponse(False, gotFile.statusCode, gotFile.statusMsg, gotFile.contentType, gotFile.contentData) + return gotFile tPdf = "" oPdf = pypdf.PdfReader(io.BytesIO(gotFile.contentData)) if (startPN <= 0): diff --git a/tools/server/public_simplechat/local.tools/tcweb.py b/tools/server/public_simplechat/local.tools/tcweb.py index a80789fed5..03e8caaa7b 100644 --- a/tools/server/public_simplechat/local.tools/tcweb.py +++ b/tools/server/public_simplechat/local.tools/tcweb.py @@ -42,8 +42,7 @@ def handle_urlreq(url: str, inHeaders: http.client.HTTPMessage, tag: str): 'Accept-Language': hAL } # Get requested url - gotFile = mFile.get_file(url, tag, "text/html", headers) - return mTC.TCOutResponse(gotFile.callOk, gotFile.statusCode, gotFile.statusMsg, gotFile.contentType, gotFile.contentData) + return mFile.get_file(url, tag, "text/html", headers) except Exception as exc: return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}")