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
|
return mime_type in valid_mime_types
|
||||||
|
|
||||||
async def downloadDocument(file_id, sourceDocumentName):
|
async def downloadDocument(file_id, sourceDocumentTempName):
|
||||||
filepath = await (app.download_media(file_id, file_name=sourceDocumentName))
|
filepath = await (app.download_media(file_id, file_name=sourceDocumentTempName))
|
||||||
return filepath
|
return filepath
|
||||||
|
|
||||||
def unoCheck(): # Check if unoserver even exists
|
def unoCheck(): # Check if unoserver even exists
|
||||||
|
@ -58,17 +58,21 @@ def unoStart():
|
||||||
unoserverPath = unoCheck()
|
unoserverPath = unoCheck()
|
||||||
subprocess.Popen(args=[unoserverPath, "--daemon"])
|
subprocess.Popen(args=[unoserverPath, "--daemon"])
|
||||||
|
|
||||||
async def unoConvertDocument(sourceDocumentPath, sourceDocumentName):
|
async def unoConvertDocument(sourceDocumentPath, sourceDocumentTempName):
|
||||||
unoconvertPath = shutil.which("unoconvert")
|
unoconvertPath = shutil.which("unoconvert")
|
||||||
outputDir = "output"
|
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
|
Path(outputDir).mkdir(parents=True, exist_ok=True) # Ensure directory exists
|
||||||
subprocess.run(args=[unoconvertPath, "--convert-to", "pdf", sourceDocumentPath, outputDocumentPath])
|
subprocess.run(args=[unoconvertPath, "--convert-to", "pdf", sourceDocumentPath, outputDocumentPath])
|
||||||
subprocess.run(args=["rm", sourceDocumentPath], check=True)
|
subprocess.run(args=["rm", sourceDocumentPath], check=True)
|
||||||
return outputDocumentPath
|
return outputDocumentPath
|
||||||
|
|
||||||
async def uploadDocument(outputDocumentPath, chat, message):
|
async def getUploadDocumentName(outputDocumentPath, sourceDocumentName):
|
||||||
await app.send_document(document=outputDocumentPath, chat_id=chat, reply_to_message_id=message)
|
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])
|
subprocess.run(args=["rm", outputDocumentPath])
|
||||||
|
|
||||||
@app.on_message(filters.document) # Get all of the messages that have files in them
|
@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
|
mime_type = message.document.mime_type
|
||||||
if validDocument(mime_type=mime_type) == True:
|
if validDocument(mime_type=mime_type) == True:
|
||||||
sourceDocumentPath = await downloadDocument(message.document.file_id, message.document.file_unique_id)
|
sourceDocumentPath = await downloadDocument(message.document.file_id, message.document.file_unique_id)
|
||||||
outputDocumentPath = await unoConvertDocument(sourceDocumentPath, message.document.file_name)
|
outputDocumentPath = await unoConvertDocument(sourceDocumentPath, message.document.file_unique_id)
|
||||||
await uploadDocument(outputDocumentPath, message.chat.id, message.id)
|
uploadDocumentName = await getUploadDocumentName(outputDocumentPath, message.document.file_name)
|
||||||
|
await uploadDocument(outputDocumentPath, message.chat.id, message.id, uploadDocumentName)
|
||||||
else:
|
else:
|
||||||
return # Hopefully message will get ignored and no futher resources will be used
|
return # Hopefully message will get ignored and no futher resources will be used
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue