From 28181428beb43ac0d9c2ed8e5e9edd6789b71d97 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 22 Sep 2020 02:54:26 -0700 Subject: [PATCH] Synchronize Etiquette and YCDL. --- etiquette/photodb.py | 3 +- frontends/etiquette_flask/backend/common.py | 26 +++++++++----- .../etiquette_flask/backend/jinja_filters.py | 2 ++ .../etiquette_flask/etiquette_flask_launch.py | 36 ++++++++++--------- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index f7825e6..efae72a 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -1710,7 +1710,8 @@ class PhotoDB( existing_database = self.database_filepath.exists if not existing_database and not create: - raise FileNotFoundError(f'"{self.data_directory}" does not exist and create is off.') + msg = f'"{self.database_filepath.absolute_path}" does not exist and create is off.' + raise FileNotFoundError(msg) os.makedirs(self.data_directory.absolute_path, exist_ok=True) self.sql = sqlite3.connect(self.database_filepath.absolute_path) diff --git a/frontends/etiquette_flask/backend/common.py b/frontends/etiquette_flask/backend/common.py index 946f3cd..a4b673c 100644 --- a/frontends/etiquette_flask/backend/common.py +++ b/frontends/etiquette_flask/backend/common.py @@ -1,3 +1,7 @@ +''' +Do not execute this file directly. +Use etiquette_flask_launch.py to start the server with gevent. +''' import flask; from flask import request import gzip import io @@ -15,7 +19,7 @@ from . import jinja_filters from . import jsonify from . import sessions -# Runtime init ##################################################################################### +# Flask init ####################################################################################### root_dir = pathclass.Path(__file__).parent.parent @@ -39,8 +43,6 @@ site.jinja_env.lstrip_blocks = True jinja_filters.register_all(site) site.debug = True -P = etiquette.photodb.PhotoDB() - session_manager = sessions.SessionManager(maxlen=10000) file_cache_manager = caching.FileCacheManager( maxlen=10000, @@ -86,7 +88,7 @@ def after_request(response): bail = bail or response.status_code < 200 bail = bail or response.status_code >= 300 bail = bail or response.direct_passthrough - bail = bail or int(response.headers.get('Content-Length', gzip_minimum_size)) > gzip_maximum_size + bail = bail or int(response.headers.get('Content-Length', 0)) > gzip_maximum_size bail = bail or len(response.get_data()) < gzip_minimum_size bail = bail or 'gzip' not in accept_encoding.lower() bail = bail or 'Content-Encoding' in response.headers @@ -208,6 +210,8 @@ def send_file(filepath, override_mimetype=None): if not filepath.is_file: flask.abort(404) + file_size = filepath.size + headers = file_cache_manager.matches(request=request, filepath=filepath) if headers: response = flask.Response(status=304, headers=headers) @@ -240,21 +244,21 @@ def send_file(filepath, override_mimetype=None): if range_min is None: range_min = 0 if range_max is None: - range_max = filepath.size + range_max = file_size # because ranges are 0-indexed - range_max = min(range_max, filepath.size - 1) + range_max = min(range_max, file_size - 1) range_min = max(range_min, 0) range_header = 'bytes {min}-{max}/{outof}'.format( min=range_min, max=range_max, - outof=filepath.size, + outof=file_size, ) outgoing_headers['Content-Range'] = range_header status = 206 else: - range_max = filepath.size - 1 + range_max = file_size - 1 range_min = 0 status = 200 @@ -280,3 +284,9 @@ def send_file(filepath, override_mimetype=None): headers=outgoing_headers, ) return response + +#################################################################################################### + +def init_photodb(*args, **kwargs): + global P + P = etiquette.photodb.PhotoDB(*args, **kwargs) diff --git a/frontends/etiquette_flask/backend/jinja_filters.py b/frontends/etiquette_flask/backend/jinja_filters.py index 3a727a9..7142a03 100644 --- a/frontends/etiquette_flask/backend/jinja_filters.py +++ b/frontends/etiquette_flask/backend/jinja_filters.py @@ -3,6 +3,8 @@ import jinja2.filters import voussoirkit.bytestring +#################################################################################################### + filter_functions = [] global_functions = [] diff --git a/frontends/etiquette_flask/etiquette_flask_launch.py b/frontends/etiquette_flask/etiquette_flask_launch.py index fd139f3..078c2f7 100644 --- a/frontends/etiquette_flask/etiquette_flask_launch.py +++ b/frontends/etiquette_flask/etiquette_flask_launch.py @@ -16,51 +16,55 @@ import argparse import gevent.pywsgi import sys +from voussoirkit import pathclass + import etiquette_flask_entrypoint -site = etiquette_flask_entrypoint.site - -def run(port=None, use_https=None): - if port is None: - port = 5000 - else: - port = int(port) +HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https') +def etiquette_flask_launch(create, port, use_https): if use_https is None: use_https = port == 443 if use_https: http = gevent.pywsgi.WSGIServer( listener=('0.0.0.0', port), - application=site, - keyfile='D:\\git\\etiquette\\frontends\\etiquette_flask\\https\\etiquette.key', - certfile='D:\\git\\etiquette\\frontends\\etiquette_flask\\https\\etiquette.crt', + application=etiquette_flask_entrypoint.site, + keyfile=HTTPS_DIR.with_child('etiquette.key').absolute_path, + certfile=HTTPS_DIR.with_child('etiquette.crt').absolute_path, ) else: http = gevent.pywsgi.WSGIServer( listener=('0.0.0.0', port), - application=site, + application=etiquette_flask_entrypoint.site, ) + etiquette_flask_entrypoint.backend.common.init_photodb(create=create) + message = f'Starting server on port {port}' if use_https: message += ' (https)' print(message) + try: http.serve_forever() except KeyboardInterrupt: pass - return 0 -def run_argparse(args): - return run(port=args.port, use_https=args.use_https) +def etiquette_flask_launch_argparse(args): + return etiquette_flask_launch( + create=args.create, + port=args.port, + use_https=args.use_https, + ) def main(argv): parser = argparse.ArgumentParser() - parser.add_argument(dest='port', nargs='?', default=None) + parser.add_argument('port', nargs='?', type=int, default=5000) + parser.add_argument('--dont_create', '--dont-create', '--no-create', dest='create', action='store_false', default=True) parser.add_argument('--https', dest='use_https', action='store_true', default=None) - parser.set_defaults(func=run_argparse) + parser.set_defaults(func=etiquette_flask_launch_argparse) args = parser.parse_args(argv) return args.func(args)