This is what I came up with (or pieced together, I don't remember
now):

from django.core.servers.basehttp import FileWrapper

class FLVWrapper(FileWrapper):
    """Wrapper to return a file-like object iterably"""

    def __init__(self, filelike):
        self.first_time = True
        #Calls the parent class __init__ function
        super(FLVWrapper, self).__init__(filelike)

    def next(self):
        if not self.first_time:
            data = self.filelike.read(self.blksize)
        else:
            data = "FLV\x01\x01\x00\x00\x00\x09\x00\x00\x00\x09"
            data += self.filelike.read(self.blksize)
        self.first_time = False
        if data:
            return data
        raise StopIteration

Then you'll do something like this:
    (Get a file-like object "response_file", using something like
open() or urllib2.urlopen() or something of the sort)
    response = HttpResponse(FLVWrapper(response_file),
mimetype=response_mimetype)
    response['Content-Length'] = calculated_content_length
    return response

You'll need to make sure that when you create the HttpResponse object,
you add the correct content length to the response (calculate:
original size + prepended data size). Otherwise you won't be able to
stream the data properly.

On Mar 16, 1:48 pm, bfrederi <brfrederi...@gmail.com> wrote:
> *I know that I should serve static files using a static file server
> that was built for doing such, but save me the lecture and indulge me
> on this please*
>
> I am serving large video files (180+MB) through HttpResponse in
> Django, and returning the file iteratively using Django's 
> FileWrapper:http://code.djangoproject.com/browser/django/trunk/django/core/server...
>
> I need to prepend some data on that video file before I return the
> file to the user, but I don't know how to do that without reading the
> entire file into memory using .read(). Is there any way I could extend
> FileWrapper to allow me to add data at the very beginning of the file,
> while it's allowing the video file to be read in chunks?
>
> Here is a simple example of what I am doing (my code has too much
> other crap going on, so I wrote this):
>
> import urllib2
> from django.http import HttpResponse
> from django.core.servers.basehttp import FileWrapper
>
> def huge_video_view(request):
>     response_handle = urllib2.urlopen('http://example.com/
> hugeassvideo.flv')
>     return HttpResponse(FileWrapper(response_handle), mimetype='video/
> x-flv')
>
> Thanks,
> Brandon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to