94 lines
3.4 KiB
TypeScript
94 lines
3.4 KiB
TypeScript
import fs from "node:fs";
|
|
import AdmZip from "adm-zip";
|
|
import Ajv from "ajv";
|
|
import addFormats from "ajv-formats";
|
|
import { app } from "electron";
|
|
import log from "electron-log/main";
|
|
|
|
/**
|
|
* In charge to unzip data from showroom Bo,
|
|
* validate data config and check version
|
|
*/
|
|
export default class UnzipData {
|
|
static zipFileToUse: string|null;
|
|
|
|
/**
|
|
* Init zipFileToUse if not already inited
|
|
*/
|
|
private static init() {
|
|
if (!UnzipData.zipFileToUse) {
|
|
// * Try to locate last zip file and load it.
|
|
const zipFile = fs.readdirSync("static/data")
|
|
.map(path => { return {path: `static/data/${path}`, "stat": fs.lstatSync(`static/data/${path}`)}; })
|
|
.filter(obj => obj.stat.isFile() && obj.path.endsWith(".zip"))
|
|
.sort((objA, objB) => objB.stat.mtime.getTime() - objA.stat.mtime.getTime())
|
|
.at(0) ?? null;
|
|
|
|
if (zipFile) {
|
|
UnzipData.zipFileToUse = zipFile.path;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Decompress zip file content
|
|
* And fetch data.json content
|
|
*/
|
|
public static loadZipFileContent(): TypeApp|null {
|
|
UnzipData.init();
|
|
if (!UnzipData.zipFileToUse) {
|
|
return null;
|
|
}
|
|
|
|
const zip = new AdmZip(fs.readFileSync(UnzipData.zipFileToUse));
|
|
// * Clear export dir
|
|
fs.rmSync("public/assets", { recursive: true, force: true });
|
|
zip.extractAllTo("public/assets");
|
|
const jsonData = fs.readFileSync("public/assets/data.json", { encoding: "utf8", flag: "r" });
|
|
const jsonSchemaPath = "schemas";
|
|
|
|
if (!UnzipData.validateAgainstJsonSchema(jsonSchemaPath, jsonData)) {
|
|
log.warn(`Data for file ${UnzipData.zipFileToUse} is not valid JSON`);
|
|
return null;
|
|
}
|
|
|
|
const appJsonConfig: TypeApp = JSON.parse(jsonData);
|
|
const appVersion: string = app.getVersion();
|
|
|
|
if (appJsonConfig.version !== appVersion) {
|
|
throw new Error(`App version ${appVersion} is not compatible with config version ${appJsonConfig.version}`);
|
|
}
|
|
|
|
return appJsonConfig;
|
|
}
|
|
|
|
private static validateAgainstJsonSchema(jsonSchemaPath:string, jsonData: string): boolean {
|
|
try {
|
|
const schemaList = fs.readdirSync(jsonSchemaPath)
|
|
.filter(schemaPath => schemaPath.endsWith(".schema.json"))
|
|
.map(schemaPath => JSON.parse(fs.readFileSync(`schemas/${schemaPath}`, { encoding: "utf8", flag: "r" })) as Record<string,string|any>);
|
|
const appSchemaIndex = schemaList.findIndex((schema) => String(schema.$id).endsWith("app.schema.json"));
|
|
if (appSchemaIndex === -1) {
|
|
throw new Error("app.schema.json not found");
|
|
}
|
|
|
|
const appSchema = schemaList.splice(appSchemaIndex, 1).at(0);
|
|
if (!appSchema) {
|
|
throw new Error("app.schema.json not found");
|
|
}
|
|
|
|
const ajv = new Ajv();
|
|
addFormats(ajv);
|
|
ajv.addMetaSchema(appSchema);
|
|
ajv.addSchema(schemaList);
|
|
|
|
return ajv.validate(appSchema, JSON.parse(jsonData));
|
|
} catch (err:unknown) {
|
|
if (err instanceof Error) {
|
|
log.error(err);
|
|
return false;
|
|
}
|
|
throw new Error("validateAgainstJsonSchema unkown error");
|
|
}
|
|
}
|
|
}
|