mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-12 04:49:04 +08:00
fix:Improve MIME type detection for remote URL uploads using python-magic (#12693)
This commit is contained in:
parent
a6a727e8a4
commit
aad7e4dd1c
@ -7,6 +7,7 @@ from typing import Any
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
import magic
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from configs import dify_config
|
from configs import dify_config
|
||||||
@ -47,6 +48,13 @@ def guess_file_info_from_response(response: httpx.Response):
|
|||||||
# If guessing fails, use Content-Type from response headers
|
# If guessing fails, use Content-Type from response headers
|
||||||
mimetype = response.headers.get("Content-Type", "application/octet-stream")
|
mimetype = response.headers.get("Content-Type", "application/octet-stream")
|
||||||
|
|
||||||
|
# Use python-magic to guess MIME type if still unknown or generic
|
||||||
|
if mimetype == "application/octet-stream":
|
||||||
|
try:
|
||||||
|
mimetype = magic.from_buffer(response.content[:1024], mime=True)
|
||||||
|
except magic.MagicException:
|
||||||
|
pass
|
||||||
|
|
||||||
extension = os.path.splitext(filename)[1]
|
extension = os.path.splitext(filename)[1]
|
||||||
|
|
||||||
# Ensure filename has an extension
|
# Ensure filename has an extension
|
||||||
|
Loading…
x
Reference in New Issue
Block a user