Add optional unoserver support
This commit is contained in:
parent
8a5cee1d5f
commit
1fc54bbd9c
|
@ -2,3 +2,4 @@
|
|||
*.session-journal
|
||||
downalods/*
|
||||
output/*
|
||||
TODO.md
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
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
|
||||
|
||||
|
@ -11,6 +9,3 @@ COPY main.py /2pdf/
|
|||
COPY 2pdf.session /2pdf/
|
||||
|
||||
CMD ["python", "main.py"]
|
||||
|
||||
|
||||
|
||||
|
|
6
TODO.md
6
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
|
||||
* Add /start command
|
||||
* Make sure that unoserver restarts https://stackoverflow.com/questions/72146908/how-to-restart-subprocess-if-it-crashes
|
31
main.py
31
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()
|
Loading…
Reference in New Issue