diff --git a/packages/lib/src/file_ops.ts b/packages/lib/src/file_ops.ts index 16701808..7aa5d8df 100644 --- a/packages/lib/src/file_ops.ts +++ b/packages/lib/src/file_ops.ts @@ -2,7 +2,6 @@ * @module lib/file_ops */ import crypto from "crypto"; -import events from "events"; import fs from "node:fs/promises"; import path from "path"; import stream from "stream"; @@ -258,7 +257,8 @@ export async function createTempWriteStream(filePath: string) { async function discardPartialDownload(writeStream: stream.Writable, tempFilePath: string) { if (!writeStream.closed) { writeStream.destroy(); - await events.once(writeStream, "close"); + // Not events.once, it rejects if the stream is still erroring from the aborted pipe. + await new Promise(resolve => writeStream.once("close", resolve)); } try { await fs.unlink(tempFilePath); diff --git a/packages/web_ui/src/components/ModsPage.tsx b/packages/web_ui/src/components/ModsPage.tsx index 89cd26e7..23b86a1c 100644 --- a/packages/web_ui/src/components/ModsPage.tsx +++ b/packages/web_ui/src/components/ModsPage.tsx @@ -25,6 +25,7 @@ import Link from "./Link"; import ModDetails from "./ModDetails"; import { Dropzone } from "./Dropzone"; import UploadButton from "./UploadButton"; +import { useUploadProgress } from "./UploadProgress"; import { InputPartialVersion } from "./InputVersion"; const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare; @@ -524,6 +525,7 @@ export default function ModsPage() { let account = useAccount(); let control = useContext(ControlContext); let [mods] = useMods(); + const { customRequest, list: uploadList } = useUploadProgress(); let [modPacks] = useModPacks(); const modPackTable = useTableQueryState({ namespace: "modPack", defaultSortKey: "name", @@ -582,6 +584,7 @@ export default function ModsPage() { "X-Access-Token": control.connector.token || "", }} action={`${webRoot}api/upload-mod`} + customRequest={customRequest} > ; @@ -635,6 +638,7 @@ export default function ModsPage() { {uploadButton} } /> + {uploadList} diff --git a/packages/web_ui/src/components/SavesList.tsx b/packages/web_ui/src/components/SavesList.tsx index 3ec8863d..79d3e3ba 100644 --- a/packages/web_ui/src/components/SavesList.tsx +++ b/packages/web_ui/src/components/SavesList.tsx @@ -1,7 +1,7 @@ import React, { useContext, useState } from "react"; import { - message, Button, Checkbox, Form, Input, List, Modal, - Popconfirm, Progress, Select, Space, Table, Tooltip, Upload, + message, Button, Checkbox, Form, Input, Modal, + Popconfirm, Select, Space, Table, Tooltip, Upload, } from "antd"; import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined"; import LeftOutlined from "@ant-design/icons/LeftOutlined"; @@ -19,6 +19,7 @@ import { useSavesOfInstance } from "../model/saves"; import { notifyErrorHandler } from "../util/notify"; import { Dropzone } from "./Dropzone"; import UploadButton from "./UploadButton"; +import { useUploadProgress } from "./UploadProgress"; type ModalProps = { @@ -193,19 +194,12 @@ function TransferModal(props: ModalProps) { } -type File = { - uid: string; - name: string; - percent: number; - status: string; -}; - export default function SavesList(props: { instance: lib.InstanceDetails }) { let account = useAccount(); let control = useContext(ControlContext); let [saves] = useSavesOfInstance(props.instance.id); let [starting, setStarting] = useState(false); - let [uploadingFiles, setUploadingFiles] = useState([]); + const { customRequest, list: uploadList } = useUploadProgress(); const tableState = useTableQueryState({ namespace: "save", defaultSortKey: "mtimeMs", defaultSortOrder: "descend", pagination: { defaultPageSize: 10 }, }); @@ -309,38 +303,6 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) { }} />; - function onChange(changeEvent: any) { - if (["done", "error"].includes(changeEvent.file.status)) { - if (changeEvent.file.status === "error") { - notifyErrorHandler("Error uploading file")(changeEvent.file.error); - } - - let newUploadingFiles = [...uploadingFiles]; - let index = newUploadingFiles.findIndex(f => f.uid === changeEvent.file.uid); - if (index !== -1) { - newUploadingFiles.splice(index, 1); - setUploadingFiles(newUploadingFiles); - } - return; - } - - let file: File = { - uid: changeEvent.file.uid, - name: changeEvent.file.name, - percent: changeEvent.file.percent, - status: changeEvent.file.status, - }; - - let newUploadingFiles = [...uploadingFiles]; - let index = newUploadingFiles.findIndex(f => f.uid === changeEvent.file.uid); - if (index !== -1) { - newUploadingFiles[index] = file; - } else { - newUploadingFiles.push(file); - } - setUploadingFiles(newUploadingFiles); - } - let uploadProps = { disabled: hostOffline, name: "file", @@ -353,7 +315,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) { }, showUploadList: false, action: `${webRoot}api/upload-save`, - onChange, + customRequest, }; return
@@ -365,6 +327,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) { } {account.hasPermission("core.instance.save.create") && } } /> + {uploadList} { account.hasPermission("core.instance.save.upload") ? @@ -373,15 +336,5 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) { : saveTable } - {uploadingFiles.length ? - {uploadingFiles.map(file => - {file.name} - `${Math.floor(percent || 0)}%`} - status={file.status === "error" ? "exception" : "normal"} - /> - )} - : null}
; } diff --git a/packages/web_ui/src/components/UploadProgress.tsx b/packages/web_ui/src/components/UploadProgress.tsx new file mode 100644 index 00000000..ea438d41 --- /dev/null +++ b/packages/web_ui/src/components/UploadProgress.tsx @@ -0,0 +1,93 @@ +import { useCallback, useState } from "react"; +import { Button, List, Progress, Typography, type UploadProps } from "antd"; + +import * as lib from "@clusterio/lib"; + +import notify, { notifyErrorHandler } from "../util/notify"; + +type CustomRequest = NonNullable; + +interface Upload { + uid: string; + name: string; + loaded: number; + total: number; + abort: () => void; +} + +function errorMessage(err: any, body: any) { + const errors = [...body?.request_errors ?? [], ...body?.errors ?? []]; + if (errors.length) { + return errors.join(", "); + } + if (err instanceof Error) { + return err.message; + } + return "Connection to the controller failed"; +} + +/** + * Track uploads made by antd Upload components and show them with a cancel button. + * @returns customRequest to pass to every Upload sharing the list, and the list to render. + */ +export function useUploadProgress(): { customRequest: CustomRequest, list: React.ReactNode } { + const [uploads, setUploads] = useState([]); + + function update(uid: string, change: Partial) { + setUploads(current => current.map(upload => (upload.uid === uid ? { ...upload, ...change } : upload))); + } + function remove(uid: string) { + setUploads(current => current.filter(upload => upload.uid !== uid)); + } + + const customRequest = useCallback((options, { defaultRequest }) => { + const file = options.file as File & { uid: string }; + const uid = file.uid; + // antd types this as void, rc-upload returns the abort handle. + const request = defaultRequest({ + ...options, + onProgress(event) { + options.onProgress?.(event); + update(uid, { loaded: event.loaded ?? 0, total: event.total ?? file.size }); + }, + onSuccess(body, xhr) { + options.onSuccess?.(body, xhr); + remove(uid); + }, + onError(err, body) { + options.onError?.(err, body); + remove(uid); + notifyErrorHandler(`Error uploading ${file.name}`)(new Error(errorMessage(err, body))); + }, + }) as unknown as { abort: () => void }; + const abort = () => { + request.abort(); + // An aborted XMLHttpRequest calls neither onload nor onerror. + options.onError?.(new Error("Upload cancelled")); + remove(uid); + notify(`Cancelled upload of ${file.name}`); + }; + setUploads(current => [...current, { uid, name: file.name, loaded: 0, total: file.size, abort }]); + return { abort }; + }, []); + + const list = uploads.length ? upload.uid} + renderItem={upload => Cancel]} + > +
+ {upload.name} + `${lib.formatBytes(upload.loaded)} / ${lib.formatBytes(upload.total)}`} + /> +
+
} + /> : null; + + return { customRequest, list }; +} diff --git a/test/lib/file_ops.js b/test/lib/file_ops.js index a44e069f..efd29ced 100644 --- a/test/lib/file_ops.js +++ b/test/lib/file_ops.js @@ -412,6 +412,20 @@ describe("lib/file_ops", function() { await assert.rejects(fs.readFile(downloadPath), { code: "ENOENT" }); }); + it("should not leave a temporary file behind if interrupted while writing", async function () { + const downloadPath = path.join(downloadDir, "interrupted-writing.txt"); + onStream = (res) => { + res.write(Buffer.alloc(1024 * 1024)); + setTimeout(() => res.destroy(), 100); + }; + await assert.rejects(lib.downloadFile(new URL("/stream", baseUrl), downloadPath, "rename")); + await assert.rejects( + fs.readFile(path.join(downloadDir, "interrupted-writing.tmp.txt")), + { code: "ENOENT" }, + ); + await assert.rejects(fs.readFile(downloadPath), { code: "ENOENT" }); + }); + it("should throw if overwriteMode is rename and directory disappeared", async function () { if (process.platform === "win32") { // Can't remove the directory while the stream is running on Windows.