From: Karl O. Pinc Date: Tue, 5 Jan 2021 19:31:39 +0000 (-0600) Subject: Detect binary file uploads X-Git-Url: https://papio.biology.duke.edu/gitweb/?a=commitdiff_plain;h=cfa159b3fea489298faa0f43fb1cb2a8c27d6e28;p=pgwui_core Detect binary file uploads --- 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=''):