FileSystemStorage不起作用,并且当上传2张图像时,返回SuspiciousFileOperation错误

时间:2019-06-28 20:16:16

标签: django django-rest-framework django-2.2

我遇到了一个可怕的问题。 我想上传2张图片,并通过大量代码运行它们后得到结果。但是,当我选择在服务器上上传这些文件的路径时,总是会收到“ SuspiciousFileOperation”错误。

api视图

import json
import os
from rest_framework import generics
from rest_framework.response import Response
from rest_framework import permissions
from .ProcessData.FaceRecognition import FaceRecognition
from .ProcessData.OCR import OCR
from .ProcessData.Wanted import Wanted
from identity.models import IdentityCheck
from .serializers import IdentityCheckSerializer
from rest_framework.generics import CreateAPIView
from django.core.files.storage import FileSystemStorage
from django.conf import settings

class IdentityCheckView(CreateAPIView, generics.ListAPIView):

    serializer_class = IdentityCheckSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        request = self.request
        qs = IdentityCheck.objects.all()
        query = self.request.GET.get('q')
        if query is not None:
            qs = qs.filter(name__icontains=query)
        return qs

    def save_fss(self, filename, file):
        mediaRoot = os.path.join(settings.MEDIA_ROOT, 'media/tmp/')
        filename = filename + ".jpg"
        fs = FileSystemStorage(location=mediaRoot)
        if fs.exists(filename):
            os.remove(mediaRoot + filename)
        newFile = fs.save(filename, file)
        uploaded_file_url = mediaRoot + fs.url(newFile)
        return uploaded_file_url

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        if(serializer.is_valid()):
            data = serializer.validated_data
            passPhoto = request.FILES['passengerPhoto']
            uploaded_file_url_pass_photo = self.save_fss("passPhoto", passPhoto)

            passPassport = request.FILES['passengerPassport']
            uploaded_file_url_pass_passport = self.save_fss("passPassport", passPassport)


            image = FaceRecognition.imageMatch(uploaded_file_url_pass_photo, uploaded_file_url_pass_passport)
            wanted = Wanted.criminalMatch(uploaded_file_url_pass_photo)
            passport_json = OCR.passportMatch(uploaded_file_url_pass_passport)
            image_json = json.loads(image)

            firstName      = passport_json['names']
            lastName       = passport_json['surname']
            nationality    = passport_json['country']
            birthDate      = passport_json['date_of_birth']
            gender         = passport_json['sex']

            ableToBoard = (wanted==0) and (int(image_json['match'])==1) and passport_json['valid_expiration_date'] and passport_json['valid_date_of_birth']
            serializer.save(agent=self.request.user, firstName=firstName, lastName=lastName, nationality=nationality, birthDate=birthDate, gender=gender, ableToBoard=ableToBoard)
        else:
            return Response({"image": "-1", "passport": "-1", "wanted": "-1"}, status=500)

        return Response({"image": image_json, "passport": passport_json, "wanted": wanted}, status=200)

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = True

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'django_postgres_extensions',

    'rest_framework',
    'corsheaders',

    'identity',
    'models',
    'admincp',
    'abnormal',
    'language'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'IBCS.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
...
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.dirname(BASE_DIR)
MEDIA_URL = '/'

from IBCS.restconf.main import *
from IBCS.apps import *

错误显示:

The joined path (C:\Users\Kyoko\Desktop\IBCS\server\media\tmp\passPhoto.jpg) is located outside of the base path component (C:\Users\Kyoko\Desktop\IBCS\server\media\tmp\)
Bad Request: /api/check/
"POST /api/check/ HTTP/1.1" 400 20335

由于某些张量流问题,此代码在重新安装Django和Python之前运行良好,但重新安装了所有必需的软件包。

谢谢您的帮助!

Django版本:2.2.2 Python版本:3.6.7(64位)

1 个答案:

答案 0 :(得分:0)

评论太久了

1

MEDIA_ROOT = os.path.dirname(BASE_DIR)
MEDIA_URL = '/'
...
os.path.join(settings.MEDIA_ROOT, 'media/tmp/')

不是,您不应该设置MEDIA_ROOT = BASEDIR,而应该设置

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')
MEDIA_URL = '/media/'

,并避免将media连接到您要使用这些文件的所有位置。 注意,在产品上,您应该为媒体文件选择其他根文件夹-项目目录之外。

另外,您应该设置STATIC_ROOT,而您的settings.py

中没有配置

2

mediaRoot = os.path.join(settings.MEDIA_ROOT, 'media/tmp/')
mediaRoot + filename

请勿将path.join与简单串联在一起使用。在此示例中,您正在混合使用\/分隔符(因为您在Windows上)。它将起作用,但不是一个好的样式。网址将失败。在这里-始终使用os.path.join。请注意,它可以包含两个以上的参数。

3

uploaded_file_url = mediaRoot + fs.url(newFile)

medirRoot是一个文件夹,在您的情况下,它使用\作为路径分隔符,而fs.url是带有/分隔符的URL。因此,您在这里既不会获得URL也不会获得文件的完整路径。是的,它应该工作的原因是fs.url将返回部分URL,并且它将部分路径复制到文件中。但是,a)fs具有path方法b)为什么要执行此串联如果您要上传到恒定路径,则完全没有?

4

if fs.exists(filename):
        os.remove(mediaRoot + filename)

fs可以做delete(),所以请不要混用处理文件的方法。请注意,无论何时何地上传文件,您都将始终覆盖文件。

5

您应该显示完整的错误回溯。但是,我的猜测是:您不应该从save_fss返回完整路径。尝试返回fs.path(),正确配置MEDIA_ROOT,也许应该可以。