Browse Source

fix: not install libmagic raise error (#13146)

tags/0.15.3
非法操作 9 months ago
parent
commit
304467e3f5
No account linked to committer's email address
2 changed files with 23 additions and 2 deletions
  1. 2
    0
      api/Dockerfile
  2. 21
    2
      api/controllers/common/helpers.py

+ 2
- 0
api/Dockerfile View File

&& apt-get install -y --no-install-recommends expat=2.6.4-1 libldap2=2.6.9+dfsg-1 perl=5.40.0-8 libsqlite3-0=3.46.1-1 zlib1g=1:1.3.dfsg+really1.3.1-1+b1 \ && apt-get install -y --no-install-recommends expat=2.6.4-1 libldap2=2.6.9+dfsg-1 perl=5.40.0-8 libsqlite3-0=3.46.1-1 zlib1g=1:1.3.dfsg+really1.3.1-1+b1 \
# install a chinese font to support the use of tools like matplotlib # install a chinese font to support the use of tools like matplotlib
&& apt-get install -y fonts-noto-cjk \ && apt-get install -y fonts-noto-cjk \
# install libmagic to support the use of python-magic guess MIMETYPE
&& apt-get install -y libmagic1 \
&& apt-get autoremove -y \ && apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*



+ 21
- 2
api/controllers/common/helpers.py View File

import mimetypes import mimetypes
import os import os
import platform
import re import re
import urllib.parse import urllib.parse
import warnings
from collections.abc import Mapping from collections.abc import Mapping
from typing import Any from typing import Any
from uuid import uuid4 from uuid import uuid4


import httpx import httpx
import magic

try:
import magic
except ImportError:
if platform.system() == "Windows":
warnings.warn(
"To use python-magic guess MIMETYPE, you need to run `pip install python-magic-bin`", stacklevel=2
)
elif platform.system() == "Darwin":
warnings.warn("To use python-magic guess MIMETYPE, you need to run `brew install libmagic`", stacklevel=2)
elif platform.system() == "Linux":
warnings.warn(
"To use python-magic guess MIMETYPE, you need to run `sudo apt-get install libmagic1`", stacklevel=2
)
else:
warnings.warn("To use python-magic guess MIMETYPE, you need to install `libmagic`", stacklevel=2)
magic = None # type: ignore

from pydantic import BaseModel from pydantic import BaseModel


from configs import dify_config from configs import dify_config
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 # Use python-magic to guess MIME type if still unknown or generic
if mimetype == "application/octet-stream":
if mimetype == "application/octet-stream" and magic is not None:
try: try:
mimetype = magic.from_buffer(response.content[:1024], mime=True) mimetype = magic.from_buffer(response.content[:1024], mime=True)
except magic.MagicException: except magic.MagicException:

Loading…
Cancel
Save