Skip to content

Commit

Permalink
Merge pull request OpenShot#3087 from ferdnyc/thumb-server-nolog
Browse files Browse the repository at this point in the history
Thumbnail server: Some fixes
  • Loading branch information
ferdnyc authored Nov 23, 2019
2 parents 5ca13a8 + 5a1419d commit 2ad1854
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions src/classes/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
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

REGEX_THUMBNAIL_URL = re.compile(r"/thumbnails/(.+?)/(\d+)(/path/)?")
REGEX_THUMBNAIL_URL = re.compile(r"/thumbnails/(?P<file_id>.+?)/(?P<file_frame>\d+)(?P<only_path>/path/)?")


def GenerateThumbnail(file_path, thumb_path, thumbnail_frame, width, height, mask, overlay):
Expand All @@ -54,11 +55,11 @@ 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)
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)

Expand Down Expand Up @@ -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
Expand All @@ -107,23 +116,29 @@ 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/
self.send_response(200)
self.send_response_only(200)
else:
self.send_error(404)
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')

try:
# Look up file data
file = File.get(id=file_id)

# Catch undefined calls
if file_id == "undefined":
# Ensure file location is an absolute path
file_path = file.absolute_path()
except AttributeError:
# Couldn't match file ID
self.send_error(404)
return

Expand All @@ -136,19 +151,19 @@ 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)
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)
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)
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 = ""
Expand Down

0 comments on commit 2ad1854

Please sign in to comment.