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
|
||||
*/
|
||||
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);
|
||||
|
||||
@@ -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<lib.ModPack>({
|
||||
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}
|
||||
>
|
||||
<UploadButton />
|
||||
</Upload>;
|
||||
@@ -635,6 +638,7 @@ export default function ModsPage() {
|
||||
<SearchModsButton />
|
||||
{uploadButton}
|
||||
</Space>} />
|
||||
{uploadList}
|
||||
|
||||
<Upload.Dragger
|
||||
className="save-list-dragger"
|
||||
@@ -646,6 +650,7 @@ export default function ModsPage() {
|
||||
"X-Access-Token": control.connector.token || "",
|
||||
}}
|
||||
action={`${webRoot}api/upload-mod`}
|
||||
customRequest={customRequest}
|
||||
showUploadList={false}
|
||||
>
|
||||
<Dropzone />
|
||||
|
||||
@@ -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<File[]>([]);
|
||||
const { customRequest, list: uploadList } = useUploadProgress();
|
||||
const tableState = useTableQueryState<lib.SaveDetails>({
|
||||
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 <div>
|
||||
@@ -365,6 +327,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
</Upload>}
|
||||
{account.hasPermission("core.instance.save.create") && <CreateSaveModal instance={props.instance} />}
|
||||
</Space>} />
|
||||
{uploadList}
|
||||
{
|
||||
account.hasPermission("core.instance.save.upload")
|
||||
? <Upload.Dragger className="save-list-dragger" openFileDialogOnClick={false} {...uploadProps}>
|
||||
@@ -373,15 +336,5 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
</Upload.Dragger>
|
||||
: 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>;
|
||||
}
|
||||
@@ -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 };
|
||||
}
|
||||
Reference in new issue
Block a user