From cfa159b3fea489298faa0f43fb1cb2a8c27d6e28 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 5 Jan 2021 13:31:39 -0600 Subject: [PATCH] Detect binary file uploads --- src/pgwui_core/core.py | 5 +++++ src/pgwui_core/exceptions.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/pgwui_core/core.py b/src/pgwui_core/core.py index ab17aee..4fd045c 100644 --- a/src/pgwui_core/core.py +++ b/src/pgwui_core/core.py @@ -893,6 +893,11 @@ class UploadData(DBData): line = next(self._fileo) except StopIteration: raise core_ex.NoDataError('Uploaded file contains no data') + except UnicodeError as ex: + raise core_ex.CantDecodeError( + 'Not a text file', + ("The file's content is not recognized as Unicode text, " + f'the error is: {ex}')) else: self.lineno += 1 # Intuit the eol sequence diff --git a/src/pgwui_core/exceptions.py b/src/pgwui_core/exceptions.py index ea618ed..22cb2a6 100644 --- a/src/pgwui_core/exceptions.py +++ b/src/pgwui_core/exceptions.py @@ -149,6 +149,12 @@ class NoDataError(Error): super(NoDataError, self).__init__(e, descr, detail) +class CantDecodeError(Error): + '''Can't decode file data into unicode''' + def __init__(self, e, descr='', detail=''): + super().__init__(e, descr, detail) + + class DuplicateUploadError(Error): '''The same filename updated twice into the same db''' def __init__(self, e, descr='', detail=''): -- 2.34.1