Skip to content

Commit

Permalink
refactor(project): support new convert backend api (#1548)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious authored Jun 2, 2022
1 parent bf5f7c6 commit 318f54e
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 76 deletions.
66 changes: 49 additions & 17 deletions desktop/renderer-app/src/api-middleware/courseware-converting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import { Region } from "flat-components";
export interface ConvertingTaskStatus {
uuid: string;
type: "static" | "dynamic";
status: "Waiting" | "Converting" | "Finished" | "Fail";
failedReason: string;
status: "Waiting" | "Converting" | "Finished" | "Fail" | "Abort";
errorCode?: string;
errorMessage?: string;
convertedPercentage?: number;
prefix?: string;
// TODO: `progress` is for static resources and will be changed in the future.
progress?: ConvertingTaskStatusLegacy["progress"];
}

export interface ConvertingTaskStatusLegacy {
uuid: string;
type: "static" | "dynamic";
status: "Waiting" | "Converting" | "Finished" | "Fail" | "Abort";
failedReason?: string;
progress?: {
totalPageSize: number;
convertedPageSize: number;
Expand All @@ -20,22 +32,42 @@ export interface ConvertingTaskStatus {
};
}

export async function queryConvertingTaskStatus({
taskUUID,
taskToken,
dynamic,
region,
}: {
export interface QueryConvertingParams {
taskUUID: string;
taskToken: string;
dynamic: boolean;
region: Region;
}): Promise<ConvertingTaskStatus> {
const { data } = await Axios.get<ConvertingTaskStatus>(
`https://api.netless.link/v5/services/conversion/tasks/${taskUUID}?type=${
dynamic ? "dynamic" : "static"
}`,
{ headers: { token: taskToken, region } },
);
return data;
dynamic: boolean;
projector: boolean;
}

export async function queryConvertingTaskStatus(
params: QueryConvertingParams,
): Promise<ConvertingTaskStatus> {
const { taskUUID, taskToken, dynamic, region, projector } = params;
if (projector) {
const { data } = await Axios.get<ConvertingTaskStatus>(
`https://api.netless.link/v5/projector/tasks/${taskUUID}`,
{ headers: { token: taskToken, region } },
);
return data;
} else {
const { data } = await Axios.get<ConvertingTaskStatusLegacy>(
`https://api.netless.link/v5/services/conversion/tasks/${taskUUID}?type=${
dynamic ? "dynamic" : "static"
}`,
{ headers: { token: taskToken, region } },
);
const prefix = data.progress?.convertedFileList?.[0]?.conversionFileUrl || "";
const index = prefix.lastIndexOf("/staticConvert") + 1;
const transformed: ConvertingTaskStatus = {
uuid: data.uuid,
type: data.type,
status: data.status,
errorMessage: data.failedReason,
convertedPercentage: data.progress?.convertedPercentage,
prefix: prefix ? prefix.slice(0, index) : undefined,
progress: data.progress,
};
return transformed;
}
}
15 changes: 15 additions & 0 deletions desktop/renderer-app/src/api-middleware/flatServer/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ interface ListFilesResponse {
files: Array<Omit<CloudFile, "createAt"> & { createAt: number }>;
}

enum FileResourceType {
WhiteboardConvert = "WhiteboardConvert",
LocalCourseware = "LocalCourseware",
OnlineCourseware = "OnlineCourseware",
NormalResources = "NormalResources",
WhiteboardProjector = "WhiteboardProjector",
}

export type ResourceType = `${FileResourceType}`;

export interface CloudFile {
fileUUID: string;
fileName: string;
Expand All @@ -25,6 +35,7 @@ export interface CloudFile {
region: Region;
/** online courseware */
external: boolean;
resourceType: ResourceType;
}

export interface ListFilesResult {
Expand Down Expand Up @@ -63,6 +74,8 @@ export async function uploadStart(payload: UploadStartPayload): Promise<UploadSt
}
interface UploadFinishPayload {
fileUUID: string;
/** Use the new backend "projector" to convert file. */
isWhiteboardProjector?: boolean;
}

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

export interface ConvertStartPayload {
fileUUID: string;
/** Use the new backend "projector" to convert file. */
isWhiteboardProjector?: boolean;
}

export interface ConvertStartResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export interface DynamicPreviewProps {
taskUUID: string;
taskToken: string;
region: Region;
projector: boolean;
windowInstance?: Window | PortalWindow;
}

export const DynamicPreview = observer<DynamicPreviewProps>(function PPTPreview({
taskUUID,
taskToken,
region,
projector,
windowInstance,
}) {
const previewer = useRef<SlidePreviewer | null>(null);
Expand All @@ -32,16 +34,19 @@ export const DynamicPreview = observer<DynamicPreviewProps>(function PPTPreview(
taskToken,
dynamic: true,
region,
projector,
}),
);

if (DynamicPreviewRef.current) {
const slidePreviewer = previewSlide({
container: DynamicPreviewRef.current,
taskId: convertState.uuid,
url: extractSlideUrlPrefix(
convertState.progress?.convertedFileList[0].conversionFileUrl,
),
url:
convertState.prefix + "dynamicConvert" ||
extractSlideUrlPrefix(
convertState.progress?.convertedFileList[0].conversionFileUrl,
),
});

if (windowInstance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const StaticPreview = observer<StaticPreviewProps>(function DocumentPrevi
taskToken,
dynamic: false,
region,
projector: false,
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type FileInfo = {
taskToken: string;
region: Region;
fileName: string;
projector: boolean;
};

export interface ResourcePreviewProps {
Expand All @@ -42,6 +43,7 @@ export const ResourcePreview = observer<ResourcePreviewProps>(function PPTPrevie
}
return (
<DynamicPreview
projector={fileInfo.projector}
region={region}
taskToken={taskToken}
taskUUID={taskUUID}
Expand Down
31 changes: 18 additions & 13 deletions desktop/renderer-app/src/pages/CloudStoragePage/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { queryH5ConvertingStatus } from "../../api-middleware/h5-converting";
import { Scheduler } from "./scheduler";

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

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

Expand Down Expand Up @@ -323,6 +323,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
file.taskToken = cloudFile.taskToken;
file.taskUUID = cloudFile.taskUUID;
file.external = cloudFile.external;
file.resourceType = cloudFile.resourceType;
} else {
this.filesMap.set(
cloudFile.fileUUID,
Expand Down Expand Up @@ -398,6 +399,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
file.taskToken = cloudFile.taskToken;
file.taskUUID = cloudFile.taskUUID;
file.external = cloudFile.external;
file.resourceType = cloudFile.resourceType;
} else {
this.filesMap.set(
cloudFile.fileUUID,
Expand Down Expand Up @@ -430,6 +432,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
taskToken: file.taskToken,
region: file.region,
fileName: file.fileName,
projector: file.resourceType === "WhiteboardProjector",
};

switch (file.convert) {
Expand Down Expand Up @@ -580,6 +583,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
}
const { taskToken, taskUUID } = await convertStart({
fileUUID: file.fileUUID,
isWhiteboardProjector: isPPTX(file.fileName),
});
runInAction(() => {
file.convert = "converting";
Expand Down Expand Up @@ -650,22 +654,25 @@ export class CloudStorageStore extends CloudStorageStoreBase {
return true;
}

let status: ConvertingTaskStatus["status"];
let progress: ConvertingTaskStatus["progress"];
const dynamic = isPPTX(file.fileName);
let status: ConvertingTaskStatus;

try {
({ status, progress } = await queryConvertingTaskStatus({
status = await queryConvertingTaskStatus({
taskToken: file.taskToken,
taskUUID: file.taskUUID,
dynamic: isPPTX(file.fileName),
dynamic,
region: file.region,
}));
projector: file.resourceType === "WhiteboardProjector",
});
} catch (e) {
console.error(e);
return false;
}

if (status === "Fail" || status === "Finished") {
const statusText = status.status;

if (statusText === "Fail" || statusText === "Finished") {
if (process.env.NODE_ENV === "development") {
console.log("[cloud storage]: convert finish", file.fileName);
}
Expand All @@ -678,15 +685,13 @@ export class CloudStorageStore extends CloudStorageStoreBase {
}

runInAction(() => {
file.convert = status === "Fail" ? "error" : "success";
file.convert = statusText === "Fail" ? "error" : "success";
});

if (status === "Finished") {
const src = progress?.convertedFileList?.[0].conversionFileUrl;
if (statusText === "Finished" && status.prefix) {
const src = status.prefix + "dynamicConvert/" + status.uuid + dynamic + "/1.png";
if (src) {
void getCoursewarePreloader()
.preload(src)
.catch(error => console.warn(error));
void getCoursewarePreloader().preload(src).catch(console.warn);
}
}

Expand Down
13 changes: 11 additions & 2 deletions desktop/renderer-app/src/stores/whiteboard-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,13 @@ export class WhiteboardStore {
return;
}

const { taskUUID, taskToken, region } = file;
const { taskUUID, taskToken, region, resourceType } = file;
const convertingStatus = await queryConvertingTaskStatus({
taskUUID,
taskToken,
dynamic: isPPTX(file.fileName),
region,
projector: resourceType === "WhiteboardProjector",
});

if (file.convert !== "success") {
Expand All @@ -548,7 +549,7 @@ export class WhiteboardStore {
if (convertingStatus.status === "Fail") {
void message.error(
this.i18n.t("transcoding-failure-reason", {
reason: convertingStatus.failedReason,
reason: convertingStatus.errorMessage,
}),
);
}
Expand All @@ -572,6 +573,14 @@ export class WhiteboardStore {
const uuid = v4uuid();
const scenesPath = `/${taskUUID}/${uuid}`;
await this.openDocsFileInWindowManager(scenesPath, file.fileName, scenes);
} else if (convertingStatus.status === "Finished" && convertingStatus.prefix) {
await this.fastboardAPP?.insertDocs({
fileType: "pptx",
title: file.fileName,
scenePath: `/${taskUUID}/${v4uuid()}`,
taskId: taskUUID,
url: convertingStatus.prefix + "dynamicConvert",
});
} else {
void message.error(this.i18n.t("unable-to-insert-courseware"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CLOUD_STORAGE_OSS_ALIBABA_CONFIG } from "../../constants/process";
import { ServerRequestError } from "../error/server-request-error";
import { RequestErrorCode } from "../../constants/error-code";
import { configStore } from "../../stores/config-store";
import { isPPTX } from "../file";

export enum UploadStatusType {
Pending = 1,
Expand Down Expand Up @@ -144,6 +145,7 @@ export class UploadTask {
try {
await uploadFinish({
fileUUID: this.fileUUID,
isWhiteboardProjector: isPPTX(this.file.name),
});
} catch (e) {
console.error(e);
Expand Down
Loading

0 comments on commit 318f54e

Please sign in to comment.