Skip to content

Commit

Permalink
Possible fix for #64 (#65)
Browse files Browse the repository at this point in the history
improvements on file name finder on lib/ddl
  • Loading branch information
Itz-fork authored Jun 15, 2024
1 parent 0124bcc commit 33b9189
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# dev environment
.env
venv
.venv
.mega/
*.session
NexaBots
NexaBots
tests
23 changes: 22 additions & 1 deletion megadl/lib/ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
import re
import asyncio
import hashlib
import mimetypes

from time import time
from aiohttp import ClientSession
Expand All @@ -19,6 +21,7 @@
r"https://drive\.google\.com/file/d/(.*?)/.*?\?usp=(sharing|drive_link)"
)
STR_GD_REPLACE = r"https://drive.google.com/uc?export=download&id=\1"
DEFAULT_EXT = ".bin"


class Downloader:
Expand Down Expand Up @@ -81,14 +84,32 @@ async def from_ddl(
# Create folder if it doesn't exist
wpath = f"{path}/{chat_id}"
os.makedirs(wpath)
wpath = f"{wpath}/{os.path.basename(url)}"

async with ClientSession() as session:
_chunksize = int(os.getenv("CHUNK_SIZE"))
async with session.get(url, timeout=None, allow_redirects=True) as resp:
# Raise HttpStatusError on failed requests
if resp.status != 200:
raise HttpStatusError(resp.status)

# Try to get the file name
cnt_disp = resp.headers.get("Content-Disposition")
if (
cnt_disp
and (cd_parts := cnt_disp.split("filename="))
and len(cd_parts) > 1
):
fname = cd_parts[1].strip('\"')

elif _ftype := mimetypes.guess_type(url):
_fext = mimetypes.guess_extension(_ftype)
fname = f"{hashlib.md5(url.encode()).hexdigest()}{DEFAULT_EXT if not _fext else _fext}"

else:
fname = os.path.basename(url)

wpath = f"{wpath}/{fname}"

# Handle content length header
total = resp.content_length
curr = 0
Expand Down

0 comments on commit 33b9189

Please sign in to comment.