From f301ca9a8a27abe6391393e76de5b193681c228c Mon Sep 17 00:00:00 2001 From: rr- Date: Mon, 26 Dec 2016 19:03:04 +0100 Subject: [PATCH] server/image-hash: fix handling invalid input --- API.md | 1 + server/szurubooru/func/image_hash.py | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/API.md b/API.md index 6d1e193..2b38172 100644 --- a/API.md +++ b/API.md @@ -1074,6 +1074,7 @@ data. - **Errors** + - input file is not an image - privileges are too low - **Description** diff --git a/server/szurubooru/func/image_hash.py b/server/szurubooru/func/image_hash.py index 9c392c5..653ac47 100644 --- a/server/szurubooru/func/image_hash.py +++ b/server/szurubooru/func/image_hash.py @@ -1,7 +1,7 @@ import elasticsearch import elasticsearch_dsl from image_match.elasticsearch_driver import SignatureES -from szurubooru import config +from szurubooru import config, errors # pylint: disable=invalid-name @@ -31,14 +31,19 @@ def delete_image(path): def search_by_image(image_content): - for result in session.search_image( - path=image_content, # sic - bytestream=True): - yield { - 'score': result['score'], - 'dist': result['dist'], - 'path': result['path'], - } + try: + for result in session.search_image( + path=image_content, # sic + bytestream=True): + yield { + 'score': result['score'], + 'dist': result['dist'], + 'path': result['path'], + } + except elasticsearch.exceptions.ElasticsearchException as ex: + raise + except Exception as ex: + raise errors.SearchError('Error searching (invalid input?)') def purge():