-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpypatho.py
180 lines (147 loc) · 5.43 KB
/
pypatho.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Pathology image analizer webservice
by Alvaro G."""
from tornado.options import options
import tornado.web
import tornado.gen
import tornado.httpserver
import tornado.ioloop
from swiftclient import Connection
# Sin usar ?
# from PIL import Image
# from skimage import io
import pymongo
import datetime
import os
import imghdr
from hashlib import md5
# Internal Functions Come Now...
import opts
# How about some processing algorithms?
import processors
# Functions
# def isalreadythere(filehash):
# pass
# Handlers
class BaseHandler(tornado.web.RequestHandler):
"""Base handler for all other methods"""
pass
class TestingHandler(BaseHandler):
"""Fake testing handler, probably will get deleted"""
pass
class HomeHandler(BaseHandler):
"""Serves the home page"""
def get(self):
self.render("index.html")
class HashHandler(BaseHandler):
"""Check the existence of a certain file on the database, via its hash"""
def get(self, filehash):
coll = self.application.mongodb.files
files_doc = coll.find_one({"_id": filehash})
if files_doc:
# Borramos la id para serializar?
# del files_doc["_id"]
# self.write(files_doc)
self.write("OK")
else:
self.set_status(404)
self.write({"error": "Image not found!"})
class PostFileHandler(BaseHandler):
"""Receives a file via POST method"""
def post(self):
# Pending use of apikey
# apikey = self.get_argument('apikey')
file1 = self.request.files['file1'][0]
# original_fname = file1['filename']
hashresult = md5(file1['body']).hexdigest()
if imghdr.what(file1, file1['body']) != 'jpeg':
self.write('No JPEG! BAD!')
else:
self.write(hashresult)
self.application.swift.put_object(container=options.container_name,
obj=hashresult + '.jpg',
contents=file1['body'],
content_type='image/jpeg')
coll = self.application.mongodb.files
files_doc = {'_id': hashresult,
'datetime': datetime.datetime.now()}
coll.insert(files_doc)
class GetFileHandler(BaseHandler):
"""Serves file via a GET method"""
def get(self, filehash):
coll = self.application.mongodb.files
files_doc = coll.find_one({"_id": filehash})
if files_doc:
imagefile = self.application.swift.get_object(
container=options.container_name,
obj=filehash + '.jpg')[1]
self.set_header("Content-Type", 'image/jpeg')
self.write(imagefile)
else:
self.write('MAAAAL')
class ProcessFileHandler(BaseHandler):
"""Asks for processing certain file"""
def get(self, process, filehash):
# TODO: Guardar los PNG procesados y 'avisar' a la base de datos
coll = self.application.mongodb.files
files_doc = coll.find_one({"_id": filehash})
if files_doc:
target = self.application.swift.get_object(
container=options.container_name,
obj=filehash + '.jpg')[1]
processed = processors.processor(process, target)
self.set_header("Content-Type", 'image/png')
self.write(processed)
else:
self.write('no existe' + filehash)
class ListImagesHandler(BaseHandler):
"""List images in database"""
def get(self, page):
page = int(page) if page else '0'
pagination = 20
pagebegin = page * pagination
coll = self.application.mongodb.files
files_doc = coll.find().limit(pagination).skip(int(pagebegin))
self.render(
"list.html",
title="Pypatho - Processed file list",
# header = "Processed files",
images=files_doc
)
# App
class Application(tornado.web.Application):
"""Application initialization"""
def __init__(self):
handlers = [
(r"/", HomeHandler),
(r"/list(?:/([0-9]*))?", ListImagesHandler),
(r"/api/v1/hash/check/([a-fA-F\d]{32})", HashHandler),
# (r"/api/hash/check/(\w+)", HashHandler),
(r"/api/v1/file/post", PostFileHandler),
(r"/api/v1/file/get/([a-fA-F\d]{32}).jpg", GetFileHandler),
(r"/api/v1/file/process/([\d]{2})/([a-fA-F\d]{32}).png",
ProcessFileHandler),
(r"/testing/", TestingHandler),
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
)
tornado.web.Application.__init__(self, handlers, **settings)
conn = pymongo.Connection(options.dburl)
self.mongodb = conn.pypatho
self.swift = Connection(
authurl=options.endpoint + "/auth/v1.0",
user=options.storageuser,
key=options.storagekey,
auth_version="1",
insecure=True)
def main():
"""Main thread"""
tornado.options.parse_command_line()
print 'Serving on ' + str(options.port) + '...'
# TODO Chequear que todas las rutas existen
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()