Skip to content

Commit

Permalink
fix: not install libmagic raise error (#13146)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjlarry authored Feb 3, 2025
1 parent 7452032 commit 304467e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ RUN apt-get update \
&& 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
&& 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 \
&& rm -rf /var/lib/apt/lists/*

Expand Down
23 changes: 21 additions & 2 deletions api/controllers/common/helpers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import mimetypes
import os
import platform
import re
import urllib.parse
import warnings
from collections.abc import Mapping
from typing import Any
from uuid import uuid4

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 configs import dify_config
Expand Down Expand Up @@ -49,7 +68,7 @@ def guess_file_info_from_response(response: httpx.Response):
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":
if mimetype == "application/octet-stream" and magic is not None:
try:
mimetype = magic.from_buffer(response.content[:1024], mime=True)
except magic.MagicException:
Expand Down

2 comments on commit 304467e

@BenjaminX
Copy link
Contributor

@BenjaminX BenjaminX commented on 304467e Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

28.15 Some packages could not be installed. This may mean that you have
28.15 requested an impossible situation or if you are using the unstable
28.15 distribution that some required packages have not yet been created
28.15 or been moved out of Incoming.
28.15 The following information may help to resolve the situation:
28.15
28.15 The following packages have unmet dependencies:
28.29 libmagic1 : Depends: libmagic-mgc (= 1:5.44-3) but 1:5.45-3+b1 is to be installed
28.30 E: Unable to correct problems, you have held broken packages.

@lingxuan630
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem:
28.15 The following packages have unmet dependencies:
28.29 libmagic1 : Depends: libmagic-mgc (= 1:5.44-3) but 1:5.45-3+b1 is to be installed
28.30 E: Unable to correct problems, you have held broken packages

The issue was resolved by removing the line: '&& apt-get install -y libmagic1 '

Please sign in to comment.