Skip to content

Commit 318f54e

Browse files
authored
refactor(project): support new convert backend api (#1548)
1 parent bf5f7c6 commit 318f54e

File tree

17 files changed

+218
-76
lines changed

17 files changed

+218
-76
lines changed

desktop/renderer-app/src/api-middleware/courseware-converting.ts

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,20 @@ import { Region } from "flat-components";
44
export interface ConvertingTaskStatus {
55
uuid: string;
66
type: "static" | "dynamic";
7-
status: "Waiting" | "Converting" | "Finished" | "Fail";
8-
failedReason: string;
7+
status: "Waiting" | "Converting" | "Finished" | "Fail" | "Abort";
8+
errorCode?: string;
9+
errorMessage?: string;
10+
convertedPercentage?: number;
11+
prefix?: string;
12+
// TODO: `progress` is for static resources and will be changed in the future.
13+
progress?: ConvertingTaskStatusLegacy["progress"];
14+
}
15+
16+
export interface ConvertingTaskStatusLegacy {
17+
uuid: string;
18+
type: "static" | "dynamic";
19+
status: "Waiting" | "Converting" | "Finished" | "Fail" | "Abort";
20+
failedReason?: string;
921
progress?: {
1022
totalPageSize: number;
1123
convertedPageSize: number;
@@ -20,22 +32,42 @@ export interface ConvertingTaskStatus {
2032
};
2133
}
2234

23-
export async function queryConvertingTaskStatus({
24-
taskUUID,
25-
taskToken,
26-
dynamic,
27-
region,
28-
}: {
35+
export interface QueryConvertingParams {
2936
taskUUID: string;
3037
taskToken: string;
31-
dynamic: boolean;
3238
region: Region;
33-
}): Promise<ConvertingTaskStatus> {
34-
const { data } = await Axios.get<ConvertingTaskStatus>(
35-
`https://api.netless.link/v5/services/conversion/tasks/${taskUUID}?type=${
36-
dynamic ? "dynamic" : "static"
37-
}`,
38-
{ headers: { token: taskToken, region } },
39-
);
40-
return data;
39+
dynamic: boolean;
40+
projector: boolean;
41+
}
42+
43+
export async function queryConvertingTaskStatus(
44+
params: QueryConvertingParams,
45+
): Promise<ConvertingTaskStatus> {
46+
const { taskUUID, taskToken, dynamic, region, projector } = params;
47+
if (projector) {
48+
const { data } = await Axios.get<ConvertingTaskStatus>(
49+
`https://api.netless.link/v5/projector/tasks/${taskUUID}`,
50+
{ headers: { token: taskToken, region } },
51+
);
52+
return data;
53+
} else {
54+
const { data } = await Axios.get<ConvertingTaskStatusLegacy>(
55+
`https://api.netless.link/v5/services/conversion/tasks/${taskUUID}?type=${
56+
dynamic ? "dynamic" : "static"
57+
}`,
58+
{ headers: { token: taskToken, region } },
59+
);
60+
const prefix = data.progress?.convertedFileList?.[0]?.conversionFileUrl || "";
61+
const index = prefix.lastIndexOf("/staticConvert") + 1;
62+
const transformed: ConvertingTaskStatus = {
63+
uuid: data.uuid,
64+
type: data.type,
65+
status: data.status,
66+
errorMessage: data.failedReason,
67+
convertedPercentage: data.progress?.convertedPercentage,
68+
prefix: prefix ? prefix.slice(0, index) : undefined,
69+
progress: data.progress,
70+
};
71+
return transformed;
72+
}
4173
}

desktop/renderer-app/src/api-middleware/flatServer/storage.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ interface ListFilesResponse {
1111
files: Array<Omit<CloudFile, "createAt"> & { createAt: number }>;
1212
}
1313

14+
enum FileResourceType {
15+
WhiteboardConvert = "WhiteboardConvert",
16+
LocalCourseware = "LocalCourseware",
17+
OnlineCourseware = "OnlineCourseware",
18+
NormalResources = "NormalResources",
19+
WhiteboardProjector = "WhiteboardProjector",
20+
}
21+
22+
export type ResourceType = `${FileResourceType}`;
23+
1424
export interface CloudFile {
1525
fileUUID: string;
1626
fileName: string;
@@ -25,6 +35,7 @@ export interface CloudFile {
2535
region: Region;
2636
/** online courseware */
2737
external: boolean;
38+
resourceType: ResourceType;
2839
}
2940

3041
export interface ListFilesResult {
@@ -63,6 +74,8 @@ export async function uploadStart(payload: UploadStartPayload): Promise<UploadSt
6374
}
6475
interface UploadFinishPayload {
6576
fileUUID: string;
77+
/** Use the new backend "projector" to convert file. */
78+
isWhiteboardProjector?: boolean;
6679
}
6780

6881
export async function uploadFinish(payload: UploadFinishPayload): Promise<void> {
@@ -90,6 +103,8 @@ export async function removeFiles(payload: RemoveFilesPayload): Promise<void> {
90103

91104
export interface ConvertStartPayload {
92105
fileUUID: string;
106+
/** Use the new backend "projector" to convert file. */
107+
isWhiteboardProjector?: boolean;
93108
}
94109

95110
export interface ConvertStartResult {

desktop/renderer-app/src/pages/CloudStoragePage/CloudStorageFilePreview/DynamicPreview.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ export interface DynamicPreviewProps {
1111
taskUUID: string;
1212
taskToken: string;
1313
region: Region;
14+
projector: boolean;
1415
windowInstance?: Window | PortalWindow;
1516
}
1617

1718
export const DynamicPreview = observer<DynamicPreviewProps>(function PPTPreview({
1819
taskUUID,
1920
taskToken,
2021
region,
22+
projector,
2123
windowInstance,
2224
}) {
2325
const previewer = useRef<SlidePreviewer | null>(null);
@@ -32,16 +34,19 @@ export const DynamicPreview = observer<DynamicPreviewProps>(function PPTPreview(
3234
taskToken,
3335
dynamic: true,
3436
region,
37+
projector,
3538
}),
3639
);
3740

3841
if (DynamicPreviewRef.current) {
3942
const slidePreviewer = previewSlide({
4043
container: DynamicPreviewRef.current,
4144
taskId: convertState.uuid,
42-
url: extractSlideUrlPrefix(
43-
convertState.progress?.convertedFileList[0].conversionFileUrl,
44-
),
45+
url:
46+
convertState.prefix + "dynamicConvert" ||
47+
extractSlideUrlPrefix(
48+
convertState.progress?.convertedFileList[0].conversionFileUrl,
49+
),
4550
});
4651

4752
if (windowInstance) {

desktop/renderer-app/src/pages/CloudStoragePage/CloudStorageFilePreview/StaticPreview.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const StaticPreview = observer<StaticPreviewProps>(function DocumentPrevi
3737
taskToken,
3838
dynamic: false,
3939
region,
40+
projector: false,
4041
}),
4142
);
4243

desktop/renderer-app/src/pages/CloudStoragePage/CloudStorageFilePreview/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type FileInfo = {
1717
taskToken: string;
1818
region: Region;
1919
fileName: string;
20+
projector: boolean;
2021
};
2122

2223
export interface ResourcePreviewProps {
@@ -42,6 +43,7 @@ export const ResourcePreview = observer<ResourcePreviewProps>(function PPTPrevie
4243
}
4344
return (
4445
<DynamicPreview
46+
projector={fileInfo.projector}
4547
region={region}
4648
taskToken={taskToken}
4749
taskUUID={taskUUID}

desktop/renderer-app/src/pages/CloudStoragePage/store.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { queryH5ConvertingStatus } from "../../api-middleware/h5-converting";
3939
import { Scheduler } from "./scheduler";
4040

4141
export type CloudStorageFile = CloudStorageFileUI &
42-
Pick<CloudFile, "fileURL" | "taskUUID" | "taskToken" | "region" | "external">;
42+
Pick<CloudFile, "fileURL" | "taskUUID" | "taskToken" | "region" | "external" | "resourceType">;
4343

4444
export type FileMenusKey = "open" | "download" | "rename" | "delete";
4545

@@ -323,6 +323,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
323323
file.taskToken = cloudFile.taskToken;
324324
file.taskUUID = cloudFile.taskUUID;
325325
file.external = cloudFile.external;
326+
file.resourceType = cloudFile.resourceType;
326327
} else {
327328
this.filesMap.set(
328329
cloudFile.fileUUID,
@@ -398,6 +399,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
398399
file.taskToken = cloudFile.taskToken;
399400
file.taskUUID = cloudFile.taskUUID;
400401
file.external = cloudFile.external;
402+
file.resourceType = cloudFile.resourceType;
401403
} else {
402404
this.filesMap.set(
403405
cloudFile.fileUUID,
@@ -430,6 +432,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
430432
taskToken: file.taskToken,
431433
region: file.region,
432434
fileName: file.fileName,
435+
projector: file.resourceType === "WhiteboardProjector",
433436
};
434437

435438
switch (file.convert) {
@@ -580,6 +583,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
580583
}
581584
const { taskToken, taskUUID } = await convertStart({
582585
fileUUID: file.fileUUID,
586+
isWhiteboardProjector: isPPTX(file.fileName),
583587
});
584588
runInAction(() => {
585589
file.convert = "converting";
@@ -650,22 +654,25 @@ export class CloudStorageStore extends CloudStorageStoreBase {
650654
return true;
651655
}
652656

653-
let status: ConvertingTaskStatus["status"];
654-
let progress: ConvertingTaskStatus["progress"];
657+
const dynamic = isPPTX(file.fileName);
658+
let status: ConvertingTaskStatus;
655659

656660
try {
657-
({ status, progress } = await queryConvertingTaskStatus({
661+
status = await queryConvertingTaskStatus({
658662
taskToken: file.taskToken,
659663
taskUUID: file.taskUUID,
660-
dynamic: isPPTX(file.fileName),
664+
dynamic,
661665
region: file.region,
662-
}));
666+
projector: file.resourceType === "WhiteboardProjector",
667+
});
663668
} catch (e) {
664669
console.error(e);
665670
return false;
666671
}
667672

668-
if (status === "Fail" || status === "Finished") {
673+
const statusText = status.status;
674+
675+
if (statusText === "Fail" || statusText === "Finished") {
669676
if (process.env.NODE_ENV === "development") {
670677
console.log("[cloud storage]: convert finish", file.fileName);
671678
}
@@ -678,15 +685,13 @@ export class CloudStorageStore extends CloudStorageStoreBase {
678685
}
679686

680687
runInAction(() => {
681-
file.convert = status === "Fail" ? "error" : "success";
688+
file.convert = statusText === "Fail" ? "error" : "success";
682689
});
683690

684-
if (status === "Finished") {
685-
const src = progress?.convertedFileList?.[0].conversionFileUrl;
691+
if (statusText === "Finished" && status.prefix) {
692+
const src = status.prefix + "dynamicConvert/" + status.uuid + dynamic + "/1.png";
686693
if (src) {
687-
void getCoursewarePreloader()
688-
.preload(src)
689-
.catch(error => console.warn(error));
694+
void getCoursewarePreloader().preload(src).catch(console.warn);
690695
}
691696
}
692697

desktop/renderer-app/src/stores/whiteboard-store.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,13 @@ export class WhiteboardStore {
521521
return;
522522
}
523523

524-
const { taskUUID, taskToken, region } = file;
524+
const { taskUUID, taskToken, region, resourceType } = file;
525525
const convertingStatus = await queryConvertingTaskStatus({
526526
taskUUID,
527527
taskToken,
528528
dynamic: isPPTX(file.fileName),
529529
region,
530+
projector: resourceType === "WhiteboardProjector",
530531
});
531532

532533
if (file.convert !== "success") {
@@ -548,7 +549,7 @@ export class WhiteboardStore {
548549
if (convertingStatus.status === "Fail") {
549550
void message.error(
550551
this.i18n.t("transcoding-failure-reason", {
551-
reason: convertingStatus.failedReason,
552+
reason: convertingStatus.errorMessage,
552553
}),
553554
);
554555
}
@@ -572,6 +573,14 @@ export class WhiteboardStore {
572573
const uuid = v4uuid();
573574
const scenesPath = `/${taskUUID}/${uuid}`;
574575
await this.openDocsFileInWindowManager(scenesPath, file.fileName, scenes);
576+
} else if (convertingStatus.status === "Finished" && convertingStatus.prefix) {
577+
await this.fastboardAPP?.insertDocs({
578+
fileType: "pptx",
579+
title: file.fileName,
580+
scenePath: `/${taskUUID}/${v4uuid()}`,
581+
taskId: taskUUID,
582+
url: convertingStatus.prefix + "dynamicConvert",
583+
});
575584
} else {
576585
void message.error(this.i18n.t("unable-to-insert-courseware"));
577586
}

desktop/renderer-app/src/utils/upload-task-manager/upload-task.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CLOUD_STORAGE_OSS_ALIBABA_CONFIG } from "../../constants/process";
1111
import { ServerRequestError } from "../error/server-request-error";
1212
import { RequestErrorCode } from "../../constants/error-code";
1313
import { configStore } from "../../stores/config-store";
14+
import { isPPTX } from "../file";
1415

1516
export enum UploadStatusType {
1617
Pending = 1,
@@ -144,6 +145,7 @@ export class UploadTask {
144145
try {
145146
await uploadFinish({
146147
fileUUID: this.fileUUID,
148+
isWhiteboardProjector: isPPTX(this.file.name),
147149
});
148150
} catch (e) {
149151
console.error(e);

0 commit comments

Comments
 (0)