@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
194 lines (193 loc) • 8.81 kB
JavaScript
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
import { __decorateMetadata } from "../_virtual/_@oxc-project_runtime@0.129.0/helpers/decorateMetadata.js";
import { __decorate } from "../_virtual/_@oxc-project_runtime@0.129.0/helpers/decorate.js";
import { validateMiddleware } from "../handlers/validators.js";
import { AppConstants } from "../server.constants.js";
import { ServerReleaseService } from "../services/core/server-release.service.js";
import { GithubService } from "../services/core/github.service.js";
import { ROLES } from "../constants/authorization.constants.js";
import { MulterService } from "../services/core/multer.service.js";
import { authenticate, authorizeRoles } from "../middleware/authenticate.js";
import { demoUserNotAllowed } from "../middleware/demo.middleware.js";
import { YamlService } from "../services/core/yaml.service.js";
import { PrinterCache } from "../state/printer.cache.js";
import { ClientBundleService } from "../services/core/client-bundle.service.js";
import { LogDumpService } from "../services/core/logs-manager.service.js";
import { updateClientBundleSchema } from "./validation/server-private.validation.js";
import { DELETE, GET, POST, before, route } from "awilix-express";
import { PassThrough } from "node:stream";
//#region src/controllers/server-private.controller.ts
var server_private_controller_exports = /* @__PURE__ */ __exportAll({ ServerPrivateController: () => ServerPrivateController });
var _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ServerPrivateController;
let ServerPrivateController = _ServerPrivateController = class ServerPrivateController {
logger;
constructor(loggerFactory, serverReleaseService, printerCache, printerService, clientBundleService, githubService, logDumpService, yamlService, multerService) {
this.serverReleaseService = serverReleaseService;
this.printerCache = printerCache;
this.printerService = printerService;
this.clientBundleService = clientBundleService;
this.githubService = githubService;
this.logDumpService = logDumpService;
this.yamlService = yamlService;
this.multerService = multerService;
this.logger = loggerFactory(_ServerPrivateController.name);
}
async getReleaseStateInfo(req, res) {
await this.serverReleaseService.syncLatestRelease();
const updateState = this.serverReleaseService.getState();
res.send(updateState);
}
async getClientReleases(_req, res) {
const releaseSpec = await this.clientBundleService.getReleases();
res.send(releaseSpec);
}
/**
* It is not advised to downgrade beyond the default minimum version, any server restart will
* update the bundle back to minimum version (if ENABLE_CLIENT_DIST_AUTO_UPDATE === 'true').
*/
async updateClientBundleGithub(req, res) {
const updateDto = await validateMiddleware(req, updateClientBundleSchema);
const willExecute = await this.clientBundleService.shouldUpdateWithReason(true, AppConstants.defaultClientMinimum, updateDto.downloadRelease, updateDto.allowDowngrade);
this.logger.log(`Will execute: ${willExecute?.shouldUpdate}, reason: ${willExecute?.reason}`);
if (!willExecute?.shouldUpdate) return res.send({
executed: false,
requestedVersion: willExecute.requestedVersion,
currentVersion: willExecute.currentVersion,
minimumVersion: willExecute.minimumVersion,
shouldUpdate: willExecute.shouldUpdate,
targetVersion: willExecute.targetVersion,
reason: willExecute?.reason
});
if (willExecute.targetVersion) await this.clientBundleService.downloadClientUpdate(willExecute.targetVersion);
return res.send({
executed: true,
requestedVersion: willExecute.requestedVersion,
currentVersion: willExecute.currentVersion,
minimumVersion: willExecute.minimumVersion,
shouldUpdate: willExecute.shouldUpdate,
targetVersion: willExecute.targetVersion,
reason: willExecute?.reason
});
}
async getGithubRateLimit(req, res) {
const rateLimitResponse = await this.githubService.getRateLimit();
res.send(rateLimitResponse.data);
}
async importYaml(req, res) {
const firstFile = (await this.multerService.multerLoadFileAsync(req, res, [".yaml", ".yml"], false))[0];
const spec = await this.yamlService.importYaml(firstFile.buffer.toString());
res.send({
success: true,
spec
});
}
async exportYaml(req, res) {
const yaml = await this.yamlService.exportYaml(req.body);
const fileContents = Buffer.from(yaml);
const readStream = new PassThrough();
readStream.end(fileContents);
const fileName = `export-${AppConstants.serverRepoName}-` + Date.now() + ".yaml";
res.set("Content-disposition", "attachment; filename=" + fileName);
res.set("Content-Type", "text/plain");
readStream.pipe(res);
}
async deleteAllPrinters(req, res) {
const printerIds = (await this.printerCache.listCachedPrinters(true)).map((p) => p.id);
await this.printerService.deleteMany(printerIds);
res.send();
}
async clearLogs(req, res) {
const counts = await this.logDumpService.deleteOlderThanWeekAndMismatchingLogFiles();
res.send(counts);
}
async dumpLogZips(req, res) {
const filePath = await this.logDumpService.dumpZip();
res.sendFile(filePath);
}
};
__decorate([
GET(),
route("/"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "getReleaseStateInfo", null);
__decorate([
GET(),
route("/client-releases"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "getClientReleases", null);
__decorate([
POST(),
route("/update-client-bundle-github"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "updateClientBundleGithub", null);
__decorate([
GET(),
route("/github-rate-limit"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "getGithubRateLimit", null);
__decorate([
POST(),
route("/yaml-import"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "importYaml", null);
__decorate([
POST(),
route("/yaml-export"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "exportYaml", null);
__decorate([
DELETE(),
route("/delete-all-printers"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "deleteAllPrinters", null);
__decorate([
DELETE(),
route("/clear-outdated-fdm-monster-logs"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "clearLogs", null);
__decorate([
GET(),
POST(),
route("/dump-fdm-monster-logs"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], ServerPrivateController.prototype, "dumpLogZips", null);
ServerPrivateController = _ServerPrivateController = __decorate([
route(AppConstants.apiRoute + "/server"),
before([
authenticate(),
authorizeRoles([ROLES.ADMIN]),
demoUserNotAllowed
]),
__decorateMetadata("design:paramtypes", [
Object,
typeof (_ref = typeof ServerReleaseService !== "undefined" && ServerReleaseService) === "function" ? _ref : Object,
typeof (_ref2 = typeof PrinterCache !== "undefined" && PrinterCache) === "function" ? _ref2 : Object,
Object,
typeof (_ref3 = typeof ClientBundleService !== "undefined" && ClientBundleService) === "function" ? _ref3 : Object,
typeof (_ref4 = typeof GithubService !== "undefined" && GithubService) === "function" ? _ref4 : Object,
typeof (_ref5 = typeof LogDumpService !== "undefined" && LogDumpService) === "function" ? _ref5 : Object,
typeof (_ref6 = typeof YamlService !== "undefined" && YamlService) === "function" ? _ref6 : Object,
typeof (_ref7 = typeof MulterService !== "undefined" && MulterService) === "function" ? _ref7 : Object
])
], ServerPrivateController);
//#endregion
export { ServerPrivateController, server_private_controller_exports };
//# sourceMappingURL=server-private.controller.js.map