don't block db while serving images

This commit is contained in:
j 2019-01-30 18:00:58 +05:30
parent f24e095987
commit 0234b8dffb

View file

@ -121,55 +121,56 @@ class Handler(http.server.SimpleHTTPRequestHandler):
else: else:
id = None id = None
if id and len(id) == 32 and id.isalnum(): if id and len(id) == 32 and id.isalnum():
path = None
data = None
with db.session(): with db.session():
if preview: if preview:
from item.icons import get_icon_sync from item.icons import get_icon_sync
try: try:
data = get_icon_sync(id, 'preview', 512) content = get_icon_sync(id, 'preview', 512)
except: except:
data = None content = None
if data: if content:
self.send_response(200, 'ok') self.send_response(200, 'ok')
self.send_header('Content-type', 'image/jpg') mimetype = 'image/jpg'
else: else:
self.send_response(404, 'Not Found') self.send_response(404, 'Not Found')
self.send_header('Content-type', 'text/plain') content = b'404 - Not Found'
data = b'404 - Not Found' mimetype = 'text/plain'
content_length = len(data)
self.send_header('Content-Length', str(content_length))
self.end_headers()
self.write_with_limit(data, content_length)
return
file = item.models.File.get(id)
if not file:
self.send_response(404, 'Not Found')
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b'404 - Not Found')
return
path = file.fullpath()
mimetype = {
'epub': 'application/epub+zip',
'pdf': 'application/pdf',
'txt': 'text/plain',
}.get(path.split('.')[-1], None)
self.send_response(200, 'OK')
self.send_header('Content-Type', mimetype)
self.send_header('X-Node-Protocol', settings.NODE_PROTOCOL)
if mimetype == 'text/plain':
with open(path, 'rb') as f:
content = f.read()
content = self.gzip_data(content)
content_length = len(content)
else: else:
content = None file = item.models.File.get(id)
content_length = os.path.getsize(path) if file:
self.send_header('Content-Length', str(content_length)) path = file.fullpath()
self.end_headers() mimetype = {
if content: 'epub': 'application/epub+zip',
self.write_with_limit(content, content_length) 'pdf': 'application/pdf',
else: 'txt': 'text/plain',
self.write_file_with_limit(path, content_length) }.get(path.split('.')[-1], None)
self.send_response(200, 'OK')
else:
self.send_response(404, 'Not Found')
content = b'404 - Not Found'
mimetype = 'text/plain'
self.send_header('Content-Type', mimetype)
self.send_header('X-Node-Protocol', settings.NODE_PROTOCOL)
if mimetype == 'text/plain' and path:
with open(path, 'rb') as f:
content = f.read()
content = self.gzip_data(content)
content_length = len(content)
elif path:
content = None
content_length = os.path.getsize(path)
elif content:
content_length = len(content)
else:
content_length = 0
self.send_header('Content-Length', str(content_length))
self.end_headers()
if content:
self.write_with_limit(content, content_length)
elif path:
self.write_file_with_limit(path, content_length)
elif len(parts) == 2 and parts[1] == 'log': elif len(parts) == 2 and parts[1] == 'log':
self._changelog() self._changelog()
else: else: