I'm trying to make a private file host and I have it set to return a send_file() when the, for example, /media/someSong.mp3
url is called.
But I want to make it more styled so when you visit /media/someSong.mp3
, instead of your browser trying to play that MP3 in it, it will use a HTML page with a tag that parses the send_file() sent by an argument.
Currently, I have this:
@app.route('/media/<mediaID>')
def mediaStuff(mediaID):
try:
return render_template('parse-it.html', title=title, media_file=send_file(f'./media/{mediaID}'))
# return send_file(f'./media/{mediaID}')
except FileNotFoundError:
return 'The media you tried to view doesn\'t exist.'
And here is what I have in my templates/parse-it.html
file:
{% extends "base.html" %}
{% block content %}
<h1>You are now viewing ...!</h1>
<source src={{media_file}}>
{% endblock %}
However all I see is <Response 200 OK> or something like that. No file to be seen!
Would I use a get_file() and then return Response(getfileVar) in that media_file argument?
send_file
returns aResponse
object. It makes no sense to pass this to the template. So you need two endpoints. One to display your website containing the media element and one to stream the file from the directory. I recommend usingsend_from_directory
for this.