Skip to content

Commit 07d5469

Browse files
author
Halcyon
committed
fixed stuff. whatevre
1 parent 2bd7974 commit 07d5469

File tree

3 files changed

+85
-62
lines changed

3 files changed

+85
-62
lines changed

hanse_web.py

+72-31
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
from flask import Flask, request, send_from_directory, render_template, send_file
1+
from flask import Flask, request, send_from_directory, render_template, send_file, redirect
22
from werkzeug.contrib.cache import SimpleCache
33

44
app = Flask(__name__, static_url_path='')
55
cache = SimpleCache()
6-
6+
app_root = "hanseweb"
77
base_path = "images/"
88

99
import numpy as np
1010
from io import BytesIO
1111
import glob
12+
import requests
1213

1314
from AnsiGraphics import AnsiGraphics
1415
from AnsiImage import AnsiImage
@@ -20,50 +21,82 @@
2021
for i in range(16):
2122
col = np.array(np.floor(np.array(ansi_graphics.cga_colour(i)) * 255.0), 'int')
2223
pal_entry = ".fg" + str(i) + "{\n"
23-
pal_entry += " color: rgb(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ");\n"
24+
pal_entry += " color: rgba(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ", 0);\n"
2425
pal_entry += "}\n\n"
2526
pal_entry += ".bg" + str(i) + "{\n"
26-
pal_entry += " background: rgb(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ");\n"
27+
pal_entry += " background: rgba(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ", 0);\n"
2728
pal_entry += "}\n\n"
2829
pal_styles += pal_entry
2930

31+
def get_remote_file(url, max_size = 200*1024):
32+
r = requests.get(url, stream=True)
33+
r.raise_for_status()
34+
35+
if int(r.headers.get('Content-Length')) > max_size:
36+
raise ValueError('response too large')
3037

31-
@app.route('/')
38+
size = 0
39+
content = b""
40+
for chunk in r.iter_content(1024):
41+
size += len(chunk)
42+
if size > max_size:
43+
raise ValueError('response too large')
44+
content += chunk
45+
return content
46+
47+
def load_ansi(path):
48+
if ".." in path or path[0] == '/':
49+
raise(ValueError("dangerous."))
50+
51+
wide_mode = False
52+
if request.args.get('wide', False) != False:
53+
wide_mode = True
54+
55+
ansi_image = AnsiImage(ansi_graphics)
56+
ansi_image.clear_image(1, 1)
57+
58+
if path[0:4] == 'http':
59+
ansi_data = get_remote_file(path)
60+
ansi_image.parse_ans(ansi_data, wide_mode = wide_mode)
61+
else:
62+
ansi_image.load_ans(base_path + path, wide_mode = wide_mode)
63+
return ansi_image
64+
65+
@app.route('/', methods = ['GET', 'POST'])
3266
def file_list():
67+
if request.form.get('load_url', None) != None:
68+
return(redirect('/view/' + request.form.get('load_url', None), code=302))
69+
3370
file_list = list(glob.glob(base_path + '*.ans'))
3471
list_html = '<h1>Files</h1><div style="text-align: left; font-size: 28px; width: 600px;">'
3572
for ansi_file in file_list:
3673
ansi_file = ansi_file[len(base_path):]
37-
list_html += '--> <a href="/view/' + ansi_file + '">' + ansi_file + '</a><br/>'
38-
list_html += '<a href="/gallery">gallery</a></div>'
39-
return(render_template("default.html", title="files", content=list_html))
74+
list_html += '--> <a href="/' + app_root + '/view/' + ansi_file + '">' + ansi_file + '</a><br/>'
75+
list_html += '<a href="/' + app_root + '/gallery">gallery</a>'
76+
list_html += '<form method="post" action="/' + app_root + '">url: <input type="text" name="load_url" style="width: 600px;"></input> <input type="submit" value="load"></input></form></div>'
77+
return(render_template("default.html", title="files", content=list_html, app_root=app_root))
4078

4179
@app.route('/gallery')
4280
def gallery():
4381
file_list = list(glob.glob(base_path + '*.ans'))
4482
list_html = '<h1>Gallery</h1><div style="text-align: left; font-size: 28px;" class="gallery">'
4583
for ansi_file in file_list:
4684
ansi_file = ansi_file[len(base_path):]
47-
list_html += '<a href="/view/' + ansi_file + '"><img src="/image/' + ansi_file + '?thumb"></img></a>'
48-
list_html += '<a href="/"><-- back</a></div>'
49-
return(render_template("default.html", title="gallery", content=list_html))
85+
list_html += '<a href="/' + app_root + '/view/' + ansi_file + '"><img src="/' + app_root + '/image/' + ansi_file + '?thumb"></img></a>'
86+
list_html += '<a href="/' + app_root + '"><-- back</a></div>'
87+
return(render_template("default.html", title="gallery", content=list_html, app_root=app_root))
5088

5189
@app.route('/view/<path:path>')
5290
def render_ansi(path):
53-
wide_mode = False
54-
if request.args.get('wide', False) != False:
55-
wide_mode = True
56-
5791
try:
58-
ansi_image = AnsiImage(ansi_graphics)
59-
ansi_image.clear_image(80, 24)
60-
ansi_image.load_ans(base_path + path, wide_mode = wide_mode)
61-
width, height = ansi_image.get_size()
92+
ansi_image = load_ansi(path)
6293
except:
63-
return(render_template("default.html", title="Loading error, sorry."))
94+
return(render_template("default.html", title="Loading error, sorry.", content="<h3>Loading error, sorry</h3>", app_root = app_root))
95+
width, height = ansi_image.get_size()
6496

6597
html_ansi = ""
66-
html_ansi += '<div style="text-align: left; font-size: 28px;"><a href="/"><-- back</a></div>'
98+
html_ansi += '<div style="text-align: left; font-size: 28px;"><a href="/' + app_root + '"><-- back</a></div>'
99+
html_ansi += '<div style="display:inline-block; background:url(' + "'/" + app_root + '/image/' + path + "'" + ');">'
67100
for y in range(height):
68101
for x in range(width):
69102
char = ansi_image.get_cell(x, y)
@@ -73,18 +106,27 @@ def render_ansi(path):
73106
html_ansi += chr(char[0])
74107
html_ansi += '</span>'
75108
html_ansi += "\n"
76-
return(render_template("default.html", title=path, styles=pal_styles, content=html_ansi, show_dl=True))
109+
html_ansi += '</div>'
110+
return(render_template("default.html", title=path, styles=pal_styles, content=html_ansi, show_dl=True, app_root=app_root))
111+
112+
@app.route('/ansi/<path:path>')
113+
def send_ansi(path):
114+
try:
115+
ansi_image = load_ansi(path)
116+
except:
117+
return(render_template("default.html", title="Loading error, sorry.", content="<h3>Loading error, sorry</h3>", app_root=app_root))
118+
img_io = BytesIO()
119+
img_io.write(ansi_image.to_ans())
120+
img_io.seek(0)
121+
return send_file(img_io, mimetype='plain/text')
77122

78123
@app.route('/image/<path:path>')
79124
def render_ansi_png(path):
80125
transparent = False
81-
wide_mode = False
82126
thumb = False
83127

84128
if request.args.get('transparent', None) != None:
85129
transparent = True
86-
if request.args.get('wide', None) != None:
87-
wide_mode = True
88130
if request.args.get('thumb', None) != None:
89131
thumb = True
90132

@@ -93,14 +135,13 @@ def render_ansi_png(path):
93135
bitmap = cached
94136
else:
95137
try:
96-
ansi_image = AnsiImage(ansi_graphics)
97-
ansi_image.clear_image(1000, 1000)
98-
ansi_image.load_ans(base_path + path, wide_mode = wide_mode)
99-
bitmap = ansi_image.to_bitmap(transparent = transparent)
138+
ansi_image = load_ansi(path)
100139
except:
101-
return(render_template("default.html", title="Loading error, sorry."))
140+
return(render_template("default.html", title="Loading error, sorry.", content="<h3>Loading error, sorry</h3>", app_root=app_root))
141+
142+
bitmap = ansi_image.to_bitmap(transparent = transparent)
102143
if thumb == True:
103-
bitmap.thumbnail((128, 128))
144+
bitmap.thumbnail((256, 256))
104145
cache.set(request.url, bitmap)
105146

106147
img_io = BytesIO()

0 commit comments

Comments
 (0)