Feat: support dispatch remote policy uploader

This commit is contained in:
HFO4 2022-02-27 14:40:16 +08:00
parent a5de76bf64
commit 98dba63496
6 changed files with 47 additions and 7 deletions

View File

@ -7,11 +7,11 @@ import { sizeToString } from "../../utils";
import { toggleSnackbar } from "../../actions";
import {
withStyles,
LinearProgress,
Typography,
Divider,
LinearProgress,
Tooltip,
Typography,
withStyles,
} from "@material-ui/core";
import ButtonBase from "@material-ui/core/ButtonBase";
import { withRouter } from "react-router";
@ -68,6 +68,7 @@ const styles = (theme) => ({
},
});
// TODO 使用 hooks 重构
class StorageBarCompoment extends Component {
state = {
percent: 0,

View File

@ -8,7 +8,7 @@ import {
import React, { useCallback } from "react";
import { DeleteSweep } from "@material-ui/icons";
import { useDispatch } from "react-redux";
import { toggleSnackbar } from "../../../actions";
import { refreshStorage, toggleSnackbar } from "../../../actions";
import API from "../../../middleware/Api";
const useStyles = makeStyles((theme) => ({}));
@ -27,6 +27,9 @@ export default function MoreActions({ anchorEl, onClose, uploadManager }) {
dispatch(toggleSnackbar(vertical, horizontal, msg, color)),
[dispatch]
);
const RefreshStorage = useCallback(() => dispatch(refreshStorage()), [
dispatch,
]);
const actionClicked = (next) => () => {
onClose();
@ -37,7 +40,17 @@ export default function MoreActions({ anchorEl, onClose, uploadManager }) {
uploadManager.cleanupSessions();
API.delete("/file/upload")
.then((response) => {
ToggleSnackbar("top", "right", "上传会话已清除", "success");
if (response.rawData.code === 0) {
ToggleSnackbar("top", "right", "上传会话已清除", "success");
} else {
ToggleSnackbar(
"top",
"right",
response.rawData.msg,
"warning"
);
}
RefreshStorage();
})
.catch((error) => {
ToggleSnackbar("top", "right", error.message, "error");

View File

@ -5,6 +5,7 @@ import Base from "./uploader/base";
import Local from "./uploader/local";
import { Pool } from "./utils/pool";
import { cleanupResumeCtx } from "./utils";
import Remote from "./uploader/remote";
export interface Option {
logLevel: LogLevel;
@ -45,7 +46,8 @@ export default class UploadManager {
switch (task.policy.type) {
case PolicyType.local:
return new Local(task, this);
case PolicyType.remote:
return new Remote(task, this);
default:
throw new UnknownPolicyError(
"Unknown policy type.",

View File

@ -3,6 +3,9 @@ import { ChunkProgress } from "./uploader/chunk";
export enum PolicyType {
local = "local",
remote = "remote",
oss = "oss",
qiniu = "qiniu",
onedrive = "onedrive",
}
export interface Policy {

View File

@ -45,7 +45,13 @@ export interface Progress {
loaded: number;
}
const resumePolicy = [PolicyType.local];
const resumePolicy = [
PolicyType.local,
PolicyType.remote,
PolicyType.qiniu,
PolicyType.oss,
PolicyType.onedrive,
];
const deleteUploadSessionDelay = 500;
export default abstract class Base {

View File

@ -0,0 +1,15 @@
import Chunk, { ChunkInfo } from "./chunk";
import { loadUploadChunk } from "../api";
export default class Remote extends Chunk {
protected async uploadChunk(chunkInfo: ChunkInfo) {
return loadUploadChunk(
this.task.session?.sessionID!,
chunkInfo,
(p) => {
this.updateChunkProgress(p.loaded, chunkInfo.index);
},
this.cancelToken.token
);
}
}