# Path: telemeta/views/core.py
# def serve_media(media_path, content_type="", buffering=True, streaming=False):
#     if not settings.DEBUG:
#         return nginx_media_accel(media_path, content_type=content_type,
#                                  buffering=buffering, streaming=streaming)
#     else:
#         try:
#             response = FileResponse(open(media_path, 'rb'))
#         except:    
#             response = StreamingHttpResponse(stream_from_file(media_path), content_type=content_type)
#         filename = os.path.basename(media_path)
#         if not streaming:
#             response['Content-Disposition'] = 'attachment; ' + 'filename=' + filename
#         return response


import telemeta
        import zipstream
        import json
from telemeta.views.core import *
from telemeta.views.core import serve_media
from telemeta.views.epub import *
        from django.http import HttpResponse, StreamingHttpResponse
        from telemeta.views import MarkerView
        from telemeta.backup import CollectionSerializer
        from zipfile import ZIP_DEFLATED, ZIP_STORED
        from django.db.models import get_models

    def collection_playlist(self, request, public_id, template, mimetype):
        try:
            collection = MediaCollection.objects.get(public_id=public_id)
        except ObjectDoesNotExist:
            raise Http404

        template = loader.get_template(template)
        context = RequestContext(request, {'collection': collection, 'host': request.META['HTTP_HOST']})
        return HttpResponse(template.render(context), content_type=mimetype)

    @method_decorator(permission_required('telemeta.delete_mediacollection'))
    def collection_delete(self, request, public_id):
        """Delete a given collection"""
        collection = MediaCollection.objects.get(public_id=public_id)
        revisions = Revision.objects.filter(element_type='collection', element_id=collection.id)
        for revision in revisions:
            revision.delete()
        collection.delete()
        return redirect('telemeta-collections')

    def related_media_collection_stream(self, request, public_id, media_id):
        collection = MediaCollection.objects.get(public_id=public_id)
        media = MediaCollectionRelated.objects.get(collection=collection, id=media_id)
        response = serve_media(media.file.path, content_type=media.mime_type)
        return response

    def related_media_collection_download(self, request, public_id, media_id):
        collection = MediaCollection.objects.get(public_id=public_id)
        media = MediaCollectionRelated.objects.get(collection=collection, id=media_id)

# Path: telemeta/views/core.py
# def serve_media(media_path, content_type="", buffering=True, streaming=False):
#     if not settings.DEBUG:
#         return nginx_media_accel(media_path, content_type=content_type,
#                                  buffering=buffering, streaming=streaming)
#     else:
#         try:
#             response = FileResponse(open(media_path, 'rb'))
#         except:    
#             response = StreamingHttpResponse(stream_from_file(media_path), content_type=content_type)
#         filename = os.path.basename(media_path)
#         if not streaming:
#             response['Content-Disposition'] = 'attachment; ' + 'filename=' + filename
#         return response


# Path: telemeta/views/collection.py
import telemeta
        import zipstream
        import json
from telemeta.views.core import *
from telemeta.views.core import serve_media
from telemeta.views.epub import *
        from django.http import HttpResponse, StreamingHttpResponse
        from telemeta.views import MarkerView
        from telemeta.backup import CollectionSerializer
        from zipfile import ZIP_DEFLATED, ZIP_STORED
        from django.db.models import get_models


    def collection_playlist(self, request, public_id, template, mimetype):
        try:
            collection = MediaCollection.objects.get(public_id=public_id)
        except ObjectDoesNotExist:
            raise Http404

        template = loader.get_template(template)
        context = RequestContext(request, {'collection': collection, 'host': request.META['HTTP_HOST']})
        return HttpResponse(template.render(context), content_type=mimetype)

    @method_decorator(permission_required('telemeta.delete_mediacollection'))
    def collection_delete(self, request, public_id):
        """Delete a given collection"""
        collection = MediaCollection.objects.get(public_id=public_id)
        revisions = Revision.objects.filter(element_type='collection', element_id=collection.id)
        for revision in revisions:
            revision.delete()
        collection.delete()
        return redirect('telemeta-collections')

    def related_media_collection_stream(self, request, public_id, media_id):
        collection = MediaCollection.objects.get(public_id=public_id)
        media = MediaCollectionRelated.objects.get(collection=collection, id=media_id)
        response = serve_media(media.file.path, content_type=media.mime_type)
        return response

    def related_media_collection_download(self, request, public_id, media_id):
        collection = MediaCollection.objects.get(public_id=public_id)
        media = MediaCollectionRelated.objects.get(collection=collection, id=media_id)

        response = serve_media(media.file.path, content_type=media.mime_type)