arq_storage.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. MIT License
  3. Copyright (c) 2021 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. from asyncio import Lock
  21. from os import remove
  22. from pyrogram import filters
  23. from pyrogram.types import Message
  24. from wbb import app, arq
  25. from wbb.core.decorators.errors import capture_err
  26. from wbb.utils.functions import get_file_id_from_message
  27. __MODULE__ = "Storage"
  28. __HELP__ = """
  29. Upload unlimited files smaller than 100MB
  30. And get a download link
  31. **Usage:**
  32. /upload [url|Reply to a file]
  33. """
  34. UPLOAD_LOCK = Lock()
  35. async def upload(m: Message, file: str = None, url: str = None):
  36. err = "Something went wrong"
  37. try:
  38. resp = await arq.upload(file=file, url=url)
  39. except Exception:
  40. await m.edit(err)
  41. if file:
  42. remove(file)
  43. return
  44. if file:
  45. remove(file)
  46. if not resp:
  47. return await m.edit(err)
  48. if not resp.ok:
  49. return await m.edit(err)
  50. await m.edit(
  51. f"**Download Link:** {resp.result}",
  52. disable_web_page_preview=True,
  53. )
  54. @app.on_message(filters.command("upload"))
  55. @capture_err
  56. async def arq_upload(_, message):
  57. if message.reply_to_message:
  58. if UPLOAD_LOCK.locked():
  59. return await message.reply(
  60. "One upload is already in progress, please try again later"
  61. )
  62. async with UPLOAD_LOCK:
  63. r = message.reply_to_message
  64. file_id = get_file_id_from_message(r, 100000000, None)
  65. if not file_id:
  66. return await message.reply("Unsupported media.")
  67. m = await message.reply("Downloading...")
  68. file = await app.download_media(file_id)
  69. await m.edit("Uploading...")
  70. return await upload(m, file=file)
  71. if len(message.command) != 2:
  72. return await message.reply("Not enough arguments")
  73. url = message.text.split(None, 1)[1]
  74. m = await message.reply("Uploading...")
  75. await upload(m, url=url)