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
|
||||
if id and len(id) == 32 and id.isalnum():
|
||||
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:
|
||||
from item.icons import get_icon_sync
|
||||
try:
|
||||
|
@ -149,7 +142,14 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
|||
# FIXME: also check for limit here
|
||||
self.wfile.write(data)
|
||||
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 = {
|
||||
'epub': 'application/epub+zip',
|
||||
'pdf': 'application/pdf',
|
||||
|
@ -158,20 +158,20 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
|||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-Type', mimetype)
|
||||
self.send_header('X-Node-Protocol', settings.NODE_PROTOCOL)
|
||||
self.send_header('Content-Length', str(os.path.getsize(path)))
|
||||
if mimetype == 'text/plain':
|
||||
with open(path, 'rb') as f:
|
||||
content = f.read()
|
||||
content = self.gzip_data(content)
|
||||
content_length = len(content)
|
||||
else:
|
||||
content = None
|
||||
content_length = os.path.getsize(path)
|
||||
self.send_header('Content-Length', str(content_length))
|
||||
self.end_headers()
|
||||
chunk_size = 16*1024
|
||||
with open(path, 'rb') as f:
|
||||
size = 0
|
||||
while 1:
|
||||
data = f.read(chunk_size)
|
||||
if not data:
|
||||
break
|
||||
size += len(data)
|
||||
self.wfile.write(data)
|
||||
if state.bandwidth:
|
||||
while not state.bandwidth.upload(chunk_size) and self.server._running:
|
||||
time.sleep(0.1)
|
||||
if content:
|
||||
self.write_with_limit(content, content_length)
|
||||
else:
|
||||
self.write_file_with_limit(path, content_length)
|
||||
else:
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
|
@ -271,6 +271,32 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
|||
self.end_headers()
|
||||
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):
|
||||
http_server = None
|
||||
|
||||
|
|
Loading…
Reference in a new issue