80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { Router } from "express";
|
|
import { Machine } from "external/db/entity/allnet";
|
|
import CreateLogCtx from "lib/logger/logger";
|
|
import { Config } from "lib/setup/config";
|
|
import {
|
|
DownloadOrderRequestSchema,
|
|
DownloadOrderStatus,
|
|
} from "servers/allnet/types/download-order";
|
|
import { fromZodError } from "zod-validation-error";
|
|
import { existsSync } from "fs";
|
|
import { join } from "path";
|
|
import type { DownloadOrderResponse } from "servers/allnet/types/download-order";
|
|
|
|
const logger = CreateLogCtx(__filename);
|
|
|
|
const router: Router = Router({ mergeParams: true });
|
|
|
|
router.post("/", async (req, res) => {
|
|
if (
|
|
!Config.ALLNET_CONFIG.UPDATE_CFG_FOLDER ||
|
|
!existsSync(Config.ALLNET_CONFIG.UPDATE_CFG_FOLDER)
|
|
) {
|
|
// Probably shouldn't place a warning.
|
|
|
|
return res.status(200).send({ stat: DownloadOrderStatus.SUCCESS, serial: "", uri: "null" });
|
|
}
|
|
|
|
const parseResult = DownloadOrderRequestSchema.safeParse(req.safeBody);
|
|
|
|
if (!parseResult.success) {
|
|
logger.error("Received invalid download order request.", {
|
|
error: fromZodError(parseResult.error).message,
|
|
body: req.safeBody,
|
|
});
|
|
return res.status(403).send("");
|
|
}
|
|
|
|
const data = parseResult.data;
|
|
|
|
if (!Config.ALLNET_CONFIG.ALLOW_UNREGISTERED_SERIALS) {
|
|
const machine = await Machine.findOne({
|
|
where: {
|
|
serial: data.serial,
|
|
},
|
|
});
|
|
|
|
if (!machine) {
|
|
logger.error("Rejected download order request from unknown serial.", { data });
|
|
return res
|
|
.status(200)
|
|
.send({ stat: DownloadOrderStatus.SUCCESS, serial: "", uri: "null" });
|
|
}
|
|
}
|
|
|
|
const appIniFilename = `${data.game_id}-${data.ver.replace(/\./u, "")}-app.ini`;
|
|
const optIniFilename = `${data.game_id}-${data.ver.replace(/\./u, "")}-opt.ini`;
|
|
const appIniPath = join(Config.ALLNET_CONFIG.UPDATE_CFG_FOLDER, appIniFilename);
|
|
const optIniPath = join(Config.ALLNET_CONFIG.UPDATE_CFG_FOLDER, optIniFilename);
|
|
let uri = "";
|
|
|
|
if (existsSync(appIniPath)) {
|
|
uri = `http://${Config.ALLNET_CONFIG.HOSTNAME}/dl/ini/${appIniFilename}`;
|
|
}
|
|
|
|
if (existsSync(optIniPath)) {
|
|
uri = `${uri}|http://${Config.ALLNET_CONFIG.HOSTNAME}/dl/ini/${optIniFilename}`;
|
|
}
|
|
|
|
// TODO: Allow network delivery.
|
|
const response = {
|
|
stat: DownloadOrderStatus.SUCCESS,
|
|
serial: "",
|
|
uri,
|
|
} satisfies DownloadOrderResponse;
|
|
|
|
return res.status(200).send(response);
|
|
});
|
|
|
|
export default router;
|