From ef5efc89e30d0cfdfbe1c15191b4b073ea3f9e9e Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Mon, 10 Oct 2011 18:57:50 +0100 Subject: [PATCH] add robust error checking on the /dl/ endpoint --- server/djrandom/frontend/views.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server/djrandom/frontend/views.py b/server/djrandom/frontend/views.py index 8c18737..d70ae5f 100644 --- a/server/djrandom/frontend/views.py +++ b/server/djrandom/frontend/views.py @@ -120,6 +120,30 @@ def download_song(sha1): mp3 = MP3.query.get(sha1) if not mp3: abort(404) + + # This is a good time to check round-trip inconsistencies: if the + # user is requesting a duplicate file, it means our SOLR index got + # out of sync, but we can still give him the file he wants. + if mp3.state == MP3.DUPLICATE: + dup_mp3 = MP3.query.get(mp3.duplicate_of) + if not dup_mp3: + log.error( + 'SEVERE: duplicate_of %s points to non-existing mp3 %s' % ( + mp3.sha1, mp3.duplicate_of)) + abort(404) + log.error( + 'inconsistency: user got duplicate song: %s (instead of %s)' % ( + mp3.sha1, mp3.duplicate_of)) + mp3 = dup_mp3 + + if mp3.state != MP3.READY: + log.error('inconsistency: user got non-ready song: %s' % mp3.sha1) + abort(404) + + if not os.path.exists(mp3.path): + log.error('SEVERE: file has disappeared for song: %s' % mp3.sha1) + abort(404) + if use_xsendfile: return download_song_xsendfile(mp3) else: -- GitLab