pyramidkotti

How do I serve this video file for download using pyramid FileResponse


Please what am i missing. I keep on getting internal server error in browser when I want to serve this video file that has been downloaded on disk. here is my code: The view function

@view_config(name='download_video')
def dl(request):
    url = request.params.get('url')
    camefrom = request.params.get('came_from')
    path_dir = request.registry.settings['app.download_path']
    result= save_to_disk(url, path_dir)
    if result:
        filename = result['filename']
        filepath = os.path.abspath(os.path.join(path_dir,filename))
        file_exists = os.path.exists(filepath)
        if file_exists:
            res = FileResponse(filepath,content_type='video/mp4')
            res.headers['Content-Length'] = os.path.getsize(filepath)
            res.headers['Content-Disposition'] = 'attachment;filename=%s'%filename
            return res
    request.session.flash(_("Error downloading video, please check your network","danger"))
    return HTTPFound(location=camefrom)

The template

<a tal:attributes="href api.url(api.root, '@@download_video', query={'came_from': request.url,'url':context.video_url})" class="btn">Download</a>

The video downloads and I can see it in the folder but I keep on getting internal server error without tracebacks on browser

My app depends on Kotti cms


Solution

  • I'm m not sure what's going on with your Pyramid logging but here is my view for downloading a file. I'm currently on an iPhone so I just cut and pasted but you should get the idea.

    @view_config(route_name="download_view", renderer="")
    def server_view1010(request):
        filepath = os.path.join(CFG.home_dir, "static/graphics/imgs/img_sample.jpg")
        if request.storage.exists(filepath):                                                     #there is no overwrite
            response = FileResponse(filepath)
            response.headers['Content-Disposition'] = ("attachment; filename=img_sample.jpg")
            return response
        else:
            return Response("Unable to find: {}".format(request.path_info))