Skip to content

Commit 4dd5c55

Browse files
committed
fix downloading password protected maps
1 parent a913dbb commit 4dd5c55

3 files changed

Lines changed: 28 additions & 16 deletions

File tree

client/src/ui/actions.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,19 @@ export async function saveMap() {
1919
export async function downloadMap() {
2020
const rmap_ = get(rmap)
2121
const server_ = get(server)
22-
const httpUrl = server_.httpUrl
23-
download(`${httpUrl}/maps/${rmap_.map.name}`, `${rmap_.map.name}.map`)
22+
const name = rmap_.map.name
23+
24+
const id = showInfo(`Downloading '${name}'…`, 'none')
25+
try {
26+
const resp = await server_.fetch('maps/' + name)
27+
const data = await resp.blob()
28+
download(data, name + '.map')
29+
showInfo(`Downloaded '${name}'.`)
30+
} catch (e) {
31+
showError(`Failed to download ${name}: ${e}`)
32+
} finally {
33+
clearDialog(id)
34+
}
2435
}
2536

2637
export async function deleteMap() {

client/src/ui/lib/util.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,22 @@ import type { WebSocketServer } from '../../server/server'
1616
import type { Config, MapCreation, MapDetail } from '../../server/protocol'
1717
import * as MapDir from '../../twmap/mapdir'
1818
import { QuadsLayer } from '../../twmap/quadsLayer'
19-
import { clearDialog, showInfo } from './dialog'
2019

2120
export type Ctor<T> = new (...args: any[]) => T
2221

2322
export type FormEvent<T> = Event & { currentTarget: EventTarget & T }
2423
export type FormInputEvent = FormEvent<HTMLInputElement>
2524

26-
export async function download(path: string, name: string) {
27-
const id = showInfo(`Downloading '${name}'…`, 'none')
28-
try {
29-
const resp = await fetch(path)
30-
if (!resp.ok) throw await resp.text()
31-
const data = await resp.blob()
25+
export function download(data: Blob, name: string) {
3226
const url = URL.createObjectURL(data)
3327

3428
const link = document.createElement('a')
3529
link.href = url
36-
link.download = name
30+
link.download = name + '.map'
3731

3832
document.body.append(link)
3933
link.click()
4034
link.remove()
41-
showInfo(`Downloaded '${name}'.`)
42-
} finally {
43-
clearDialog(id)
44-
}
4535
}
4636

4737
export async function uploadMap(url: string, name: string, file: Blob, config: Partial<Config>) {

client/src/ui/routes/lobby.svelte

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,19 @@
220220
alert('TODO renaming maps is not yet implemented.')
221221
}
222222
223-
function onDownloadMap(name: string) {
224-
download(`${httpUrl}/maps/${name}`, `${name}.map`)
223+
async function onDownloadMap(name: string) {
224+
const id = showInfo(`Downloading '${name}'…`, 'none')
225+
try {
226+
const resp = await fetch(`${httpUrl}/maps/${name}`)
227+
if (!resp.ok) throw await resp.text()
228+
const data = await resp.blob()
229+
download(data, name + '.map')
230+
showInfo(`Downloaded '${name}'.`)
231+
} catch (e) {
232+
showError(`Failed to download ${name}: ${e}`)
233+
} finally {
234+
clearDialog(id)
235+
}
225236
}
226237
227238
function onAddServer() {

0 commit comments

Comments
 (0)