# Path: chamber/config.py
# DEFAULTS = {
#     'MAX_FILE_UPLOAD_SIZE': 20,
#     'MULTIDOMAINS_OVERTAKER_AUTH_COOKIE_NAME': None,
#     'DEFAULT_IMAGE_ALLOWED_CONTENT_TYPES': {'image/jpeg', 'image/png', 'image/gif'},
#     'PRIVATE_S3_STORAGE_URL_EXPIRATION': 3600,
#     'AWS_S3_ON': getattr(django_settings, 'AWS_S3_ON', False),
#     'AWS_REGION': getattr(django_settings, 'AWS_REGION', None),
# }
# class Settings:
#     def __getattr__(self, attr):


import boto3
from botocore.client import Config
from django.core.files.base import ContentFile
from django.core.files.storage import DefaultStorage
from storages.backends.s3boto3 import S3Boto3Storage
from chamber.config import settings





__all__ = (
    'BaseS3Storage',
    'BasePrivateS3Storage',
    'BasePrivateS3DataStorage',
    'force_bytes_content',
    'get_storage_instance',
)


def force_bytes_content(content, blocksize=1024):
    """Returns a tuple of content (file-like object) and bool indicating wheter the content has been casted or not"""
    block = content.read(blocksize)
    content.seek(0)

    if not isinstance(block, bytes):
        _content = bytes(
            content.read(),
            'utf-8' if not hasattr(content, 'encoding') or content.encoding is None else content.encoding,
        )
        return ContentFile(_content), True
    return content, False


def get_storage_class(s3_storage_class, default_storage_class=DefaultStorage):

# Path: chamber/config.py
# DEFAULTS = {
#     'MAX_FILE_UPLOAD_SIZE': 20,
#     'MULTIDOMAINS_OVERTAKER_AUTH_COOKIE_NAME': None,
#     'DEFAULT_IMAGE_ALLOWED_CONTENT_TYPES': {'image/jpeg', 'image/png', 'image/gif'},
#     'PRIVATE_S3_STORAGE_URL_EXPIRATION': 3600,
#     'AWS_S3_ON': getattr(django_settings, 'AWS_S3_ON', False),
#     'AWS_REGION': getattr(django_settings, 'AWS_REGION', None),
# }
# class Settings:
#     def __getattr__(self, attr):


# Path: chamber/storages/boto3.py
import boto3
from botocore.client import Config
from django.core.files.base import ContentFile
from django.core.files.storage import DefaultStorage
from storages.backends.s3boto3 import S3Boto3Storage
from chamber.config import settings






__all__ = (
    'BaseS3Storage',
    'BasePrivateS3Storage',
    'BasePrivateS3DataStorage',
    'force_bytes_content',
    'get_storage_instance',
)


def force_bytes_content(content, blocksize=1024):
    """Returns a tuple of content (file-like object) and bool indicating wheter the content has been casted or not"""
    block = content.read(blocksize)
    content.seek(0)

    if not isinstance(block, bytes):
        _content = bytes(
            content.read(),
            'utf-8' if not hasattr(content, 'encoding') or content.encoding is None else content.encoding,
        )
        return ContentFile(_content), True
    return content, False


def get_storage_class(s3_storage_class, default_storage_class=DefaultStorage):

    return s3_storage_class if settings.AWS_S3_ON else default_storage_class