Skip to content

Commit

Permalink
Check & update fields.json when project loaded. (microsoft#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
buddhawang authored Mar 8, 2021
1 parent 3924afe commit 1b48fd2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);

/**
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/redux/actions/projectActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions src/services/projectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -222,6 +223,27 @@ export default class ProjectService implements IProjectService {
}
}

public static async checkAndUpdateSchema(project: IProject): Promise<void> {
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
Expand Down Expand Up @@ -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<IFieldInfo> => {
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
Expand Down

0 comments on commit 1b48fd2

Please sign in to comment.