Skip to content

Commit

Permalink
fix: content_type match error
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 committed Sep 21, 2023
1 parent 64e074f commit 54e6c2d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 7 additions & 2 deletions modules/handlers/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ async def url_handler(event):
url = cmd[0]
# lest the url is bold
url = url.strip().strip('*')
name, local_response = get_filename(url)
total_length = int(local_response.headers['Content-Length']) / (1024 * 1024)
except:
await event.reply(url_res)
raise events.StopPropagation
Expand All @@ -38,6 +36,13 @@ async def url_handler(event):

status_message = await Status_Message.create(event)

try:
name, local_response = get_filename(url)
total_length = int(local_response.headers['Content-Length']) / (1024 * 1024)
except Exception as e:
await event.reply(logger('Error:\n%s'%analysis_content_not_found))
raise events.StopPropagation

try:
logger('upload url: %s' % url)
progress_url = onedrive.upload_from_url(url, name)
Expand Down
17 changes: 13 additions & 4 deletions modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,30 @@ def get_filename(url):
name = get_filename_from_url(url)
if name:
ext = get_ext(response.headers['Content-Type'])
if ext != name.split('.')[-1]:
name = name.split('.')[0] + ext
if ext:
if ext != name.split('.')[-1]:
name = name.split('.')[0] + ext
else:
raise Exception("Url refer to none-file")
else:
name = str(int(time.time())) + ext
elif len(name) > 100:
ext = get_ext(response.headers['Content-Type'])
name = str(int(time.time())) + ext
if ext:
name = str(int(time.time())) + ext
else:
raise Exception("Url refer to none-file")
return name, response
else:
raise Exception("File from url not found")



def get_ext(content_type):
return mimetypes.guess_extension(content_type)
content_type = re.findall('([^;]+);', content_type)
if len(content_type) == 0:
return None
return mimetypes.guess_extension(content_type[0])


def get_link(string):
Expand Down

0 comments on commit 54e6c2d

Please sign in to comment.