예제 #1
0
def test_truncate_middle():
    assert truncate_middle("testtesttest", 13) == "testtesttest"
    assert truncate_middle("testtesttest", 12) == "testtesttest"
    assert truncate_middle("testabcdecho", 11) == "test...echo"
    assert truncate_middle("testabcdecho", 10) == "test...cho"
    assert truncate_middle("testabcdecho", 9) == "tes...cho"
    assert truncate_middle("testabcdecho", 8) == "tes...ho"
    assert truncate_middle("testabcdecho", 7) == "te...ho"
    assert truncate_middle("testabcdecho", 6) == "te...o"
    assert truncate_middle("testabcdecho", 5) == "t...o"
    assert truncate_middle("testabcdecho", 4) == "t..."

    with pytest.raises(ValueError):
        truncate_middle("testtesttest", 3) == "..."
예제 #2
0
    def upload_user_file(self, user, file_name, stream, storage=None):
        """
        Saves the provided uploaded file in the provided storage. If no storage is
        provided the default_storage will be used. An entry into the user file table
        is also created.

        :param user: The user on whose behalf the file is uploaded.
        :type user: User
        :param file_name: The provided file name when the file was uploaded.
        :type file_name: str
        :param stream: An IO stream containing the uploaded file.
        :type stream: IOBase
        :param storage: The storage where the file must be saved to.
        :type storage: Storage
        :raises InvalidFileStreamError: If the provided stream is invalid.
        :raises FileSizeToLargeError: If the provided content is too large.
        :return: The newly created user file.
        :rtype: UserFile
        """

        if not hasattr(stream, 'read'):
            raise InvalidFileStreamError('The provided stream is not readable.')

        size = stream_size(stream)

        if size > settings.USER_FILE_SIZE_LIMIT:
            raise FileSizeTooLargeError(
                settings.USER_FILE_SIZE_LIMIT,
                'The provided file is too large.'
            )

        storage = storage or default_storage
        hash = sha256_hash(stream)
        file_name = truncate_middle(file_name, 64)

        try:
            return UserFile.objects.get(original_name=file_name, sha256_hash=hash)
        except UserFile.DoesNotExist:
            pass

        extension = pathlib.Path(file_name).suffix[1:].lower()
        mime_type = mimetypes.guess_type(file_name)[0] or ''
        unique = self.generate_unique(hash, extension)

        # By default the provided file is not an image.
        image = None
        is_image = False
        image_width = None
        image_height = None

        # Try to open the image with Pillow. If that succeeds we know the file is an
        # image.
        try:
            image = Image.open(stream)
            is_image = True
            image_width = image.width
            image_height = image.height
        except IOError:
            pass

        user_file = UserFile.objects.create(
            original_name=file_name,
            original_extension=extension,
            size=size,
            mime_type=mime_type,
            unique=unique,
            uploaded_by=user,
            sha256_hash=hash,
            is_image=is_image,
            image_width=image_width,
            image_height=image_height
        )

        # If the uploaded file is an image we need to generate the configurable
        # thumbnails for it. We want to generate them before the file is saved to the
        # storage because some storages close the stream after saving.
        if image:
            self.generate_and_save_image_thumbnails(image, user_file, storage=storage)

            # When all the thumbnails have been generated, the image can be deleted
            # from memory.
            del image

        # Save the file to the storage.
        full_path = self.user_file_path(user_file)
        storage.save(full_path, stream)

        # Close the stream because we don't need it anymore.
        stream.close()

        return user_file
예제 #3
0
def test_truncate_middle():
    assert truncate_middle('testtesttest', 13) == 'testtesttest'
    assert truncate_middle('testtesttest', 12) == 'testtesttest'
    assert truncate_middle('testabcdecho', 11) == 'test...echo'
    assert truncate_middle('testabcdecho', 10) == 'test...cho'
    assert truncate_middle('testabcdecho', 9) == 'tes...cho'
    assert truncate_middle('testabcdecho', 8) == 'tes...ho'
    assert truncate_middle('testabcdecho', 7) == 'te...ho'
    assert truncate_middle('testabcdecho', 6) == 'te...o'
    assert truncate_middle('testabcdecho', 5) == 't...o'
    assert truncate_middle('testabcdecho', 4) == 't...'

    with pytest.raises(ValueError):
        truncate_middle('testtesttest', 3) == '...'