diff --git a/partitioncloud/modules/search.py b/partitioncloud/modules/search.py index 060e1dd..e28891a 100644 --- a/partitioncloud/modules/search.py +++ b/partitioncloud/modules/search.py @@ -8,6 +8,7 @@ import threading import socket import os +import pypdf import googlesearch from .db import get_db @@ -52,12 +53,17 @@ def local_search(query, partitions): def download_search_result(element, instance_path): uuid = element["uuid"] url = element["url"] + filename = f"{instance_path}/search-partitions/{uuid}.pdf" try: - urllib.request.urlretrieve(url, f"{instance_path}/search-partitions/{uuid}.pdf") + urllib.request.urlretrieve(url, filename) + pypdf.PdfReader(filename) - except (urllib.error.HTTPError, urllib.error.URLError): - with open(f"{instance_path}/search-partitions/{uuid}.pdf", 'a', encoding="utf8") as _: + except (urllib.error.HTTPError, urllib.error.URLError, + pypdf.errors.PdfReadError, pypdf.errors.PdfStreamError): + if os.path.exists(filename): + os.remove(filename) + with open(filename, 'a', encoding="utf8") as _: pass # Create empty file diff --git a/requirements.txt b/requirements.txt index 8a0b15a..0fd1cf3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ flask flask-babel google colorama +pypdf qrcode \ No newline at end of file diff --git a/scripts/hooks/v1.py b/scripts/hooks/v1.py index 07776b1..995bf78 100644 --- a/scripts/hooks/v1.py +++ b/scripts/hooks/v1.py @@ -179,4 +179,12 @@ def move_thumbnails(): def install_babel(): - utils.install_package("flask-babel") \ No newline at end of file + utils.install_package("flask-babel") + + +""" + v1.8.* +""" + +def install_pypdf(): + utils.install_package("pypdf") \ No newline at end of file diff --git a/scripts/migration.py b/scripts/migration.py index a93302c..b99d1a2 100644 --- a/scripts/migration.py +++ b/scripts/migration.py @@ -35,7 +35,8 @@ hooks = [ ("v1.4.1", [("Install qrcode", v1_hooks.install_qrcode)]), ("v1.5.0", [("Move to instance directory", v1_hooks.move_instance)]), ("v1.5.1", [("Move thumbnails", v1_hooks.move_thumbnails)]), - ("v1.7.0", [("Install babel", v1_hooks.install_babel)]) + ("v1.7.0", [("Install babel", v1_hooks.install_babel)]), + ("v1.8.2", [("Install pypdf", v1_hooks.install_pypdf)]) ]