v1.0.47: deploy/ — конвертер .doc + nginx прокси

This commit is contained in:
2026-06-19 08:14:00 +04:00
parent 92113c0374
commit 3cb312ab64
3 changed files with 107 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""HTTP-сервер для конвертации .doc → .docx"""
from http.server import HTTPServer, BaseHTTPRequestHandler
import subprocess, tempfile, os, sys
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
data = self.rfile.read(length)
with tempfile.NamedTemporaryFile(suffix=".doc", delete=False) as f:
f.write(data); doc_path = f.name
tmpdir = tempfile.mkdtemp()
try:
subprocess.run(["libreoffice", "--headless", "--convert-to", "docx", "--outdir", tmpdir, doc_path],
timeout=30, capture_output=True)
docx = [x for x in os.listdir(tmpdir) if x.endswith(".docx")]
if docx:
with open(os.path.join(tmpdir, docx[0]), "rb") as f:
out = f.read()
self.send_response(200)
self.send_header("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
self.send_header("Content-Length", len(out))
self.end_headers()
self.wfile.write(out)
else:
self.send_error(500, "Conversion failed")
finally:
os.unlink(doc_path)
for x in os.listdir(tmpdir): os.unlink(os.path.join(tmpdir, x))
os.rmdir(tmpdir)
def log_message(self, *a): pass
HTTPServer(("127.0.0.1", 8766), Handler).serve_forever()