From 1fc54bbd9c873a0bba7c3fd443054e10c32d68c0 Mon Sep 17 00:00:00 2001 From: neko Date: Tue, 21 Jan 2025 19:16:10 +0500 Subject: [PATCH] Add optional unoserver support --- .gitignore | 1 + Dockerfile | 11 +++-------- TODO.md | 6 +++--- main.py | 33 ++++++++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 2d92d52..f968951 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.session-journal downalods/* output/* +TODO.md diff --git a/Dockerfile b/Dockerfile index c4b40cf..4540abe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,11 @@ FROM alpine:latest RUN apk add --no-cache libreoffice -RUN apk add font-terminus font-noto font-noto-extra font-arabic-misc font-misc-cyrillic font-mutt-misc font-screen-cyrillic font-winitzki-cyrillic font-cronyx-cyrillic font-noto-arabic font-noto-armenian font-noto-cherokee font-noto-devanagari font-noto-ethiopic font-noto-georgian font-noto-hebrew font-noto-lao font-noto-malayalam font-noto-tamil font-noto-thaana font-noto-thai -RUN apk add --no-cache --virtual temporary python3-dev py3-pip gcc libc-dev && pip install --break-system-packages pyrogram TgCrypto && apk del temporary - - +RUN apk add --no-cache font-terminus font-noto font-noto-extra font-arabic-misc font-misc-cyrillic font-mutt-misc font-screen-cyrillic font-winitzki-cyrillic font-cronyx-cyrillic font-noto-arabic font-noto-armenian font-noto-cherokee font-noto-devanagari font-noto-ethiopic font-noto-georgian font-noto-hebrew font-noto-lao font-noto-malayalam font-noto-tamil font-noto-thaana font-noto-thai +RUN apk add --no-cache --virtual temporary python3-dev py3-pip gcc libc-dev && pip install --break-system-packages pyrogram TgCrypto unoserver && apk del temporary WORKDIR /2pdf COPY main.py /2pdf/ COPY 2pdf.session /2pdf/ -CMD ["python", "main.py"] - - - +CMD ["python", "main.py"] \ No newline at end of file diff --git a/TODO.md b/TODO.md index 98a5867..6f620d8 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,9 @@ # TODO ## Planned -* Add /start command + * Add /replace command * Make every filename random to prevent issues with the same filename - ## In progress -* Use unoserver \ No newline at end of file +* Add /start command +* Make sure that unoserver restarts https://stackoverflow.com/questions/72146908/how-to-restart-subprocess-if-it-crashes \ No newline at end of file diff --git a/main.py b/main.py index 722e3e2..502866a 100644 --- a/main.py +++ b/main.py @@ -46,7 +46,30 @@ async def downloadDocument(file_id, file_name): filepath = await (app.download_media(file_id, file_name=file_name)) return filepath -async def convertDocument(sourceDocumentPath, sourceDocumentName): +def unoCheck(): # Check if unoserver even exists + global unoserverPath + unoserverPath = shutil.which("unoservers") + if unoserverPath: + return unoserverPath + else: + return None + +def unoStart(): + unoserverPath = unoCheck() + if unoserverPath: + subprocess.Popen(args=[unoserverPath, "--daemon"]) + else: + return + +async def unoConvertDocument(sourceDocumentPath, sourceDocumentName): + unoconvertPath = shutil.which("unoconvert") + outputDir = "output" + outputDocumentPath = Path(outputDir) / Path(f"{sourceDocumentName}").with_suffix('.pdf') + Path(outputDir).mkdir(parents=True, exist_ok=True) # Ensure directory exists + subprocess.run(args=[unoconvertPath, "--convert-to", "pdf", sourceDocumentPath, outputDocumentPath]) + return outputDocumentPath + +async def convertDocumentOneShot(sourceDocumentPath, sourceDocumentName): libreofficePath = shutil.which("libreoffice") # Find full path to the libreoffice binary outputDir = "output" Path(outputDir).mkdir(parents=True, exist_ok=True) # Ensure directory exists @@ -64,9 +87,13 @@ async def documentFetcher(client, message): mime_type = message.document.mime_type if validDocument(mime_type=mime_type) == True: sourceDocumentPath = await downloadDocument(message.document.file_id, message.document.file_name) - outputDocumentPath = await convertDocument(sourceDocumentPath, message.document.file_name) + if unoserverPath: + outputDocumentPath = await unoConvertDocument(sourceDocumentPath, message.document.file_name) + else: + outputDocumentPath = await convertDocumentOneShot(sourceDocumentPath, message.document.file_name) await uploadDocument(outputDocumentPath, message.chat.id, message.id) else: return # Hopefully message will get ignored and no futher resources will be used - + +unoStart() app.run() \ No newline at end of file