SuspiciousFileOperation连接的路径位于基本路径组件之外

时间:2019-06-17 16:17:48

标签: python django python-imaging-library

运行以下测试时,我得到了SuspiciousFileOperation

from django.conf import settings
from django.contrib.auth import get_user_model
import pytest

User = get_user_model()
settings.CONNECT_HOT_FOLDER = f"{settings.ROOT_DIR}\\media\\hot_folder\\"
settings.MEDIA_ROOT = f"{settings.ROOT_DIR}\\media\\"  # <-- this was the issue!!


def image_for_test():
    employee = Employee.objects.create(core_id='1000', connect_id=1, connect_username='HOPCOBROKER.Test',)
    user = User.objects.create(username="hopcobroker\\test", employee=employee,)
    receipt = Receipt.objects.create(receipt_date=now(), description="test", amount=100.0, employee=user)
    image = ReceiptImage.objects.create(image=r'test_files\breaking_image.jpg', receipt=receipt,)
    return employee, user, receipt, image


@pytest.mark.django_db(transaction=True)
def test_convert_creates_csv_and_pdf_file(clean_up_test_files_in_hot_folder, settings):
    employee, user, receipt, image = image_for_test()
    pdf_file = f'{settings.CONNECT_HOT_FOLDER}\\{receipt.id}.pdf'
    csv_file = f'{settings.CONNECT_HOT_FOLDER}\\{receipt.id}.csv'

    convert(receipt)

    assert os.path.isfile(pdf_file)
    assert os.path.isfile(csv_file)
class ReceiptImage(models.Model):
    image = models.ImageField(upload_to='images/%Y/%m/')
    receipt = models.ForeignKey(
        Receipt,
        on_delete=models.CASCADE,
        related_name='images',
    )
    active = models.BooleanField(default=True)
    meta_data = fields.JSONField(blank=True, null=True)

使用PIL库打开图像并如下图所示

from PIL import Image
from .models import ReceiptImage
image = ReceiptImage.objects.filter(receipt=receipt)

img = Image.open(image.image)

我收到以下错误...

错误消息:

base = 'C:\\code\\receipts\\media\\'
paths = ('test_files\\no_meta_data_picture.png',)
final_path = 'C:\\code\\receipts\\media\\test_files\\no_meta_data_picture.png'
base_path = 'C:\\code\\receipts\\media\\'

    def safe_join(base, *paths):
        """
        Join one or more path components to the base path component intelligently.
        Return a normalized, absolute version of the final path.

        Raise ValueError if the final path isn't located inside of the base path
        component.
        """
        final_path = abspath(join(base, *paths))
        base_path = abspath(base)
        # Ensure final_path starts with base_path (using normcase to ensure we
        # don't false-negative on case insensitive operating systems like Windows),
        # further, one of the following conditions must be true:
        #  a) The next character is the path separator (to prevent conditions like
        #     safe_join("/dir", "/../d"))
        #  b) The final path must be the same as the base path.
        #  c) The base path must be the most root path (meaning either "/" or "C:\\")
        if (not normcase(final_path).startswith(normcase(base_path + sep)) and
                normcase(final_path) != normcase(base_path) and
                dirname(normcase(base_path)) != normcase(base_path)):
            raise SuspiciousFileOperation(
                'The joined path ({}) is located outside of the base path '
>               'component ({})'.format(final_path, base_path))
E           django.core.exceptions.SuspiciousFileOperation: The joined path (C:\code\receipts\media\test_files\no_meta_data_picture.png) is located outside of the base path component (C:\code\receipts\media\)

上面的错误消息

  
    

django.core.exceptions.SuspiciousFileOperation:联接路径(C:\ code \ receipts \ media \ test_files \ no_meta_data_picture.png)位于基本路径组件(C:\ code \ receipts \ media)之外   

...这对我来说意义不大。在错误消息中,它告诉我基本路径和最终路径是什么。最终路径在基本路径中。

以下是我认为是设置文件的相关部分。在查看其他解决方案时,我尝试添加或删除结尾的\/无济于事。我在Windows 10上。

settings.py

ROOT_DIR = (
    environ.Path(__file__) - 3  # (C:\code\receipts\config(3)\settings(2)\base.py(1) )
)
MEDIA_ROOT = os.path.join(ROOT_DIR, "media")

感谢您的帮助!

0 个答案:

没有答案