Django:在自定义网址后面投放媒体 [英] Django: Serving Media Behind Custom URL

查看:113
本文介绍了Django:在自定义网址后面投放媒体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我当然知道,通过Django提供的静态文件将直接发送给你,但是我对如何使用自定义URL来隐藏使用Django的文件的真实位置感到困惑。 Django:在通用视图中提供下载,但答案我接受似乎是错误的做事方式。

So I of course know that serving static files through Django will send you straight to hell but I am confused on how to use a custom url to mask the true location of the file using Django. Django: Serving a Download in a Generic View but the answer I accepted seems to be the "wrong" way of doing things.

urls.py:

url(r'^song/(?P<song_id>\d+)/download/$', song_download, name='song_download'),

views.py:

def song_download(request, song_id):
    song = Song.objects.get(id=song_id)
    fsock = open(os.path.join(song.path, song.filename))

    response = HttpResponse(fsock, mimetype='audio/mpeg')
    response['Content-Disposition'] = "attachment; filename=%s - %s.mp3" % (song.artist, song.title)

    return response

此解决方案完美的运行但不完全足够,事实证明。

This solution works perfectly but not perfectly enough it turns out. How can I avoid having a direct link to the mp3 while still serving through nginx/apache?

编辑1 - 附加信息

目前,我可以通过使用以下地址获取我的文件:
http://www.example.com/music/song/1692/下载/
但是上面提到的方法是魔鬼的工作。

Currently I can get my files by using an address such as: http://www.example.com/music/song/1692/download/ But the above mentioned method is the devil's work.

我还能做些什么, / apache服务媒体?这是应该在网络服务器级别完成的吗?一些疯狂的mod_rewrite?

How can I accomplished what I get above while still making nginx/apache serve the media? Is this something that should be done at the webserver level? Some crazy mod_rewrite?

http://static.example.com/music/Aphex%20Twin%20-%20Richard%20D.%20James %20(V0)/10%20Logon-Rock%20Witch.mp3

编辑2 - 其他附加信息

我使用nginx作为我的前端和反向代理back apache /开发服务器,所以我想如果它需要某种mod_rewrite工作,我将不得不找到可以工作的东西与nginx。

I use nginx for my frontend and reverse proxy back apache/development server so I think if it does require some sort of mod_rewrite work I will have to find something that would work with nginx.

推荐答案

要扩展以前的答案,您应该可以修改以下代码,并让nginx直接提供您的下载文件仍然保护文件。

To expand on the previous answers you should be able to modify the following code and have nginx directly serve your download files whilst still having the files protected.

首先添加一个位置,如:

First of all add a location such as :

location /files/ {
   alias /true/path/to/mp3/files/;
   internal;
}

到您的nginx.conf文件(内部使此不能直接访问)。那么你需要一个Django View这样的东西:

to your nginx.conf file (the internal makes this not directly accessible). Then you need a Django View something like this:

def song_download(request, song_id):
    try:
        song = Song.objects.get(id=song_id)
        response = HttpResponse()
        response['Content-Type'] = 'application/mp3'
        response['X-Accel-Redirect'] = '/files/' + song.filenamename
        response['Content-Disposition'] = 'attachment;filename=' + song.filename
    except Exception:
        raise Http404
    return response

将把文件下载到nginx。

which will hand off the file download to nginx.

这篇关于Django:在自定义网址后面投放媒体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆