3
3
Download/upload provider
4
4
========================
5
5
6
- Copyright (c) 2024 Dannii Willis
6
+ Copyright (c) 2025 Dannii Willis
7
7
MIT licenced
8
8
https://github.com/curiousdannii/asyncglk
9
9
@@ -30,8 +30,8 @@ export class DownloadProvider implements Provider {
30
30
}
31
31
32
32
async download ( url : string , progress_callback ?: ProgressCallback ) : Promise < string > {
33
- const data = await fetch_storyfile ( this . options , url , progress_callback )
34
- const path = url_to_path ( url )
33
+ const [ final_url , data ] = await fetch_storyfile ( this . options , url , progress_callback )
34
+ const path = url_to_path ( final_url )
35
35
this . store . set ( path , data )
36
36
return path
37
37
}
@@ -77,9 +77,10 @@ export class DownloadProvider implements Provider {
77
77
}
78
78
79
79
/** Fetch a storyfile, using the proxy if necessary, and handling JSified stories */
80
- export async function fetch_storyfile ( options : DownloadOptions , url : string , progress_callback ?: ProgressCallback ) : Promise < Uint8Array > {
80
+ async function fetch_storyfile ( options : DownloadOptions , url : string , progress_callback ?: ProgressCallback ) : Promise < [ string , Uint8Array ] > {
81
81
// Handle a relative URL
82
82
const story_url = new URL ( url , document . URL )
83
+ url = story_url + ''
83
84
const story_domain = story_url . hostname
84
85
const same_protocol = story_url . protocol === document . location . protocol
85
86
const same_domain = story_domain === document . location . hostname
@@ -93,7 +94,7 @@ export async function fetch_storyfile(options: DownloadOptions, url: string, pro
93
94
if ( node . type . endsWith ( ';gzip' ) ) {
94
95
data = gunzipSync ( data )
95
96
}
96
- return data
97
+ return [ url , data ]
97
98
}
98
99
99
100
// Only directly access files same origin files or those from the list of reliable domains
@@ -113,6 +114,8 @@ export async function fetch_storyfile(options: DownloadOptions, url: string, pro
113
114
if ( direct_access ) {
114
115
try {
115
116
response = await fetch ( '' + story_url )
117
+ // If we were redirected get the final URL
118
+ url = response . url ?? url
116
119
}
117
120
// We can't specifically detect CORS errors but that's probably what happened
118
121
catch {
@@ -124,6 +127,8 @@ export async function fetch_storyfile(options: DownloadOptions, url: string, pro
124
127
else {
125
128
if ( options . use_proxy && options . proxy_url ) {
126
129
response = await fetch ( `${ options . proxy_url } ?url=${ story_url } ` )
130
+ // If the proxy was redirected get the final URL
131
+ url = response . headers . get ( 'Final-Url' ) ?? url
127
132
}
128
133
else {
129
134
throw new Error ( 'Storyfile not in list of direct domains and proxy disabled' )
@@ -146,10 +151,10 @@ export async function fetch_storyfile(options: DownloadOptions, url: string, pro
146
151
throw new Error ( 'Abnormal JSified story' )
147
152
}
148
153
149
- return parse_base64 ( matched [ 1 ] )
154
+ return [ url , await parse_base64 ( matched [ 1 ] ) ]
150
155
}
151
156
152
- return data
157
+ return [ url , data ]
153
158
}
154
159
155
160
/** Read an uploaded file and return it as a Uint8Array */
0 commit comments