Finish: Make sure file names get randomized during processing
This commit is contained in:
parent
c6bd9b1252
commit
5b49edddec
21
main.py
21
main.py
|
@ -42,8 +42,8 @@ def validDocument(mime_type):
|
|||
]
|
||||
return mime_type in valid_mime_types
|
||||
|
||||
async def downloadDocument(file_id, sourceDocumentName):
|
||||
filepath = await (app.download_media(file_id, file_name=sourceDocumentName))
|
||||
async def downloadDocument(file_id, sourceDocumentTempName):
|
||||
filepath = await (app.download_media(file_id, file_name=sourceDocumentTempName))
|
||||
return filepath
|
||||
|
||||
def unoCheck(): # Check if unoserver even exists
|
||||
|
@ -58,17 +58,21 @@ def unoStart():
|
|||
unoserverPath = unoCheck()
|
||||
subprocess.Popen(args=[unoserverPath, "--daemon"])
|
||||
|
||||
async def unoConvertDocument(sourceDocumentPath, sourceDocumentName):
|
||||
async def unoConvertDocument(sourceDocumentPath, sourceDocumentTempName):
|
||||
unoconvertPath = shutil.which("unoconvert")
|
||||
outputDir = "output"
|
||||
outputDocumentPath = Path(outputDir) / Path(f"{sourceDocumentName}").with_suffix('.pdf')
|
||||
outputDocumentPath = Path(outputDir) / Path(sourceDocumentTempName)
|
||||
Path(outputDir).mkdir(parents=True, exist_ok=True) # Ensure directory exists
|
||||
subprocess.run(args=[unoconvertPath, "--convert-to", "pdf", sourceDocumentPath, outputDocumentPath])
|
||||
subprocess.run(args=["rm", sourceDocumentPath], check=True)
|
||||
return outputDocumentPath
|
||||
|
||||
async def uploadDocument(outputDocumentPath, chat, message):
|
||||
await app.send_document(document=outputDocumentPath, chat_id=chat, reply_to_message_id=message)
|
||||
async def getUploadDocumentName(outputDocumentPath, sourceDocumentName):
|
||||
uploadDocumentName = Path(f"{sourceDocumentName}").with_suffix('.pdf').as_posix()
|
||||
return uploadDocumentName
|
||||
|
||||
async def uploadDocument(outputDocumentPath, chat, message, name):
|
||||
await app.send_document(document=outputDocumentPath, chat_id=chat, reply_to_message_id=message, file_name=name)
|
||||
subprocess.run(args=["rm", outputDocumentPath])
|
||||
|
||||
@app.on_message(filters.document) # Get all of the messages that have files in them
|
||||
|
@ -76,8 +80,9 @@ 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_unique_id)
|
||||
outputDocumentPath = await unoConvertDocument(sourceDocumentPath, message.document.file_name)
|
||||
await uploadDocument(outputDocumentPath, message.chat.id, message.id)
|
||||
outputDocumentPath = await unoConvertDocument(sourceDocumentPath, message.document.file_unique_id)
|
||||
uploadDocumentName = await getUploadDocumentName(outputDocumentPath, message.document.file_name)
|
||||
await uploadDocument(outputDocumentPath, message.chat.id, message.id, uploadDocumentName)
|
||||
else:
|
||||
return # Hopefully message will get ignored and no futher resources will be used
|
||||
|
||||
|
|
Loading…
Reference in New Issue