# Path: solo/web/ctx.py
# LOGGER = logging.getLogger('solo.web')
# class GreenletServing(object):
# class _ThreadLocalProxy(object):
#     def __init__(self):
#     def load(self, request, response):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def clear(self):
#     def __init__(self, attrname):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __delattr__(self, name):
#     def _get_dict(self):
#     def __getitem__(self, key):
#     def __setitem__(self, key, value):
#     def __delitem__(self, key):
#     def __contains__(self, key):
#     def __len__(self):
#     def __nonzero__(self):


import logging
import mimetypes
import os
import os.path
from os.path import (
    getmtime,
    getsize
)
from webob import exc
from solo.web.ctx import request, response

class AssetController(object):

    def __init__(self, root, default_filename=None, block_size=_BLOCK_SIZE):
        self.root = root
        self.default_filename = default_filename
        self.block_size = block_size or _BLOCK_SIZE

    def get_content_type(self, absolute_path):
        mine_type, encoding = mimetypes.guess_type(absolute_path)
        mine_type = mine_type or 'application/octet-stream'
        return mine_type

    def asset(self, path):
        if request.method == 'HEAD':
            return self.get(path, False)
        return self.get(path)

    def head(self, path):
        self.get(path, load=False)

    def get(self, path, load=True):
        LOGGER.info('request.path_info:%s', request.path_info)
        path = self.parse_url_path(path)
        absolute_path = self.get_absolute_path(self.root, path)
        absolute_path = self.check_absolute_path(self.root, absolute_path)
        if absolute_path is None:
            raise exc.HTTPNotFound('The asset file not found')

        # !!! must initialize the content type and set codintional response firstly

# Path: solo/web/ctx.py
# LOGGER = logging.getLogger('solo.web')
# class GreenletServing(object):
# class _ThreadLocalProxy(object):
#     def __init__(self):
#     def load(self, request, response):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def clear(self):
#     def __init__(self, attrname):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __delattr__(self, name):
#     def _get_dict(self):
#     def __getitem__(self, key):
#     def __setitem__(self, key, value):
#     def __delitem__(self, key):
#     def __contains__(self, key):
#     def __len__(self):
#     def __nonzero__(self):


# Path: solo/web/asset.py
import logging
import mimetypes
import os
import os.path
from os.path import (
    getmtime,
    getsize
)
from webob import exc
from solo.web.ctx import request, response


class AssetController(object):

    def __init__(self, root, default_filename=None, block_size=_BLOCK_SIZE):
        self.root = root
        self.default_filename = default_filename
        self.block_size = block_size or _BLOCK_SIZE

    def get_content_type(self, absolute_path):
        mine_type, encoding = mimetypes.guess_type(absolute_path)
        mine_type = mine_type or 'application/octet-stream'
        return mine_type

    def asset(self, path):
        if request.method == 'HEAD':
            return self.get(path, False)
        return self.get(path)

    def head(self, path):
        self.get(path, load=False)

    def get(self, path, load=True):
        LOGGER.info('request.path_info:%s', request.path_info)
        path = self.parse_url_path(path)
        absolute_path = self.get_absolute_path(self.root, path)
        absolute_path = self.check_absolute_path(self.root, absolute_path)
        if absolute_path is None:
            raise exc.HTTPNotFound('The asset file not found')

        # !!! must initialize the content type and set codintional response firstly

        response.content_type = self.get_content_type(absolute_path)