瀏覽代碼

Updated: images served as images.

We now also handle 404.  So if they
get off the path, we catch them and
put them back on it.
Steve Thielemann 4 年之前
父節點
當前提交
3a8d47eaa4
共有 3 個文件被更改,包括 57 次插入5 次删除
  1. 47 4
      messages.py
  2. 9 0
      templates/404.html
  3. 1 1
      templates/message.html

+ 47 - 4
messages.py

@@ -1,4 +1,4 @@
-from flask import Flask, render_template
+from flask import Flask, render_template, make_response
 from flask_paginate import Pagination, get_page_parameter, get_page_args
 from flask_caching import Cache
 from flask import request
@@ -21,25 +21,41 @@ def format_datetime(value):
 # Check Configuring Flask-Caching section for more details
 # cache = Cache(app, config={"CACHE_TYPE": "filesystem", "CACHE_DIR": "cache"})
 cache = Cache(app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "redis"})
+# cache = Cache(app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "olympus"})
 
 import jammin
 
-bases = {"FSX_General": "msgs/fsx_gen", "FSX_BBS": "msgs/fsx_bbs", "FSX_BOT": "msgs/fsx_bot", "FSX_Magicka": "msgs/fsx_mag", "HappyNet": "msgs/hpy_gen"}
+bases = {
+    "FSX_General": "msgs/fsx_gen",
+    "FSX_BBS": "msgs/fsx_bbs",
+    "FSX_BOT": "msgs/fsx_bot",
+    "FSX_Magicka": "msgs/fsx_mag",
+    "HappyNet": "msgs/hpy_gen",
+}
 
+# bases = {"FSX_BOT": "fsx_bot"}
 
 # @cache.memoize(timeout=5 * 60, key_prefix="messages")
 
+
 @cache.memoize(timeout=5 * 60)
 def get_messages(base):
     messages = jammin.get_messages(base)
     messages.reverse()
     return messages
 
+
 @cache.memoize(timeout=60)
 def get_message(base, msgno):
     message = jammin.read_message(base, msgno)
     return message
 
+
[email protected](404)
+def not_found(e):
+    return render_template("404.html")
+
+
 @app.route(base_path + "/list")
 def list_bases():
     return render_template(
@@ -86,6 +102,7 @@ def display_messages(area):
     )
 
 
[email protected](timeout=60)
 def ansi_to_png(raw_ansi_bytes, idx):
     pid = os.getppid()
     ansifile = "{0}-{1}.ans".format(idx, pid)
@@ -97,9 +114,29 @@ def ansi_to_png(raw_ansi_bytes, idx):
         png = fp.read()
     os.unlink(ansifile)
     os.unlink(pngfile)
+    return png
+
+
+def ansi_to_png64(raw_ansi_bytes, idx):
+    png = ansi_to_png(raw_ansi_bytes, idx)
     return base64.b64encode(png).decode("utf-8")
 
 
[email protected](base_path + "/image/<area>/<int:msgno>.png")
+def display_ansi(area, msgno):
+    if area not in bases:
+        return "RATS", 404
+    message = get_message(bases[area], msgno)
+    if not message:
+        return "RATS", 404
+    if not "text" in message:
+        return "RATS", 404
+    png = ansi_to_png(message["bytes"].replace(b"\r", b"\n"), msgno)
+    response = make_response(png)
+    response.headers.set("Content-Type", "image/png")
+    return response
+
+
 # <img alt="My Image" src="data:image/png;base64,
 @app.route(base_path + "/read/<area>/<int:msgno>")
 def display_message(area, msgno):
@@ -113,13 +150,19 @@ def display_message(area, msgno):
 
     if not message:
         return render_template(
-            "missing-message.html", base_path=base_path, area=area, title="Missing Message"
+            "missing-message.html",
+            base_path=base_path,
+            area=area,
+            title="Missing Message",
         )
 
     if "text" in message:
         if "\x1b" in message["text"]:
             # Ok, the message contains ANSI CODES -- Convert
-            message["png"] = ansi_to_png(message["bytes"].replace(b"\r", b"\n"), msgno)
+            message["png"] = True
+            # message["png"] = ansi_to_png64(
+            #     message["bytes"].replace(b"\r", b"\n"), msgno
+            # )
         else:
             message["text"] = message["text"].replace("\r", "\n")  # <br >\n")
 

+ 9 - 0
templates/404.html

@@ -0,0 +1,9 @@
+{% extends 'base.html' %}
+
+{% block header %}
+<h1>Sorry, what?</h1>
+{% endblock %}
+
+{% block content %}
+<div><a href="{{ url_for('list_bases') }}">Return to Message Base Lists</a></div>
+{% endblock %}

+ 1 - 1
templates/message.html

@@ -37,7 +37,7 @@
 <div class="grid-x padding-x">
 <div class="large-2 cell">Text:</div>
 <div class="large-10 cell">{% if message.png is defined %}
-  <img alt="Message {{ message.MsgNum }}" src="data:image/png;base64,{{ message.png|safe }}">{% else %}
+  <img alt="Message {{ message.MsgNum }}" src="{{ url_for('display_ansi', area=area, msgno=message.MsgNum ) }}">{% else %}
   <pre class="oldstyle">{{ message.text }}</pre>{% endif %}
 </div>