"""Cloud browser views."""
import cloudfiles as cf

from django.http import Http404
from django.shortcuts import render_to_response
from django.template import RequestContext

from cloud_browser.common import SEP, path_parts, path_join, path_yield
from cloud_browser.cloud import get_connection, CloudObject


def _get_objects(container_obj, object_path):
    """Get object information."""

    object_limit = 20
    object_path = object_path + SEP if object_path else ''
    object_infos = container_obj.list_objects_info(
        limit=object_limit, delimiter=SEP, prefix=object_path)

    return [CloudObject.from_info(container_obj, x) for x in object_infos]


def _breadcrumbs(path):
    """Return breadcrumb dict from path."""

    full = None
    crumbs = []
    for part in path_yield(path):
        full = path_join(full, part) if full else part
        crumbs.append((full, part))

    return crumbs


def browser(request, path='', template="cloud_browser/browser.html"):
    """View files in a file path.

    :param request: The request.
    :param path: Path to resource, including container as first part of path.
    :param template: Template to render.
    """
    container_path, object_path = path_parts(path)
    container_infos = None
    object_infos = None
    conn = get_connection()

    if container_path == '':
        # List containers.
        container_infos = conn.list_containers_info()

    else:
        try:
            container_obj = conn.get_container(container_path)
        except cf.errors.NoSuchContainer:
            raise Http404("No container at: %s" % container_path)

        object_infos = _get_objects(container_obj, object_path)
        if not object_infos:
            raise Http404("No objects at: %s" % object_path)

        # TODO: Have a 'view object' if get single object succeeds.

    return render_to_response(template,
                              {'path': path,
                               'breadcrumbs': _breadcrumbs(path),
                               'container_path': container_path,
                               'container_infos': container_infos,
                               'object_path': object_path,
                               'object_infos': object_infos},
                              context_instance=RequestContext(request))


def view(_, path=''):
    """View single file from path.

    :param request: The request.
    :param path: Path to resource, including container as first part of path.
    :param template: Template to render.
    """
    container_path, object_path = path_parts(path)
    conn = get_connection()
    try:
        container_obj = conn.get_container(container_path)
    except cf.errors.NoSuchContainer:
        raise Http404("No container at: %s" % container_path)

    try:
        file_obj = container_obj.get_object(object_path)
    except cf.errors.NoSuchObject:
        raise Http404("No object at: %s" % object_path)

    print(file_obj)
    raise Http404("TODO: IMPLEMENT ME: %s" % object_path)

@@ -68,16 +68,30 @@
 p import
+ HttpResponse,
  Http404
@@ -2332,17 +2332,23 @@
 ef view(
-_
+request
 , path='
@@ -2539,32 +2539,54 @@
 render.%0A    %22%22%22%0A
+    import mimetypes%0A%0A
     container_pa
@@ -2983,13 +2983,94 @@
     
-print
+content_type = file_obj.content_type%0A    guessed_type, encoding = mimetypes.guess_type
 (fil
@@ -3078,64 +3078,460 @@
 _obj
+.name
 )%0A    
-raise Http404(%22TODO: IMPLEMENT ME: %25s%22 %25 object_path)
+if content_type in (None, '', 'application/octet-stream'):%0A        content_type = guessed_type%0A%0A    response = HttpResponse(content=file_obj.read(),%0A                            content_type=content_type)%0A%0A    # Check if compression allowed. If so, set up pass through%0A    use_gzip = 'gzip' in request.META.get(%22HTTP_ACCEPT_ENCODING%22, '')%0A    if use_gzip and encoding == 'gzip':%0A        response%5B'Content-Encoding'%5D = 'gzip'%0A%0A    return response
 %0A

Add content type / encoding inference and pass through.