mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Merge pull request #1250 from bbassie/feature/upload-progress
Show upload progress with a cancel button
This commit is contained in:
5 files changed
+120
-55
No files matched your search
@@ -2,7 +2,6 @@
|
|||||||
* @module lib/file_ops
|
* @module lib/file_ops
|
||||||
*/
|
*/
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
import events from "events";
|
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import stream from "stream";
|
import stream from "stream";
|
||||||
@@ -258,7 +257,8 @@ export async function createTempWriteStream(filePath: string) {
|
|||||||
async function discardPartialDownload(writeStream: stream.Writable, tempFilePath: string) {
|
async function discardPartialDownload(writeStream: stream.Writable, tempFilePath: string) {
|
||||||
if (!writeStream.closed) {
|
if (!writeStream.closed) {
|
||||||
writeStream.destroy();
|
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 {
|
try {
|
||||||
await fs.unlink(tempFilePath);
|
await fs.unlink(tempFilePath);
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import Link from "./Link";
|
|||||||
import ModDetails from "./ModDetails";
|
import ModDetails from "./ModDetails";
|
||||||
import { Dropzone } from "./Dropzone";
|
import { Dropzone } from "./Dropzone";
|
||||||
import UploadButton from "./UploadButton";
|
import UploadButton from "./UploadButton";
|
||||||
|
import { useUploadProgress } from "./UploadProgress";
|
||||||
import { InputPartialVersion } from "./InputVersion";
|
import { InputPartialVersion } from "./InputVersion";
|
||||||
|
|
||||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||||
@@ -524,6 +525,7 @@ export default function ModsPage() {
|
|||||||
let account = useAccount();
|
let account = useAccount();
|
||||||
let control = useContext(ControlContext);
|
let control = useContext(ControlContext);
|
||||||
let [mods] = useMods();
|
let [mods] = useMods();
|
||||||
|
const { customRequest, list: uploadList } = useUploadProgress();
|
||||||
let [modPacks] = useModPacks();
|
let [modPacks] = useModPacks();
|
||||||
const modPackTable = useTableQueryState<lib.ModPack>({
|
const modPackTable = useTableQueryState<lib.ModPack>({
|
||||||
namespace: "modPack", defaultSortKey: "name",
|
namespace: "modPack", defaultSortKey: "name",
|
||||||
@@ -582,6 +584,7 @@ export default function ModsPage() {
|
|||||||
"X-Access-Token": control.connector.token || "",
|
"X-Access-Token": control.connector.token || "",
|
||||||
}}
|
}}
|
||||||
action={`${webRoot}api/upload-mod`}
|
action={`${webRoot}api/upload-mod`}
|
||||||
|
customRequest={customRequest}
|
||||||
>
|
>
|
||||||
<UploadButton />
|
<UploadButton />
|
||||||
</Upload>;
|
</Upload>;
|
||||||
@@ -635,6 +638,7 @@ export default function ModsPage() {
|
|||||||
<SearchModsButton />
|
<SearchModsButton />
|
||||||
{uploadButton}
|
{uploadButton}
|
||||||
</Space>} />
|
</Space>} />
|
||||||
|
{uploadList}
|
||||||
|
|
||||||
<Upload.Dragger
|
<Upload.Dragger
|
||||||
className="save-list-dragger"
|
className="save-list-dragger"
|
||||||
@@ -646,6 +650,7 @@ export default function ModsPage() {
|
|||||||
"X-Access-Token": control.connector.token || "",
|
"X-Access-Token": control.connector.token || "",
|
||||||
}}
|
}}
|
||||||
action={`${webRoot}api/upload-mod`}
|
action={`${webRoot}api/upload-mod`}
|
||||||
|
customRequest={customRequest}
|
||||||
showUploadList={false}
|
showUploadList={false}
|
||||||
>
|
>
|
||||||
<Dropzone />
|
<Dropzone />
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useContext, useState } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import {
|
import {
|
||||||
message, Button, Checkbox, Form, Input, List, Modal,
|
message, Button, Checkbox, Form, Input, Modal,
|
||||||
Popconfirm, Progress, Select, Space, Table, Tooltip, Upload,
|
Popconfirm, Select, Space, Table, Tooltip, Upload,
|
||||||
} from "antd";
|
} from "antd";
|
||||||
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
||||||
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
||||||
@@ -19,6 +19,7 @@ import { useSavesOfInstance } from "../model/saves";
|
|||||||
import { notifyErrorHandler } from "../util/notify";
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
import { Dropzone } from "./Dropzone";
|
import { Dropzone } from "./Dropzone";
|
||||||
import UploadButton from "./UploadButton";
|
import UploadButton from "./UploadButton";
|
||||||
|
import { useUploadProgress } from "./UploadProgress";
|
||||||
|
|
||||||
|
|
||||||
type ModalProps = {
|
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 }) {
|
export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||||
let account = useAccount();
|
let account = useAccount();
|
||||||
let control = useContext(ControlContext);
|
let control = useContext(ControlContext);
|
||||||
let [saves] = useSavesOfInstance(props.instance.id);
|
let [saves] = useSavesOfInstance(props.instance.id);
|
||||||
let [starting, setStarting] = useState(false);
|
let [starting, setStarting] = useState(false);
|
||||||
let [uploadingFiles, setUploadingFiles] = useState<File[]>([]);
|
const { customRequest, list: uploadList } = useUploadProgress();
|
||||||
const tableState = useTableQueryState<lib.SaveDetails>({
|
const tableState = useTableQueryState<lib.SaveDetails>({
|
||||||
namespace: "save", defaultSortKey: "mtimeMs", defaultSortOrder: "descend", pagination: { defaultPageSize: 10 },
|
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 = {
|
let uploadProps = {
|
||||||
disabled: hostOffline,
|
disabled: hostOffline,
|
||||||
name: "file",
|
name: "file",
|
||||||
@@ -353,7 +315,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
|||||||
},
|
},
|
||||||
showUploadList: false,
|
showUploadList: false,
|
||||||
action: `${webRoot}api/upload-save`,
|
action: `${webRoot}api/upload-save`,
|
||||||
onChange,
|
customRequest,
|
||||||
};
|
};
|
||||||
|
|
||||||
return <div>
|
return <div>
|
||||||
@@ -365,6 +327,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
|||||||
</Upload>}
|
</Upload>}
|
||||||
{account.hasPermission("core.instance.save.create") && <CreateSaveModal instance={props.instance} />}
|
{account.hasPermission("core.instance.save.create") && <CreateSaveModal instance={props.instance} />}
|
||||||
</Space>} />
|
</Space>} />
|
||||||
|
{uploadList}
|
||||||
{
|
{
|
||||||
account.hasPermission("core.instance.save.upload")
|
account.hasPermission("core.instance.save.upload")
|
||||||
? <Upload.Dragger className="save-list-dragger" openFileDialogOnClick={false} {...uploadProps}>
|
? <Upload.Dragger className="save-list-dragger" openFileDialogOnClick={false} {...uploadProps}>
|
||||||
@@ -373,15 +336,5 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
|||||||
</Upload.Dragger>
|
</Upload.Dragger>
|
||||||
: saveTable
|
: saveTable
|
||||||
}
|
}
|
||||||
{uploadingFiles.length ? <List>
|
|
||||||
{uploadingFiles.map(file => <List.Item key={file.uid}>
|
|
||||||
{file.name}
|
|
||||||
<Progress
|
|
||||||
percent={file.percent}
|
|
||||||
format={percent => `${Math.floor(percent || 0)}%`}
|
|
||||||
status={file.status === "error" ? "exception" : "normal"}
|
|
||||||
/>
|
|
||||||
</List.Item>)}
|
|
||||||
</List> : null}
|
|
||||||
</div>;
|
</div>;
|
||||||
}
|
}
|
||||||
@@ -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<UploadProps["customRequest"]>;
|
||||||
|
|
||||||
|
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<Upload[]>([]);
|
||||||
|
|
||||||
|
function update(uid: string, change: Partial<Upload>) {
|
||||||
|
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<CustomRequest>((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 ? <List
|
||||||
|
size="small"
|
||||||
|
className="upload-progress-list"
|
||||||
|
dataSource={uploads}
|
||||||
|
rowKey={upload => upload.uid}
|
||||||
|
renderItem={upload => <List.Item
|
||||||
|
actions={[<Button key="cancel" size="small" onClick={upload.abort}>Cancel</Button>]}
|
||||||
|
>
|
||||||
|
<div style={{ flex: 1, minWidth: 0 }}>
|
||||||
|
<Typography.Text ellipsis>{upload.name}</Typography.Text>
|
||||||
|
<Progress
|
||||||
|
percent={upload.total ? upload.loaded / upload.total * 100 : 0}
|
||||||
|
format={() => `${lib.formatBytes(upload.loaded)} / ${lib.formatBytes(upload.total)}`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</List.Item>}
|
||||||
|
/> : null;
|
||||||
|
|
||||||
|
return { customRequest, list };
|
||||||
|
}
|
||||||
@@ -412,6 +412,20 @@ describe("lib/file_ops", function() {
|
|||||||
await assert.rejects(fs.readFile(downloadPath), { code: "ENOENT" });
|
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 () {
|
it("should throw if overwriteMode is rename and directory disappeared", async function () {
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
// Can't remove the directory while the stream is running on Windows.
|
// Can't remove the directory while the stream is running on Windows.
|
||||||
|
|||||||
Reference in new issue
Block a user