Bugfix: json parsing error handling

pull/12/head 0.3.1
Cesura 4 years ago
parent d75ade2401
commit 683143d7ad

@ -6,7 +6,7 @@ from os import environ
from distutils.util import strtobool from distutils.util import strtobool
from threading import Thread from threading import Thread
pastey_version = "0.3" pastey_version = "0.3.1"
loaded_config = {} loaded_config = {}
loaded_themes = [] loaded_themes = []
@ -45,6 +45,7 @@ if __name__ == "__main__":
print("Pastey version ", pastey_version) print("Pastey version ", pastey_version)
print("USING THE FOLLOWING CONFIGURATION:") print("USING THE FOLLOWING CONFIGURATION:")
print("=====================================") print("=====================================")
loaded_config['pastey_version'] = pastey_version
for option in dir(config): for option in dir(config):
if not option.startswith("__"): if not option.startswith("__"):
loaded_config[option] = eval("config.%s" % option) loaded_config[option] = eval("config.%s" % option)

@ -18,18 +18,25 @@ def get_recent(limit=config.recent_pastes):
recent_pastes = [] recent_pastes = []
i = 0 i = 0
while i < limit and i < len(paths): while i < limit and i < len(paths):
with open(paths[i]) as fp: if paths[i].is_file():
paste = json.loads(fp.read()) with open(paths[i]) as fp:
try:
basename = path.basename(paths[i]) paste = json.loads(fp.read())
paste['unique_id'] = basename[:-8] if basename.endswith(".expires") else basename except json.JSONDecodeError:
paste['content'] = '\n'.join(paste['content'].splitlines()[0:10]) i += 1
paste['icon'] = common.get_icon(paste['language']) continue
if paste['encrypted']: # Set extra metadata
paste['content'] = "[Encrypted]" basename = path.basename(paths[i])
paste['unique_id'] = basename[:-8] if basename.endswith(".expires") else basename
recent_pastes.append(paste) 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 i += 1
return recent_pastes return recent_pastes
@ -40,7 +47,10 @@ def get_paste(unique_id, key=""):
if file_path is not None: if file_path is not None:
with open(file_path, "r") as fp: 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 # Check if paste is expired
if common.is_expired(paste): if common.is_expired(paste):

@ -1,4 +1,4 @@
flask Flask==1.1.2
Flask-Limiter==1.4 Flask-Limiter==1.4
guesslang guesslang==2.0.1
cryptography cryptography==3.4.7
Loading…
Cancel
Save