compress txt files
This commit is contained in:
parent
28bbad1586
commit
5a5b5baf7b
1 changed files with 47 additions and 21 deletions
|
@ -124,13 +124,6 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
id = None
|
id = None
|
||||||
if id and len(id) == 32 and id.isalnum():
|
if id and len(id) == 32 and id.isalnum():
|
||||||
with db.session():
|
with db.session():
|
||||||
i = item.models.Item.get(id)
|
|
||||||
if not i:
|
|
||||||
self.send_response(404, 'Not Found')
|
|
||||||
self.send_header('Content-type', 'text/plain')
|
|
||||||
self.end_headers()
|
|
||||||
self.wfile.write(b'404 - Not Found')
|
|
||||||
return
|
|
||||||
if preview:
|
if preview:
|
||||||
from item.icons import get_icon_sync
|
from item.icons import get_icon_sync
|
||||||
try:
|
try:
|
||||||
|
@ -149,7 +142,14 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
# FIXME: also check for limit here
|
# FIXME: also check for limit here
|
||||||
self.wfile.write(data)
|
self.wfile.write(data)
|
||||||
return
|
return
|
||||||
path = i.get_path()
|
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 = {
|
mimetype = {
|
||||||
'epub': 'application/epub+zip',
|
'epub': 'application/epub+zip',
|
||||||
'pdf': 'application/pdf',
|
'pdf': 'application/pdf',
|
||||||
|
@ -158,20 +158,20 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
self.send_response(200, 'OK')
|
self.send_response(200, 'OK')
|
||||||
self.send_header('Content-Type', mimetype)
|
self.send_header('Content-Type', mimetype)
|
||||||
self.send_header('X-Node-Protocol', settings.NODE_PROTOCOL)
|
self.send_header('X-Node-Protocol', settings.NODE_PROTOCOL)
|
||||||
self.send_header('Content-Length', str(os.path.getsize(path)))
|
if mimetype == 'text/plain':
|
||||||
self.end_headers()
|
|
||||||
chunk_size = 16*1024
|
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
size = 0
|
content = f.read()
|
||||||
while 1:
|
content = self.gzip_data(content)
|
||||||
data = f.read(chunk_size)
|
content_length = len(content)
|
||||||
if not data:
|
else:
|
||||||
break
|
content = None
|
||||||
size += len(data)
|
content_length = os.path.getsize(path)
|
||||||
self.wfile.write(data)
|
self.send_header('Content-Length', str(content_length))
|
||||||
if state.bandwidth:
|
self.end_headers()
|
||||||
while not state.bandwidth.upload(chunk_size) and self.server._running:
|
if content:
|
||||||
time.sleep(0.1)
|
self.write_with_limit(content, content_length)
|
||||||
|
else:
|
||||||
|
self.write_file_with_limit(path, content_length)
|
||||||
else:
|
else:
|
||||||
self.send_response(200, 'OK')
|
self.send_response(200, 'OK')
|
||||||
self.send_header('Content-type', 'text/plain')
|
self.send_header('Content-type', 'text/plain')
|
||||||
|
@ -271,6 +271,32 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(content)
|
self.wfile.write(content)
|
||||||
|
|
||||||
|
def chunk_size(self, content_length):
|
||||||
|
return min(16*1024, content_length)
|
||||||
|
|
||||||
|
def write_with_limit(self, content, content_length):
|
||||||
|
chunk_size = self.chunk_size(content_length)
|
||||||
|
position = 0
|
||||||
|
while position < content_length:
|
||||||
|
if state.bandwidth:
|
||||||
|
while not state.bandwidth.upload(chunk_size) and self.server._running:
|
||||||
|
time.sleep(0.1)
|
||||||
|
data = content[position:position+chunk_size]
|
||||||
|
self.wfile.write(data)
|
||||||
|
position += chunk_size
|
||||||
|
|
||||||
|
def write_file_with_limit(self, path, content_length):
|
||||||
|
chunk_size = self.chunk_size(content_length)
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
while True:
|
||||||
|
data = f.read(chunk_size)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
self.wfile.write(data)
|
||||||
|
if state.bandwidth:
|
||||||
|
while not state.bandwidth.upload(chunk_size) and self.server._running:
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
class Server(Thread):
|
class Server(Thread):
|
||||||
http_server = None
|
http_server = None
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue