def _determine_content_type()

in airavata_django_portal_sdk/user_storage/api.py [0:0]


def _determine_content_type(full_path, content_type=None, backend=None):
    result = content_type
    if result is None:
        # Try to guess the content-type from file extension
        guessed_type, encoding = mimetypes.guess_type(full_path)
        result = guessed_type
    if result is None or result == "application/octet-stream":
        # Check if file is Unicode text by trying to read some of it
        try:
            if backend is not None:
                with backend.open(full_path) as file:
                    # Try to decode the first kb as UTF8
                    file.read(1024).decode('utf-8')
                    result = "text/plain"
        except UnicodeDecodeError:
            logger.debug(f"Failed to read as Unicode text: {full_path}")
    return result