Skip to content

Commit c8c01b0

Browse files
committed
fix: read file name from Content-Disposition header and override title bar
1 parent cc9afb0 commit c8c01b0

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

scripts/control_create/control_create.gml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ function control_create() {
563563
song_total_size = -1
564564
song_download_status = 0
565565
song_download_file = ""
566+
song_download_display_name = ""
566567

567568
// Delete old installer
568569
if (file_exists_lib(update_file)) {
@@ -645,7 +646,7 @@ function control_create() {
645646
song_download_file = filename_change_ext(downloaded_song_file, song_download_ext)
646647

647648
// Set displayed filename to the server's file name
648-
filename = string_copy(download_url_noparams, string_last_pos("/", download_url_noparams), string_length(download_url_noparams));
649+
song_download_display_name = string_copy(download_url_noparams, string_last_pos("/", download_url_noparams) + 1, string_length(download_url_noparams));
649650

650651
// Init download
651652
song_download_data = http_get_file(download_url, song_download_file);

scripts/control_draw/control_draw.gml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function control_draw() {
2828
showmenu = 0
2929
cursmarker = 0
3030
compx = 160
31-
window_set_caption(condstr((filename = "" || filename = "-player") && (midiname = "" || !isplayer), condstr(language != 1, "Unsaved song", "新文件")) + condstr(filename != "-player", filename_name(filename)) + condstr((filename = "" || filename = "-player") && midiname != "" && isplayer, midiname) + condstr(changed && filename != "" && filename != "-player", "*") + " - Minecraft Note Block Studio" + condstr(isplayer, " - Player Mode"))
31+
window_set_caption(condstr((protocol_data != pointer_null), song_download_display_name, condstr((filename = "" || filename = "-player") && (midiname = "" || !isplayer), condstr(language != 1, "Unsaved song", "新文件")) + condstr(filename != "-player", filename_name(filename)) + condstr((filename = "" || filename = "-player") && midiname != "" && isplayer, midiname) + condstr(changed && filename != "" && filename != "-player", "*")) + " - Minecraft Note Block Studio" + condstr(isplayer, " - Player Mode"))
3232
// Performance indicator: "(" + string_format(currspeed * 100, 1, 0) + "%) "
3333
draw_set_alpha(1)
3434
draw_theme_color()
@@ -2146,7 +2146,7 @@ function control_draw() {
21462146
draw_set_halign(fa_left)
21472147
draw_theme_color()
21482148
draw_theme_font(font_info_med)
2149-
draw_text_dynamic(rw / 2 - 200, rh / 2 - 80, condstr(filename != "-player", filename_name(filename)) + condstr((filename = "" || filename = "-player") && midiname != "", midiname), true)
2149+
draw_text_dynamic(rw / 2 - 200, rh / 2 - 80, condstr((protocol_data != pointer_null), song_download_display_name, condstr(filename != "-player", filename_name(filename)) + condstr((filename = "" || filename = "-player") && midiname != "", midiname), true))
21502150
draw_theme_font(font_main)
21512151
dropalpha = 1
21522152
} else {

scripts/download_song_from_url/download_song_from_url.gml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,30 @@ function download_song_from_url() {
2828
// To avoid this from happening, we compare the Content-Length response header with the downloaded file's disk size.
2929
var headers = async_load[? "response_headers"];
3030
var contentLength = -1;
31+
var contentDisposition = "";
3132
if (headers > 0) {
3233
contentLength = headers[? "Content-Length"];
34+
contentDisposition = headers[? "Content-Disposition"]
3335
}
3436
var writtenFileSize = file_get_size(song_download_file);
3537

38+
// Read file name from Content-Disposition header, if present
39+
var override_fn = "";
40+
if (!is_undefined(contentDisposition) && string_count("attachment; filename=", contentDisposition) > 0) { // attachment; filename="<song.nbs>"
41+
show_debug_message("Content-Disposition: " + contentDisposition)
42+
43+
var firstQuotePos = string_pos("\"", contentDisposition) + 1;
44+
var lastQuotePos = string_last_pos("\"", contentDisposition);
45+
override_fn = string_copy(contentDisposition, firstQuotePos, lastQuotePos - firstQuotePos);
46+
}
47+
3648
if (contentLength > 0 && writtenFileSize == contentLength) {
3749
song_downloaded_size = song_total_size; // prevent freezing under 100%
38-
load_song(song_download_file);
50+
show_debug_message(override_fn);
51+
load_song(song_download_file, true); // load as backup file (keep unsaved, don't add to recent etc.)
52+
if (override_fn != "") {
53+
song_download_display_name = override_fn; // override title bar display name
54+
}
3955
files_delete_lib(song_download_file);
4056
} else {
4157
if (language != 1) {

0 commit comments

Comments
 (0)