返回提交历史
Modified
g4f/tools/files.py
+3
-3
XFEstudio/gpt4free
feat: Add customizable file extension in `get_filename_from_url`
- Modified `get_filename_from_url` function to accept a second parameter `extension`, with a default value of ".md". - Updated the return statement in `get_filename_from_url` to use the new `extension` parameter instead of the hardcoded ".md" suffix. - Adjusted `get_filename` asynchronous function to pass the `extension` retrieved from `await get_file_extension(response)` into `get_filename_from_url`.
c6b85efb
代码差异
1 个文件
+3
-3
@@ -346,12 +346,12 @@ def split_file_by_size_and_newline(input_filename, output_dir, chunk_size_bytes=
346
346
with open(output_filename, 'w', encoding='utf-8') as outfile:
347
347
outfile.write(current_chunk)
348
348
349
def get_filename_from_url(url: str) -> str:
349
def get_filename_from_url(url: str, extension: str = ".md") -> str:
350
350
parsed_url = urllib.parse.urlparse(url)
351
351
sha256_hash = hashlib.sha256(url.encode()).digest()
352
352
base32_encoded = base64.b32encode(sha256_hash).decode()
353
353
url_hash = base32_encoded[:24].lower()
354
return f"{parsed_url.netloc}+{parsed_url.path[1:].replace('/', '_')}+{url_hash}.md"
354
return f"{parsed_url.netloc}+{parsed_url.path[1:].replace('/', '_')}+{url_hash}{extension}"
355
355
356
356
async def get_filename(response: ClientResponse) -> str:
357
357
"""
@@ -378,7 +378,7 @@ async def get_filename(response: ClientResponse) -> str:
378
378
if content_type and url:
379
379
extension = await get_file_extension(response)
380
380
if extension:
381
return get_filename_from_url(url)
381
return get_filename_from_url(url, extension)
382
382
383
383
return None
384
384