Django: Serving Media Behind Custom URL
Posted
by TheLizardKing
on Stack Overflow
See other posts from Stack Overflow
or by TheLizardKing
Published on 2010-04-22T02:53:35Z
Indexed on
2010/04/22
3:13 UTC
Read the original article
Hit count: 349
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. http://stackoverflow.com/questions/2681338/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?
EDIT 1 - ADDITIONAL INFO
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.
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
© Stack Overflow or respective owner