From 683143d7ad07f55ee782865032749895e42284ed Mon Sep 17 00:00:00 2001 From: Cesura Date: Fri, 16 Apr 2021 01:08:17 +0300 Subject: [PATCH] Bugfix: json parsing error handling --- app.py | 3 ++- pastey/functions.py | 36 +++++++++++++++++++++++------------- requirements.txt | 6 +++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app.py b/app.py index e5a8a07..e54a324 100644 --- a/app.py +++ b/app.py @@ -6,7 +6,7 @@ from os import environ from distutils.util import strtobool from threading import Thread -pastey_version = "0.3" +pastey_version = "0.3.1" loaded_config = {} loaded_themes = [] @@ -45,6 +45,7 @@ if __name__ == "__main__": print("Pastey version ", pastey_version) print("USING THE FOLLOWING CONFIGURATION:") print("=====================================") + loaded_config['pastey_version'] = pastey_version for option in dir(config): if not option.startswith("__"): loaded_config[option] = eval("config.%s" % option) diff --git a/pastey/functions.py b/pastey/functions.py index edc207d..429401a 100644 --- a/pastey/functions.py +++ b/pastey/functions.py @@ -18,18 +18,25 @@ def get_recent(limit=config.recent_pastes): recent_pastes = [] i = 0 while i < limit and i < len(paths): - with open(paths[i]) as fp: - paste = json.loads(fp.read()) - - basename = path.basename(paths[i]) - paste['unique_id'] = basename[:-8] if basename.endswith(".expires") else basename - paste['content'] = '\n'.join(paste['content'].splitlines()[0:10]) - paste['icon'] = common.get_icon(paste['language']) - - if paste['encrypted']: - paste['content'] = "[Encrypted]" - - recent_pastes.append(paste) + if paths[i].is_file(): + with open(paths[i]) as fp: + try: + paste = json.loads(fp.read()) + except json.JSONDecodeError: + i += 1 + continue + + # Set extra metadata + basename = path.basename(paths[i]) + paste['unique_id'] = basename[:-8] if basename.endswith(".expires") else basename + paste['content'] = '\n'.join(paste['content'].splitlines()[0:10]) + paste['icon'] = common.get_icon(paste['language']) + + # Replace preview if encrypted + if paste['encrypted']: + paste['content'] = "[Encrypted]" + + recent_pastes.append(paste) i += 1 return recent_pastes @@ -40,7 +47,10 @@ def get_paste(unique_id, key=""): if file_path is not None: with open(file_path, "r") as fp: - paste = json.loads(fp.read()) + try: + paste = json.loads(fp.read()) + except json.JSONDecodeError: + return None # Check if paste is expired if common.is_expired(paste): diff --git a/requirements.txt b/requirements.txt index dad8d20..a73c186 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -flask +Flask==1.1.2 Flask-Limiter==1.4 -guesslang -cryptography \ No newline at end of file +guesslang==2.0.1 +cryptography==3.4.7 \ No newline at end of file