From 1742d56e8b090e6a7b51aed62972d78081f87e7d Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 20 Nov 2019 15:20:39 -0500 Subject: [PATCH 1/6] thumbnail: Don't log 200 responses --- src/classes/thumbnail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/thumbnail.py b/src/classes/thumbnail.py index b448a26628..7c3c187a72 100644 --- a/src/classes/thumbnail.py +++ b/src/classes/thumbnail.py @@ -112,7 +112,7 @@ def do_GET(self): # Path is expected to have 3 matched components (third is optional though) # /thumbnails/FILE-ID/FRAME-NUMBER/ or # /thumbnails/FILE-ID/FRAME-NUMBER/path/ - self.send_response(200) + self.send_response_only(200) else: self.send_error(404) return From 82863458700accf543ce2680cbf96a574ef8e8d2 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 20 Nov 2019 16:34:36 -0500 Subject: [PATCH 2/6] thumbnail: Name & lookup RE groups --- src/classes/thumbnail.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/classes/thumbnail.py b/src/classes/thumbnail.py index 7c3c187a72..d04e472691 100644 --- a/src/classes/thumbnail.py +++ b/src/classes/thumbnail.py @@ -36,7 +36,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer from socketserver import ThreadingMixIn -REGEX_THUMBNAIL_URL = re.compile(r"/thumbnails/(.+?)/(\d+)(/path/)?") +REGEX_THUMBNAIL_URL = re.compile(r"/thumbnails/(?P.+?)/(?P\d+)(?P/path/)?") def GenerateThumbnail(file_path, thumb_path, thumbnail_frame, width, height, mask, overlay): @@ -107,8 +107,8 @@ def do_GET(self): """ Process each GET request and return a value (image or file path)""" # Parse URL - url_output = REGEX_THUMBNAIL_URL.findall(self.path) - if url_output and len(url_output[0]) == 3: + url_output = REGEX_THUMBNAIL_URL.match(self.path) + if url_output and len(url_output.groups()) == 3: # Path is expected to have 3 matched components (third is optional though) # /thumbnails/FILE-ID/FRAME-NUMBER/ or # /thumbnails/FILE-ID/FRAME-NUMBER/path/ @@ -118,9 +118,9 @@ def do_GET(self): return # Get URL parts - file_id = url_output[0][0] - file_frame = int(url_output[0][1]) - only_path = url_output[0][2] + file_id = url_output.group('file_id') + file_frame = int(url_output.group('file_frame')) + only_path = url_output.group('only_path') # Catch undefined calls if file_id == "undefined": From 62cb4525f662b83119af22f9495a39b309d00296 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 20 Nov 2019 16:35:49 -0500 Subject: [PATCH 3/6] Don't create useless variable --- src/classes/thumbnail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/thumbnail.py b/src/classes/thumbnail.py index d04e472691..eb93419497 100644 --- a/src/classes/thumbnail.py +++ b/src/classes/thumbnail.py @@ -58,7 +58,7 @@ def GenerateThumbnail(file_path, thumb_path, thumbnail_frame, width, height, mas pass # Create thumbnail folder (if needed) - parent_path, file_name = os.path.split(thumb_path) + parent_path = os.path.dirname(thumb_path) if not os.path.exists(parent_path): os.mkdir(parent_path) From 9fb11f87596313d5a0bbbf397574b2d9eeba2e5f Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 20 Nov 2019 16:37:25 -0500 Subject: [PATCH 4/6] Look up ID earlier, error if not found --- src/classes/thumbnail.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/classes/thumbnail.py b/src/classes/thumbnail.py index eb93419497..cb0dd00e06 100644 --- a/src/classes/thumbnail.py +++ b/src/classes/thumbnail.py @@ -122,8 +122,14 @@ def do_GET(self): file_frame = int(url_output.group('file_frame')) only_path = url_output.group('only_path') - # Catch undefined calls - if file_id == "undefined": + try: + # Look up file data + file = File.get(id=file_id) + + # Ensure file location is an absolute path + file_path = file.absolute_path() + except AttributeError: + # Couldn't match file ID self.send_error(404) return @@ -137,18 +143,14 @@ def do_GET(self): # Locate thumbnail thumb_path = os.path.join(info.THUMBNAIL_PATH, file_id, "%s.png" % file_frame) if not os.path.exists(thumb_path) and file_frame == 1: - # Try with no frame # (for backwards compatibility) + # Try ID with no frame # (for backwards compatibility) thumb_path = os.path.join(info.THUMBNAIL_PATH, "%s.png" % file_id) if not os.path.exists(thumb_path) and file_frame != 1: - # Try with no frame # (for backwards compatibility) + # Try with ID and frame # in filename (for backwards compatibility) thumb_path = os.path.join(info.THUMBNAIL_PATH, "%s-%s.png" % (file_id, file_frame)) if not os.path.exists(thumb_path): # Generate thumbnail (since we can't find it) - file = File.get(id=file_id) - - # Convert path to the correct relative path (based on this folder) - file_path = file.absolute_path() # Determine if video overlay should be applied to thumbnail overlay_path = "" From bb3e8a1293a2603ea63e1a3c576a4b4978812740 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 20 Nov 2019 16:56:09 -0500 Subject: [PATCH 5/6] Add overrides for HTTP logging --- src/classes/thumbnail.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/classes/thumbnail.py b/src/classes/thumbnail.py index cb0dd00e06..5f59f3339b 100644 --- a/src/classes/thumbnail.py +++ b/src/classes/thumbnail.py @@ -33,6 +33,7 @@ from threading import Thread from classes import info from classes.query import File +from classes.logger import log from http.server import BaseHTTPRequestHandler, HTTPServer from socketserver import ThreadingMixIn @@ -54,7 +55,7 @@ def GenerateThumbnail(file_path, thumb_path, thumbnail_frame, width, height, mas try: if reader.info.metadata.count("rotate"): rotate = float(reader.info.metadata.find("rotate").value()[1]) - except: + except Exception: pass # Create thumbnail folder (if needed) @@ -99,6 +100,14 @@ def run(self): class httpThumbnailHandler(BaseHTTPRequestHandler): """ This class handles HTTP requests to the HTTP thumbnail server above.""" + def log_message(self, msg_format, *args): + """ Log message from HTTPServer """ + log.info(msg_format % args) + + def log_error(self, msg_format, *args): + """ Log error from HTTPServer """ + log.error(msg_format % args) + def do_GET(self): # Pause processing of request (since we don't currently use thread pooling, this allows # the threads to be processed without choking the CPU as much From 5a1419d52be760e50da65e27ea1b4a71ac72ec10 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 20 Nov 2019 17:12:13 -0500 Subject: [PATCH 6/6] thumbnail: Fix path for thumb output --- src/classes/thumbnail.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/classes/thumbnail.py b/src/classes/thumbnail.py index 5f59f3339b..4b2fcaf425 100644 --- a/src/classes/thumbnail.py +++ b/src/classes/thumbnail.py @@ -151,12 +151,16 @@ def do_GET(self): # Locate thumbnail thumb_path = os.path.join(info.THUMBNAIL_PATH, file_id, "%s.png" % file_frame) - if not os.path.exists(thumb_path) and file_frame == 1: - # Try ID with no frame # (for backwards compatibility) - thumb_path = os.path.join(info.THUMBNAIL_PATH, "%s.png" % file_id) - if not os.path.exists(thumb_path) and file_frame != 1: - # Try with ID and frame # in filename (for backwards compatibility) - thumb_path = os.path.join(info.THUMBNAIL_PATH, "%s-%s.png" % (file_id, file_frame)) + if not os.path.exists(thumb_path): + if file_frame == 1: + # Try ID with no frame # (for backwards compatibility) + alt_path = os.path.join(info.THUMBNAIL_PATH, "%s.png" % file_id) + else: + # Try with ID and frame # in filename (for backwards compatibility) + alt_path = os.path.join(info.THUMBNAIL_PATH, "%s-%s.png" % (file_id, file_frame)) + + if os.path.exists(alt_path): + thumb_path = alt_path if not os.path.exists(thumb_path): # Generate thumbnail (since we can't find it)