def download(request, token):
context = {}
if request.method == 'GET':
try:
# check token in database and retrieve corresponding music
targetQuery = MusicQuery.objects.get(token=token)
targetMusic = targetQuery.query
file_name = targetMusic.file_name
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = dir_path + '/music/' + file_name
file_wrapper = FileWrapper(file(file_path, 'rb'))
file_mimetype = mimetypes.guess_type(file_path)
# store the download record
newDownload = Download.objects.create(music=targetMusic, download_loc='')
newDownload.save()
# download the music file
response = HttpResponse(file_wrapper, content_type=file_mimetype)
response['X-Sendfile'] = file_path
response['Content-Length'] = os.stat(file_path).st_size
response['Content-Disposition'] = 'attachment; filename=' + file_name
targetQuery.delete()
return response
except Exception as e:
messages.add_message(request, messages.ERROR, "Music Download Failed: QR Code Expired")
logging.exception("message")
return redirect('/musician')
else:
return redirect('/musician')
# artist upload handler
评论列表
文章目录