fix(ui/browser): move files in batch

This commit is contained in:
hexxa 2021-08-27 18:11:45 +08:00 committed by Hexxa
parent 85be9fe67d
commit 1d727b4416
3 changed files with 40 additions and 15 deletions

View file

@ -136,11 +136,9 @@ export class Updater {
}) })
.map((selectedItem: MetadataResp): string => { .map((selectedItem: MetadataResp): string => {
return getItemPath(dirParts.join("/"), selectedItem.name); return getItemPath(dirParts.join("/"), selectedItem.name);
// const resp = await this.filesClient.delete(itemPath);
// return resp.status === 200 ? "" : selectedItem.name;
}); });
const batchSize = 5; const batchSize = 3;
let batch = List<string>(); let batch = List<string>();
let fails = List<string>(); let fails = List<string>();
@ -164,7 +162,9 @@ export class Updater {
} }
if (fails.size > 0) { if (fails.size > 0) {
alertMsg(`failed to delete ${fails.join(",\n")}`); alertMsg(
`${this.props.msg.pkg.get("delete.fail")}: ${fails.join(",\n")}`
);
} }
return this.setItems(dirParts); return this.setItems(dirParts);
@ -196,22 +196,45 @@ export class Updater {
dstDir: string, dstDir: string,
selectedItems: Map<string, boolean> selectedItems: Map<string, boolean>
): Promise<void> => { ): Promise<void> => {
const moveRequests = List<string>(selectedItems.keys()).map( const itemsToMove = List<string>(selectedItems.keys()).map(
async (itemName: string): Promise<string> => { (itemName: string): any => {
const oldPath = getItemPath(srcDir, itemName); const from = getItemPath(srcDir, itemName);
const newPath = getItemPath(dstDir, itemName); const to = getItemPath(dstDir, itemName);
const resp = await this.filesClient.move(oldPath, newPath); return { from, to };
return resp.status === 200 ? "" : itemName; // const resp = await this.filesClient.move(oldPath, newPath);
// return resp.status === 200 ? "" : itemName;
} }
); );
const failedFiles = await Promise.all(moveRequests); const batchSize = 3;
failedFiles.forEach((failedItem) => { let batch = List<any>();
if (failedItem !== "") { let fails = List<string>();
alertMsg(`failed to move ${failedItem}`);
for (let i = 0; i < itemsToMove.size; i++) {
batch = batch.push(itemsToMove.get(i));
if (batch.size >= batchSize || i == itemsToMove.size - 1) {
let promises = batch.map(async (fromTo: any): Promise<Response<any>> => {
return this.filesClient.move(fromTo.from, fromTo.to);
});
const resps = await Promise.all(promises.toSeq());
resps.forEach((resp: Response<any>, i: number) => {
if (resp.status !== 200) {
fails = fails.push(batch.get(i).from);
} }
}); });
batch = batch.clear();
}
}
if (fails.size > 0) {
alertMsg(
`${this.props.msg.pkg.get("move.fail")}: ${fails.join(",\n")}`
);
}
return this.setItems(List<string>(dstDir.split("/"))); return this.setItems(List<string>(dstDir.split("/")));
}; };

View file

@ -65,4 +65,5 @@ export const msgs: Map<string, string> = Map({
"admin.roles": "Roles", "admin.roles": "Roles",
zhCN: "Chinese (simplified)", zhCN: "Chinese (simplified)",
enUS: "English (USA)", enUS: "English (USA)",
"move.fail": "Failed to move",
}); });

View file

@ -64,4 +64,5 @@ export const msgs: Map<string, string> = Map({
"admin.roles": "角色列表", "admin.roles": "角色列表",
"zhCN": "中文简体", "zhCN": "中文简体",
"enUS": "英语美国", "enUS": "英语美国",
"move.fail": "移动失败",
}); });