|
| 1 | +try: |
| 2 | + from aiohttp import web |
| 3 | +except ImportError: |
| 4 | + raise SystemExit("Install with server extra to use this command.") |
| 5 | +import importlib.metadata |
| 6 | +import logging |
| 7 | +import os.path |
| 8 | +import tempfile |
| 9 | + |
| 10 | +from docx import Document |
| 11 | + |
| 12 | +from docxcompose.composer import Composer |
| 13 | + |
| 14 | + |
| 15 | +CHUNK_SIZE = 65536 |
| 16 | +logger = logging.getLogger("docxcompose") |
| 17 | +version = importlib.metadata.version("docxcompose") |
| 18 | + |
| 19 | + |
| 20 | +async def compose(request): |
| 21 | + |
| 22 | + documents = [] |
| 23 | + temp_dir = None |
| 24 | + |
| 25 | + if not request.content_type == "multipart/form-data": |
| 26 | + logger.info( |
| 27 | + "Bad request. Received content type %s instead of multipart/form-data.", |
| 28 | + request.content_type, |
| 29 | + ) |
| 30 | + return web.Response(status=400, text="Multipart request required") |
| 31 | + |
| 32 | + reader = await request.multipart() |
| 33 | + |
| 34 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 35 | + while True: |
| 36 | + part = await reader.next() |
| 37 | + |
| 38 | + if part is None: |
| 39 | + break |
| 40 | + |
| 41 | + if part.filename is None: |
| 42 | + continue |
| 43 | + |
| 44 | + documents.append(await save_part_to_file(part, temp_dir)) |
| 45 | + |
| 46 | + if not documents: |
| 47 | + return web.Response(status=400, text="No documents provided") |
| 48 | + |
| 49 | + composed_filename = os.path.join(temp_dir, "composed.docx") |
| 50 | + |
| 51 | + try: |
| 52 | + composer = Composer(Document(documents.pop(0))) |
| 53 | + for document in documents: |
| 54 | + composer.append(Document(document)) |
| 55 | + composer.save(composed_filename) |
| 56 | + except Exception: |
| 57 | + logger.exception("Failed composing documents.") |
| 58 | + return web.Response(status=500, text="Failed composing documents") |
| 59 | + |
| 60 | + return await stream_file( |
| 61 | + request, |
| 62 | + composed_filename, |
| 63 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +async def save_part_to_file(part, directory): |
| 68 | + filename = os.path.join(directory, f"{part.name}_{part.filename}") |
| 69 | + with open(filename, "wb") as file_: |
| 70 | + while True: |
| 71 | + chunk = await part.read_chunk(CHUNK_SIZE) |
| 72 | + if not chunk: |
| 73 | + break |
| 74 | + file_.write(chunk) |
| 75 | + return filename |
| 76 | + |
| 77 | + |
| 78 | +async def stream_file(request, filename, content_type): |
| 79 | + response = web.StreamResponse( |
| 80 | + status=200, |
| 81 | + reason="OK", |
| 82 | + headers={ |
| 83 | + "Content-Type": content_type, |
| 84 | + "Content-Disposition": f'attachment; filename="{os.path.basename(filename)}"', |
| 85 | + }, |
| 86 | + ) |
| 87 | + await response.prepare(request) |
| 88 | + |
| 89 | + with open(filename, "rb") as outfile: |
| 90 | + while True: |
| 91 | + data = outfile.read(CHUNK_SIZE) |
| 92 | + if not data: |
| 93 | + break |
| 94 | + await response.write(data) |
| 95 | + |
| 96 | + await response.write_eof() |
| 97 | + return response |
| 98 | + |
| 99 | + |
| 100 | +async def healthcheck(request): |
| 101 | + return web.Response(status=200, text="OK") |
| 102 | + |
| 103 | + |
| 104 | +def create_app(): |
| 105 | + app = web.Application() |
| 106 | + app.add_routes([web.post("/", compose)]) |
| 107 | + app.add_routes([web.get("/healthcheck", healthcheck)]) |
| 108 | + return app |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + print(f"docxcompose {version}") |
| 113 | + logging.basicConfig( |
| 114 | + format="%(asctime)s %(levelname)s %(name)s %(message)s", |
| 115 | + level=logging.INFO, |
| 116 | + ) |
| 117 | + web.run_app(create_app()) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments