From 2805900e8fa008936f996c5986a9a1f31c6c0f50 Mon Sep 17 00:00:00 2001 From: mjfernez Date: Thu, 3 Feb 2022 00:31:01 -0500 Subject: Fix 500 error for missing files Instead, entering an invalid path will yield a 404 page as expected. The error was introduced by adding the file_last_modified check before confirming in the file exists in the render_file function --- views.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/views.py b/views.py index 4a41248..3393cb2 100644 --- a/views.py +++ b/views.py @@ -3,7 +3,7 @@ views.py - defines the logic that generates views that a user sees when browsing to certain pages """ import os -from flask import request, send_from_directory, abort +from flask import send_from_directory, abort from flask import render_template, render_template_string, make_response from siteconfig import siteconfig from server import app, cache @@ -49,16 +49,17 @@ def render_file(path): """ if is_hidden_path(path): abort(404) + abs_path = siteconfig.BASE_DIR + path context = default_context() - context.update( - { - 'title': path.split('.')[0].upper(), - 'parent_dir': '/' + '/'.join(path.split('/')[:-1]), - 'last_update': file_last_modified(abs_path), - } - ) if os.path.isfile(abs_path): + context.update( + { + 'title': path.split('.')[0].upper(), + 'parent_dir': '/' + '/'.join(path.split('/')[:-1]), + 'last_update': file_last_modified(abs_path) + } + ) if abs_path.endswith('.html'): with open(abs_path, 'rb') as f: content = f.read().decode("UTF-8") @@ -81,6 +82,8 @@ def render_file(path): dirs, files, links, description = index_dir(abs_path) context.update( { + 'title': path.split('.')[0].upper(), + 'parent_dir': '/' + '/'.join(path.split('/')[:-1]), 'cur_path': path, 'cur_dir': path.split('/')[-1] + '/', 'dirs': dirs, -- cgit v1.2.3