how can i use python-magic in python,docker

Viewed 159

using docker, python 3.8.5-alpine

i want to check upload file's mime type

so i make validators.py and download python-magic-bin module

it is working in test server(window, pycharm)

it is great working


so, I'm trying to upload to ubuntu 18.04 + docker(real server)

docker build is success but I get an Import Error: No module called magic. message

so first---

i tried to download

python-magic python-magic-bin libmagic libmagic1 py3-magic etc..

it isn't working...

and another solution is change my python version ex)3.8.3-slim it use apt-get command and It is also difficult to find a module to use instead.

Is there any solution in this situation? I just want to activate the right vaildator and I know the problem is that just download the "magic" module

but it is not easy for me

please me help

my Dockerfile

FROM python:3.8.5-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apk update
RUN apk add g++ postgresql-dev cargo gcc python3-dev libffi-dev openssl-dev musl-dev zlib-dev jpeg-dev #--(5.2)

COPY . /usr/src/app/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

requrements.txt

asgiref==3.3.2
backcall==0.2.0
beautifulsoup4==4.9.1
certifi==2020.6.20
chardet==3.0.4
colorama==0.4.3
Cython==0.29.22
decorator==4.4.2
defusedxml==0.6.0
Django==3.2.6
django-allauth==0.42.0
django-crispy-forms==1.9.2
django-extensions==3.0.9
django-markdownx==3.0.1
django-summernote==0.8.11.6
django-axes==5.17.0
et-xmlfile==1.0.1
gunicorn==20.0.4
idna==2.10
importlib-metadata==2.0.0
ipython==7.18.1
ipython-genutils==0.2.0
jdcal==1.4.1
jedi==0.17.2
Markdown==3.2.2
numpy==1.20.2
numexpr==2.8.1
oauthlib==3.1.0
openpyxl==3.0.6
pandas==1.3.0
parso==0.7.1
pickleshare==0.7.5
Pillow==7.2.0
prompt-toolkit==3.0.7
psycopg2==2.8.6
pycparser==2.20
Pygments==2.7.1
python-dateutil==2.8.1
python-magic==0.4.24
python3-openid==3.2.0
pytz==2020.1
requests==2.24.0
requests-oauthlib==1.3.0
semantic-version==2.8.5
setuptools
setuptools-rust==0.11.6
six==1.15.0
soupsieve==2.0.1
sqlparse==0.3.1
toml==0.10.2
traitlets==5.0.4
urllib3==1.25.10
wcwidth==0.2.5
zipp==3.2.0

validators.py

import os
import magic
from django.core.exceptions import ValidationError

def validate_extension(file):
    valid_mime_types = ["application/avi", "application/mp4", "application/xlsx", "application/xls", "application/ppt", "application/pptx", "application/hwp", "application/doc", "application/docx", "application/pdf", "image/jpeg", "image/png", "image/jpg"]
    file_mime_type = magic.from_buffer(file.read(2048), mime=True) #  Changed this to 1024 to 2048

    if file_mime_type not in valid_mime_types:
        raise ValidationError("it is not correct files.")

    valid_file_extensions = [".avi", ".mp4", ".xlsx", ".xls", ".ppt", ".pptx", ".hwp", ".doc", ".docx", ".pdf", ".jpeg", ".png", ".jpg"]
    ext = os.path.splitext(file.name)[1]

    if ext.lower() not in valid_file_extensions:
        raise ValidationError("it is not correct")
0 Answers
Related