All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m24s
94 lines
2.5 KiB
TypeScript
94 lines
2.5 KiB
TypeScript
import fs from "node:fs";
|
|
import { Router } from "express";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import { db } from "../db/db.controller.js";
|
|
import { scanLog } from "../db/schema/scanlog.schema.js";
|
|
import { apiReturn } from "../utils/returnHelper.utils.js";
|
|
|
|
const router = Router();
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const downloadDir = path.resolve(__dirname, "../../downloads/mobile");
|
|
const projectRoot = path.resolve("./lstMobile"); // adjust as needed
|
|
const appJsonPath = path.join(projectRoot, "app.json");
|
|
|
|
const currentApk = {
|
|
fileName: "lst-mobile.apk",
|
|
};
|
|
|
|
router.get("/version", async (req, res) => {
|
|
const baseUrl = `${req.protocol}://${req.get("host")}`;
|
|
|
|
const raw = fs.readFileSync(appJsonPath, "utf-8");
|
|
const config = JSON.parse(raw);
|
|
|
|
const exp = config.expo;
|
|
|
|
res.json({
|
|
packageName: exp.android?.package,
|
|
versionName: exp.version,
|
|
versionCode: exp.android?.versionCode,
|
|
minSupportedVersionCode: exp?.android?.minSupportedVersionCode ?? 0,
|
|
downloadUrl: `${baseUrl}/lst/api/mobile/apk/latest`,
|
|
});
|
|
});
|
|
|
|
router.get("/apk/latest", (_, res) => {
|
|
const apkPath = path.join(downloadDir, currentApk.fileName);
|
|
|
|
if (!fs.existsSync(apkPath)) {
|
|
return res.status(404).json({ success: false, message: "APK not found" });
|
|
}
|
|
|
|
res.setHeader("Content-Type", "application/vnd.android.package-archive");
|
|
res.setHeader(
|
|
"Content-Disposition",
|
|
`attachment; filename="${currentApk.fileName}"`,
|
|
);
|
|
|
|
return res.sendFile(apkPath);
|
|
});
|
|
|
|
router.get("/apk/ehs", (_, res) => {
|
|
const apkPath = path.join(downloadDir, "EHS.apk");
|
|
|
|
if (!fs.existsSync(apkPath)) {
|
|
return res.status(404).json({ success: false, message: "APK not found" });
|
|
}
|
|
|
|
res.setHeader("Content-Type", "application/vnd.android.package-archive");
|
|
res.setHeader("Content-Disposition", `attachment; filename="EHS.apk}"`);
|
|
|
|
return res.sendFile(apkPath);
|
|
});
|
|
|
|
router.post("/logs", async (req, res) => {
|
|
const body = req.body;
|
|
const newLog = await db
|
|
.insert(scanLog)
|
|
.values({
|
|
scannerId: body.data.scannerId,
|
|
message: body.data.message,
|
|
prompt: body.data.prompt,
|
|
commandDescription: body.data.commandDescription,
|
|
status: body.data.status,
|
|
lines: body.data.lines,
|
|
})
|
|
.returning();
|
|
|
|
return apiReturn(res, {
|
|
success: true,
|
|
level: "info",
|
|
module: "mobile",
|
|
subModule: "scan logs",
|
|
message: `New log from ${body.data.scannerId}`,
|
|
data: newLog,
|
|
status: 200,
|
|
});
|
|
});
|
|
|
|
export default router;
|