@bp.get("/song/<int:userid>/<int:songid>")
def song(userid, songid):
- if request.args.get("action", None) == "view":
+ action = request.args.get("action", None)
+ if action in ["view", "download"]:
try:
song = by_id(songid)
if song.userid != userid:
abort(404)
- return render_template(
- "song.html",
- songs=[song],
- song=song,
- **users.get_user_colors(userid))
+ if action == "view":
+ return render_template(
+ "song.html",
+ songs=[song],
+ song=song,
+ **users.get_user_colors(userid))
+ else: # download
+ return send_from_directory(
+ datadir.get_user_songs_path(userid), str(songid) + ".mp3", as_attachment=True, download_name=song.title + ".mp3")
except ValueError:
abort(404)
else:
// Handle link clicks with AJAX
document.querySelectorAll("a").forEach((anchor) => {
anchor.removeEventListener("click", onLinkClick);
- anchor.addEventListener("click", onLinkClick);
+ if (!anchor.download) {
+ anchor.addEventListener("click", onLinkClick);
+ }
});
// Handle form submissions with AJAX
{% if session["userid"] == song.userid -%}
<a href="/edit-song?songid={{ song.songid }}" class="song-list-button" title="Edit"><img class="lsp_btn_edit02" /></a>
<a href="/delete-song/{{ song.songid }}" class="song-list-button" onclick="return confirm('Are you sure you want to delete this song?')" title="Delete"><img class="lsp_btn_delete02" /></a>
+<a href="/song/{{ song.userid }}/{{ song.songid }}?action=download" class="song-list-button" download="{{ song.title }}.mp3" title="Download mp3 File"><img class="lsp_btn_download02" /></a>
{%- endif %}
</p>