@@ -7,9 +7,14 @@ function download_song_from_url() {
77
88 if (async_load[? " id" ] == song_download_data) {
99 var status = async_load[? " status" ];
10- if (status == 1 ) {
10+ show_debug_message (" Status: " + string (status));
11+
12+ if (status == 1 ) { // Downloading, if multiple packets are returned. The status may never be 1 if the server responds immediately
1113 song_downloaded_size = async_load[? " sizeDownloaded" ];
1214 song_total_size = async_load[? " contentLength" ];
15+
16+ // show_debug_message(string(song_downloaded_size) + " " + string(song_total_size) + " " + string(file_get_size(song_download_file)))
17+
1318 if (song_total_size > max_song_download_size) {
1419 message (" This file is too large to be opened via a URL! Please try downloading and manually opening the song." , " Error" );
1520 song_download_data = -1 ; // Cancel
@@ -21,7 +26,31 @@ function download_song_from_url() {
2126 // Download was interrupted, may have been successful or not (if connection was interrupted)
2227 song_download_data = -1 ;
2328 song_download_status = 0 ;
24- if (song_total_size > 0 && file_get_size (song_download_file) == song_total_size) {
29+
30+ // The sizeDownloaded and contentLength variables may never be reported if the song is downloaded in one go.
31+ // To avoid this from happening, we compare the Content-Length response header with the downloaded file's disk size.
32+ var headers = async_load[? " response_headers" ];
33+ var contentLength = -1 ;
34+ if (headers > 0 ) {
35+ contentLength = headers[? " Content-Length" ];
36+ }
37+ var writtenFileSize = file_get_size (song_download_file);
38+
39+ show_debug_message (contentLength);
40+ show_debug_message (writtenFileSize);
41+
42+ // show_debug_message("Downloaded: " + string(async_load[? "sizeDownloaded"]));
43+ // show_debug_message("Total: " + string(async_load[? "contentLength"]));
44+ //
45+ // var headers = ds_map_find_value(async_load, "response_headers")
46+ // show_debug_message("Headers: " + string(headers));
47+ // if (headers > 0) {
48+ // show_debug_message(ds_map_keys_to_array(headers));
49+ // show_debug_message(ds_map_values_to_array(headers));
50+ // }
51+ // show_debug_message(string(song_downloaded_size) + " " + string(song_total_size) + " " + string(file_get_size(song_download_file)))
52+
53+ if (contentLength > 0 && writtenFileSize == contentLength) {
2554 song_downloaded_size = song_total_size; // prevent freezing under 100%
2655 load_song (song_download_file);
2756 files_delete_lib (song_download_file);
0 commit comments