forked from ctb/cse491-serverz
/
imageapp.py
135 lines (108 loc) · 4.33 KB
/
imageapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import urlparse
import jinja2
import cgi
import time
import StringIO
import Cookie
import sqlite3
from app import render_template, fileData
def image_app(environ, start_response):
method = environ['REQUEST_METHOD']
path = environ['PATH_INFO']
query_string = environ['QUERY_STRING']
redirect = False
# Set up jinja2
loader = jinja2.FileSystemLoader('./templates')
env = jinja2.Environment(loader=loader)
vars = dict()
if path == '/image_upload':
# Get content
content_type = environ['CONTENT_TYPE']
content_length = int(environ['CONTENT_LENGTH'])
content = environ['wsgi.input'].read(content_length)
headers = {}
for key, val in environ.iteritems():
headers[key.lower().replace('_', '-')] = val
fs = cgi.FieldStorage(fp=StringIO.StringIO(content),
headers = headers, environ = environ)
filename = fs['file'].filename
image_type = filename.split('.')[-1]
if image_type in {'png', 'jpg', 'tiff', 'jpeg'}:
# connect to the already existing database
db = sqlite3.connect('images.sqlite')
# configure to allow binary insertions
db.text_factory = bytes
# data to be inserted into database
r = fs['file'].value
f = fs['file'].filename
d = fs['description'].value
u = 1
# insert
db.execute('INSERT INTO image_store (image, name, description, user_id) VALUES (?, ?, ?, ?)', (r, f, d, u))
db.commit()
start_response('302 Moved Temporarily',
[('Content-type', 'text/plain'),
('Location', '/')])
redirect = True
if path == '/':
db = sqlite3.connect('images.sqlite')
# configure to retrieve bytes, not text
db.text_factory = bytes
# get a query handle (or "cursor")
c = db.cursor()
# select all of the images
c.execute('SELECT iid, name, description, user_id FROM image_store ORDER BY iid DESC LIMIT 1')
iid, name, description, user_id = c.fetchone()
vars['name'] = name
vars['description'] = description
vars['time'] = time.time()
start_response('200 OK', [('Content-type', 'text/html')])
ret = render_template(env, 'imageapp.html', vars)
elif path == 'imageapp_list':
db = sqlite3.connect('images.sqlite')
db.text_factory = bytes
c = db.cursor()
c.execute('SELECT iid, name, description, user_id FROM image_store ORDER BY iid DESC LIMIT 1')
iid, name, description, user_id = c.fetchone()
vars['iid'] = iid
vars['name'] = name
vars['description'] = description
vars['time'] = time.time()
start_response('200 OK', [('Content-type', 'text/html')])
ret = render_template(env, 'imageapp.html', vars)
elif path.startswith('/latest_image'):
db = sqlite3.connect('images.sqlite')
# configure to retrieve bytes, not text
db.text_factory = bytes
# get a query handle (or "cursor")
c = db.cursor()
# select all of the images
c.execute('SELECT iid FROM image_store ORDER BY iid DESC LIMIT 1')
iid, = c.fetchone()
start_response('302 Moved Temporarily',
[('Content-type', 'text/plain'),
('Location', '/image_raw/' + str(iid))])
redirect = True
elif path.startswith('/image_raw/'):
image_no = path.lstrip('/image_raw/')
db = sqlite3.connect('images.sqlite')
# configure to retrieve bytes, not text
db.text_factory = bytes
# get a query handle (or "cursor")
c = db.cursor()
# select image with iid=image_no
c.execute('SELECT image, name FROM image_store WHERE iid=?', (image_no,))
image, name = c.fetchone()
start_response('200 OK', [('Content-type',
'image/' + name.split('.')[-1])])
ret = image
else:
start_response('200 OK', [('Content-type', 'text/html')])
ret = render_template(env, '404.html', vars)
# Needs to be a single-entry list for wsgi compliance
if redirect:
return ['']
else:
return [ret]
def make_image_app():
return image_app