files.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import math
  21. import os
  22. from PIL import Image
  23. from pyrogram import Client, raw
  24. from pyrogram.file_id import FileId
  25. STICKER_DIMENSIONS = (512, 512)
  26. async def resize_file_to_sticker_size(file_path: str) -> str:
  27. im = Image.open(file_path)
  28. if (im.width, im.height) < STICKER_DIMENSIONS:
  29. size1 = im.width
  30. size2 = im.height
  31. if im.width > im.height:
  32. scale = STICKER_DIMENSIONS[0] / size1
  33. size1new = STICKER_DIMENSIONS[0]
  34. size2new = size2 * scale
  35. else:
  36. scale = STICKER_DIMENSIONS[1] / size2
  37. size1new = size1 * scale
  38. size2new = STICKER_DIMENSIONS[1]
  39. size1new = math.floor(size1new)
  40. size2new = math.floor(size2new)
  41. sizenew = (size1new, size2new)
  42. im = im.resize(sizenew)
  43. else:
  44. im.thumbnail(STICKER_DIMENSIONS)
  45. try:
  46. os.remove(file_path)
  47. file_path = f"{file_path}.png"
  48. return file_path
  49. finally:
  50. im.save(file_path)
  51. async def upload_document(
  52. client: Client, file_path: str, chat_id: int
  53. ) -> raw.base.InputDocument:
  54. media = await client.invoke(
  55. raw.functions.messages.UploadMedia(
  56. peer=await client.resolve_peer(chat_id),
  57. media=raw.types.InputMediaUploadedDocument(
  58. mime_type=client.guess_mime_type(file_path)
  59. or "application/zip",
  60. file=await client.save_file(file_path),
  61. attributes=[
  62. raw.types.DocumentAttributeFilename(
  63. file_name=os.path.basename(file_path)
  64. )
  65. ],
  66. ),
  67. )
  68. )
  69. return raw.types.InputDocument(
  70. id=media.document.id,
  71. access_hash=media.document.access_hash,
  72. file_reference=media.document.file_reference,
  73. )
  74. async def get_document_from_file_id(
  75. file_id: str,
  76. ) -> raw.base.InputDocument:
  77. decoded = FileId.decode(file_id)
  78. return raw.types.InputDocument(
  79. id=decoded.media_id,
  80. access_hash=decoded.access_hash,
  81. file_reference=decoded.file_reference,
  82. )