From f224fcc0bce483f8eb33a2faa351324acfa734ec Mon Sep 17 00:00:00 2001 From: alessandro90 Date: Sat, 3 Nov 2018 20:38:05 +0100 Subject: [PATCH] Add checksum control od db download and switch to urllib3 --- download_thread.py | 25 ++++++++++++++++++------- download_window.py | 11 ++++++++++- utilities.py | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 utilities.py diff --git a/download_thread.py b/download_thread.py index 2a7d0c1..699976e 100644 --- a/download_thread.py +++ b/download_thread.py @@ -2,13 +2,15 @@ from io import BytesIO from os import mkdir import os.path from shutil import rmtree -import urllib +import urllib3 from zipfile import ZipFile from PyQt5.QtCore import QThread, pyqtSignal +import utilities class DownloadThread(QThread): no_connection_error = pyqtSignal() bad_db_download_error = pyqtSignal() + bad_file_error = pyqtSignal() def __init__(self, db_location, path): super().__init__() @@ -22,12 +24,11 @@ class DownloadThread(QThread): self.wait() def run(self): - if os.path.exists(self.__path): - rmtree(self.__path) try: - db = urllib.request.urlopen(self.__db_location) + db = urllib3.PoolManager().request('GET', self.__db_location) + # db = urllib.request.urlopen(self.__db_location) # raise urllib.error.URLError('Test') - except urllib.error.URLError: # No internet connection. + except urllib3.exceptions.MaxRetryError: # No internet connection. self.regular_execution = False self.no_connection_error.emit() return @@ -36,8 +37,18 @@ class DownloadThread(QThread): self.reason = db.reason self.bad_db_download_error.emit() return + if not utilities.checksum_ok(db.data, "folder"): + regular_execution = False + self.bad_db_download_error.emit() + return + if os.path.exists(self.__path): + rmtree(self.__path) try: - with ZipFile(BytesIO(db.read())) as zipped: + # data_folder = db.read() + # data_folder = db.data + with ZipFile(BytesIO(db.data)) as zipped: zipped.extractall() except: - pass + self.regular_execution = False + self.bad_file_error.emit() + return diff --git a/download_window.py b/download_window.py index d609bcf..1469d3b 100644 --- a/download_window.py +++ b/download_window.py @@ -18,14 +18,23 @@ class DownloadWindow(QWidget, Ui_Download_window): self.everything_ok = True self.no_internet_msg = QMessageBox(self) self.no_internet_msg.setWindowTitle("No internet connection") - self.no_internet_msg.setText("Unable to establish an internet connection") + self.no_internet_msg.setText("Unable to establish an internet connection.") # self.no_internet_msg.buttonClicked.connect(self.close) self.no_internet_msg.finished.connect(self.close) self.bad_db_download_msg = QMessageBox(self) self.bad_db_download_msg.setWindowTitle("Something wrong") + self.bad_db_download_msg.setText("""Something went wrong with the downaload. + Check your internet connection and try again.""") self.bad_db_download_msg.finished.connect(self.close) + self.bad_file_msg = QMessageBox(self) + self.bad_file_msg.setWindowTitle("Bad file detected") + self.bad_file_msg.setText("""The downloaded file seems to be corrupted. + The old database has not been deleted and + the downloaded file has been discarded.""") + self.bad_file_msg.finished.connect(self.close) + self.download_thread = DownloadThread(db_location, data_folder) self.download_thread.finished.connect(self.wait_close) self.download_thread.no_connection_error.connect(self.show_no_connection_warning) diff --git a/utilities.py b/utilities.py new file mode 100644 index 0000000..9027886 --- /dev/null +++ b/utilities.py @@ -0,0 +1,18 @@ +import hashlib +from pandas import read_csv + +def checksum_ok(data, what): + code = hashlib.sha256() + code.update(data) + ref_loc = 'https://aresvalley.com/Storage/Artemis/Database/data.zip.log' + if what == "folder": + n = 0 + elif what == "db": + n = 1 + else: + raise ValueError("Wrong entry name.") + try: + reference = read_csv(ref_loc, delimiter = '*').iat[-1, n] + except HTTPError: + return False + return code.hexdigest() == reference \ No newline at end of file