|
| 1 | +import { |
| 2 | + type AbstractModrinthClient, |
| 3 | + type DownloadSink, |
| 4 | + getNodeBaseUrl, |
| 5 | + ModrinthApiError, |
| 6 | +} from '@modrinth/api-client' |
| 7 | +import type { FileDownloadProvider } from '@modrinth/ui' |
| 8 | +import { Channel } from '@tauri-apps/api/core' |
| 9 | + |
| 10 | +import { |
| 11 | + type ExternalFileError, |
| 12 | + type ExternalFileProgress, |
| 13 | + releaseExternal, |
| 14 | + saveExternal, |
| 15 | + selectExternal, |
| 16 | +} from '@/platform/app-lib/files/commands' |
| 17 | + |
| 18 | +export interface FileDownloadTask { |
| 19 | + update: (progress: ExternalFileProgress) => void |
| 20 | + succeed: (filename: string) => void |
| 21 | + fail: (error: string) => void |
| 22 | + canceled: () => void |
| 23 | +} |
| 24 | + |
| 25 | +/** Registers a running download so the app can display its progress and cancel it. */ |
| 26 | +export type TrackFileDownload = ( |
| 27 | + saveId: string, |
| 28 | + filename: string, |
| 29 | + onCancel: () => Promise<void>, |
| 30 | + server: { serverId?: string; serverName?: string }, |
| 31 | +) => FileDownloadTask |
| 32 | + |
| 33 | +export function createFileDownload( |
| 34 | + client: AbstractModrinthClient, |
| 35 | + trackDownload: TrackFileDownload, |
| 36 | +): FileDownloadProvider { |
| 37 | + return { |
| 38 | + async download(file) { |
| 39 | + const saveId = await selectExternal( |
| 40 | + file.filename, |
| 41 | + file.type === 'server-backup' || file.type === 'server-world' |
| 42 | + ? { name: 'ZIP archive', extensions: ['zip'] } |
| 43 | + : null, |
| 44 | + ).catch((error: ExternalFileError) => { |
| 45 | + throw new Error(error.message ?? String(error)) |
| 46 | + }) |
| 47 | + if (!saveId) return false |
| 48 | + |
| 49 | + let cancelled = false |
| 50 | + let finished = false |
| 51 | + let savedFilename = file.filename |
| 52 | + const task = trackDownload( |
| 53 | + saveId, |
| 54 | + file.filename, |
| 55 | + async () => { |
| 56 | + cancelled = true |
| 57 | + await releaseExternal(saveId) |
| 58 | + }, |
| 59 | + { serverId: file.serverId, serverName: file.serverName }, |
| 60 | + ) |
| 61 | + const onProgress = new Channel<ExternalFileProgress>((progress) => { |
| 62 | + if (cancelled || finished) return |
| 63 | + task.update(progress) |
| 64 | + }) |
| 65 | + |
| 66 | + const saveUrl = async (url: string, headers: Record<string, string> = {}) => { |
| 67 | + if (cancelled) throw new Error('Download cancelled') |
| 68 | + try { |
| 69 | + savedFilename = await saveExternal(saveId, { url, headers }, onProgress) |
| 70 | + } catch (error) { |
| 71 | + const nativeError = error as ExternalFileError |
| 72 | + throw new ModrinthApiError(nativeError.message ?? String(error), { |
| 73 | + statusCode: nativeError.statusCode, |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + const sink: DownloadSink = (url, options) => saveUrl(url, options.headers) |
| 78 | + |
| 79 | + try { |
| 80 | + switch (file.type) { |
| 81 | + case 'server-file': { |
| 82 | + const { token } = await client.kyros.files_v1.authorizeFileDownload( |
| 83 | + file.nodeUrlHost, |
| 84 | + file.worldId, |
| 85 | + file.path, |
| 86 | + ) |
| 87 | + await saveUrl( |
| 88 | + client.kyros.files_v1.getFileDownloadUrl(file.nodeUrlHost, file.worldId, token), |
| 89 | + ) |
| 90 | + break |
| 91 | + } |
| 92 | + case 'server-world': { |
| 93 | + const { token } = await client.kyros.files_v1.authorizeFullWorldDownload( |
| 94 | + file.nodeUrlHost, |
| 95 | + file.worldId, |
| 96 | + ) |
| 97 | + await client.download( |
| 98 | + `/worlds/${encodeURIComponent(file.worldId)}/files/download-full-zip`, |
| 99 | + { |
| 100 | + api: getNodeBaseUrl(file.nodeUrlHost), |
| 101 | + version: 'v1', |
| 102 | + params: { token }, |
| 103 | + skipAuth: true, |
| 104 | + }, |
| 105 | + sink, |
| 106 | + ) |
| 107 | + break |
| 108 | + } |
| 109 | + case 'server-backup': { |
| 110 | + const { token } = await client.kyros.backups_v1.authorizeDownload( |
| 111 | + file.nodeUrlHost, |
| 112 | + file.worldId, |
| 113 | + file.backupId, |
| 114 | + ) |
| 115 | + await saveUrl( |
| 116 | + client.kyros.backups_v1.getDownloadUrl( |
| 117 | + file.nodeUrlHost, |
| 118 | + file.worldId, |
| 119 | + file.backupId, |
| 120 | + token, |
| 121 | + ), |
| 122 | + ) |
| 123 | + break |
| 124 | + } |
| 125 | + } |
| 126 | + task.succeed(savedFilename) |
| 127 | + return true |
| 128 | + } catch (error) { |
| 129 | + if (cancelled) { |
| 130 | + task.canceled() |
| 131 | + return false |
| 132 | + } |
| 133 | + task.fail(error instanceof Error ? error.message : String(error)) |
| 134 | + throw error |
| 135 | + } finally { |
| 136 | + finished = true |
| 137 | + await releaseExternal(saveId).catch(() => undefined) |
| 138 | + } |
| 139 | + }, |
| 140 | + } |
| 141 | +} |
0 commit comments