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
2
2
from werkzeug .contrib .cache import SimpleCache
3
3
4
4
app = Flask (__name__ , static_url_path = '' )
5
5
cache = SimpleCache ()
6
-
6
+ app_root = "hanseweb"
7
7
base_path = "images/"
8
8
9
9
import numpy as np
10
10
from io import BytesIO
11
11
import glob
12
+ import requests
12
13
13
14
from AnsiGraphics import AnsiGraphics
14
15
from AnsiImage import AnsiImage
20
21
for i in range (16 ):
21
22
col = np .array (np .floor (np .array (ansi_graphics .cga_colour (i )) * 255.0 ), 'int' )
22
23
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 "
24
25
pal_entry += "}\n \n "
25
26
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 "
27
28
pal_entry += "}\n \n "
28
29
pal_styles += pal_entry
29
30
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' )
30
37
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' ])
32
66
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
+
33
70
file_list = list (glob .glob (base_path + '*.ans' ))
34
71
list_html = '<h1>Files</h1><div style="text-align: left; font-size: 28px; width: 600px;">'
35
72
for ansi_file in file_list :
36
73
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 ))
40
78
41
79
@app .route ('/gallery' )
42
80
def gallery ():
43
81
file_list = list (glob .glob (base_path + '*.ans' ))
44
82
list_html = '<h1>Gallery</h1><div style="text-align: left; font-size: 28px;" class="gallery">'
45
83
for ansi_file in file_list :
46
84
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 ))
50
88
51
89
@app .route ('/view/<path:path>' )
52
90
def render_ansi (path ):
53
- wide_mode = False
54
- if request .args .get ('wide' , False ) != False :
55
- wide_mode = True
56
-
57
91
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 )
62
93
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 ()
64
96
65
97
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 + "'" + ');">'
67
100
for y in range (height ):
68
101
for x in range (width ):
69
102
char = ansi_image .get_cell (x , y )
@@ -73,18 +106,27 @@ def render_ansi(path):
73
106
html_ansi += chr (char [0 ])
74
107
html_ansi += '</span>'
75
108
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' )
77
122
78
123
@app .route ('/image/<path:path>' )
79
124
def render_ansi_png (path ):
80
125
transparent = False
81
- wide_mode = False
82
126
thumb = False
83
127
84
128
if request .args .get ('transparent' , None ) != None :
85
129
transparent = True
86
- if request .args .get ('wide' , None ) != None :
87
- wide_mode = True
88
130
if request .args .get ('thumb' , None ) != None :
89
131
thumb = True
90
132
@@ -93,14 +135,13 @@ def render_ansi_png(path):
93
135
bitmap = cached
94
136
else :
95
137
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 )
100
139
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 )
102
143
if thumb == True :
103
- bitmap .thumbnail ((128 , 128 ))
144
+ bitmap .thumbnail ((256 , 256 ))
104
145
cache .set (request .url , bitmap )
105
146
106
147
img_io = BytesIO ()
0 commit comments