Skip to content

Commit 655b092

Browse files
committed
DownloadProvider now uses the final URL (after following redirects) as its path
1 parent 051f550 commit 655b092

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/dialog/browser/download.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Download/upload provider
44
========================
55
6-
Copyright (c) 2024 Dannii Willis
6+
Copyright (c) 2025 Dannii Willis
77
MIT licenced
88
https://github.com/curiousdannii/asyncglk
99
@@ -30,8 +30,8 @@ export class DownloadProvider implements Provider {
3030
}
3131

3232
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)
3535
this.store.set(path, data)
3636
return path
3737
}
@@ -77,9 +77,10 @@ export class DownloadProvider implements Provider {
7777
}
7878

7979
/** 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]> {
8181
// Handle a relative URL
8282
const story_url = new URL(url, document.URL)
83+
url = story_url + ''
8384
const story_domain = story_url.hostname
8485
const same_protocol = story_url.protocol === document.location.protocol
8586
const same_domain = story_domain === document.location.hostname
@@ -93,7 +94,7 @@ export async function fetch_storyfile(options: DownloadOptions, url: string, pro
9394
if (node.type.endsWith(';gzip')) {
9495
data = gunzipSync(data)
9596
}
96-
return data
97+
return [url, data]
9798
}
9899

99100
// 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
113114
if (direct_access) {
114115
try {
115116
response = await fetch('' + story_url)
117+
// If we were redirected get the final URL
118+
url = response.url ?? url
116119
}
117120
// We can't specifically detect CORS errors but that's probably what happened
118121
catch {
@@ -124,6 +127,8 @@ export async function fetch_storyfile(options: DownloadOptions, url: string, pro
124127
else {
125128
if (options.use_proxy && options.proxy_url) {
126129
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
127132
}
128133
else {
129134
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
146151
throw new Error('Abnormal JSified story')
147152
}
148153

149-
return parse_base64(matched[1])
154+
return [url, await parse_base64(matched[1])]
150155
}
151156

152-
return data
157+
return [url, data]
153158
}
154159

155160
/** Read an uploaded file and return it as a Uint8Array */

0 commit comments

Comments
 (0)