From 1b48fd220b151fad69903d1ec6f08a401a4a652c Mon Sep 17 00:00:00 2001 From: Buddha Wang Date: Mon, 8 Mar 2021 19:32:32 +0800 Subject: [PATCH] Check & update fields.json when project loaded. (#885) --- src/common/constants.ts | 2 ++ src/redux/actions/projectActions.ts | 1 + src/services/projectService.ts | 40 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/common/constants.ts b/src/common/constants.ts index 71f8db6d7..4d91c1987 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -11,6 +11,7 @@ const appVersion = appVersionArr.join("."); const enableAPIVersionSelection = appInfo.enableAPIVersionSelection; const enablePredictionResultUpload = appInfo.enablePredictionResultUpload; const apiVersion = "v2.1-preview.3"; +const supportedFieldsSchemas = new Set(["http://www.azure.com/schema/formrecognizer/fields.json", "https://schema.cognitiveservices.azure.com/formrecognizer/2021-03-01/fields.json"]); const supportedLabelsSchemas = new Set(["http://www.azure.com/schema/formrecognizer/labels.json", "https://schema.cognitiveservices.azure.com/formrecognizer/2021-03-01/labels.json"]); /** @@ -48,6 +49,7 @@ export const constants = { showOriginLabelsByDefault: true, fieldsSchema: "https://schema.cognitiveservices.azure.com/formrecognizer/2021-03-01/fields.json", labelsSchema: "https://schema.cognitiveservices.azure.com/formrecognizer/2021-03-01/labels.json", + supportedFieldsSchemas, supportedLabelsSchemas, enableMultiPageField: false, pdfjsWorkerSrc(version: string) { diff --git a/src/redux/actions/projectActions.ts b/src/redux/actions/projectActions.ts index 4c573884d..931af06c6 100644 --- a/src/redux/actions/projectActions.ts +++ b/src/redux/actions/projectActions.ts @@ -90,6 +90,7 @@ export function loadProject(project: IProject, sharedToken?: ISecurityToken): throw new AppError(ErrorCode.SecurityTokenNotFound, "Security Token Not Found"); } const loadedProject = await projectService.load(project, projectToken); + await ProjectService.checkAndUpdateSchema(loadedProject); const schemaUpdatedProject = await AssetService.checkAndUpdateSchema(loadedProject); dispatch(loadProjectAction(schemaUpdatedProject)); return schemaUpdatedProject; diff --git a/src/services/projectService.ts b/src/services/projectService.ts index d03965083..45fc5f79e 100644 --- a/src/services/projectService.ts +++ b/src/services/projectService.ts @@ -19,6 +19,7 @@ import packageJson from "../../package.json"; import { strings, interpolate } from "../common/strings"; import { toast } from "react-toastify"; import clone from "rfdc"; +import _ from "lodash"; // tslint:disable-next-line:no-var-requires const tagColors = require("../react/components/common/tagColors.json"); @@ -222,6 +223,27 @@ export default class ProjectService implements IProjectService { } } + public static async checkAndUpdateSchema(project: IProject): Promise { + try { + const storageProvider = StorageProviderFactory.createFromConnection(project.sourceConnection); + const fieldInfo = await ProjectService.getFieldInfo(project, storageProvider); + const fieldsSchema = _.get(fieldInfo, "$schema", ""); + if (ProjectService.shouldUpdateSchema(fieldsSchema)) { + fieldInfo["$schema"] = constants.fieldsSchema; + const fieldFilePath = joinPath("/", project.folderPath, constants.fieldsFileName); + await storageProvider.writeText(fieldFilePath, JSON.stringify(fieldInfo, null, 4)); + } + } catch (err) { + console.warn(err); + } + } + + private static shouldUpdateSchema(fieldsSchema: string) { + return fieldsSchema + && constants.supportedFieldsSchemas.has(fieldsSchema) + && fieldsSchema !== constants.fieldsSchema; + } + /** * Assign project tags. * A new project doesn't have any tags at the beginning. But it could connect to a blob container @@ -302,6 +324,24 @@ export default class ProjectService implements IProjectService { } } + /** + * Get fields info from fields.json file. + * @param project the project we're trying to create + * @param storageProvider the storage we're trying to save the project to + */ + private static getFieldInfo = async (project: IProject, storageProvider: IStorageProvider): Promise => { + const fieldFilePath = joinPath("/", project.folderPath, constants.fieldsFileName); + try { + const json = await storageProvider.readText(fieldFilePath, true); + return JSON.parse(json) as IFieldInfo; + } catch (err) { + if (err instanceof SyntaxError) { + const reason = interpolate(strings.errors.invalidJSONFormat.message, { fieldFilePath }); + toast.error(reason, { autoClose: false }); + } + } + } + /** * Assign project tags * A new project does not have any tags at the beginning. But it could connect to a blob container