UNPKG

homebridge-config-ui-x

Version:

A web based management, configuration and control platform for Homebridge.

112 lines 5.68 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HbServiceService = void 0; const node_path_1 = require("node:path"); const node_stream_1 = require("node:stream"); const common_1 = require("@nestjs/common"); const fs_extra_1 = require("fs-extra"); const config_service_1 = require("../../../core/config/config.service"); const logger_service_1 = require("../../../core/logger/logger.service"); let HbServiceService = class HbServiceService { constructor(configService, logger) { this.configService = configService; this.logger = logger; this.hbServiceSettingsPath = (0, node_path_1.resolve)(this.configService.storagePath, '.uix-hb-service-homebridge-startup.json'); } async getHomebridgeStartupSettings() { try { if (await (0, fs_extra_1.pathExists)(this.hbServiceSettingsPath)) { const settings = await (0, fs_extra_1.readJson)(this.hbServiceSettingsPath); return { HOMEBRIDGE_DEBUG: settings.debugMode, HOMEBRIDGE_KEEP_ORPHANS: settings.keepOrphans, HOMEBRIDGE_INSECURE: typeof settings.insecureMode === 'boolean' ? settings.insecureMode : this.configService.homebridgeInsecureMode, ENV_DEBUG: settings.env.DEBUG, ENV_NODE_OPTIONS: settings.env.NODE_OPTIONS, }; } else { return { HOMEBRIDGE_INSECURE: this.configService.homebridgeInsecureMode, }; } } catch (e) { return {}; } } async setHomebridgeStartupSettings(data) { this.configService.hbServiceUiRestartRequired = true; const settings = { debugMode: data.HOMEBRIDGE_DEBUG, keepOrphans: data.HOMEBRIDGE_KEEP_ORPHANS, insecureMode: data.HOMEBRIDGE_INSECURE, env: { DEBUG: data.ENV_DEBUG ? data.ENV_DEBUG : undefined, NODE_OPTIONS: data.ENV_NODE_OPTIONS ? data.ENV_NODE_OPTIONS : undefined, }, }; return (0, fs_extra_1.writeJsonSync)(this.hbServiceSettingsPath, settings, { spaces: 4 }); } async setFullServiceRestartFlag() { this.configService.hbServiceUiRestartRequired = true; return { status: 0 }; } async downloadLogFile(shouldRemoveColour) { if (!await (0, fs_extra_1.pathExists)(this.configService.ui.log.path)) { this.logger.error(`Cannot download log file ${this.configService.ui.log.path} as it does not exist.`); throw new common_1.BadRequestException('Log file not found on disk.'); } try { await (0, fs_extra_1.access)(this.configService.ui.log.path, fs_extra_1.constants.R_OK); } catch (e) { this.logger.error(`Cannot download log file as missing read permissions on ${this.configService.ui.log.path}.`); throw new common_1.BadRequestException('Cannot read log file. Check the log file permissions.'); } if (shouldRemoveColour) { return (0, fs_extra_1.createReadStream)(this.configService.ui.log.path, { encoding: 'utf8' }); } const removeColour = new node_stream_1.Transform({ transform(chunk, _encoding, callback) { callback(null, chunk.toString().replace(/\x1B\[(\d{1,3}(;\d{1,2})?)?[mGK]/g, '')); }, }); return (0, fs_extra_1.createReadStream)(this.configService.ui.log.path, { encoding: 'utf8' }) .pipe(removeColour); } async truncateLogFile(username) { if (!await (0, fs_extra_1.pathExists)(this.configService.ui.log.path)) { this.logger.error(`Cannot truncate log file ${this.configService.ui.log.path} as it does not exist.`); throw new common_1.BadRequestException('Log file not found on disk.'); } try { await (0, fs_extra_1.access)(this.configService.ui.log.path, fs_extra_1.constants.R_OK | fs_extra_1.constants.W_OK); } catch (e) { this.logger.error(`Cannot truncate log file as missing write permissions on ${this.configService.ui.log.path}.`); throw new common_1.BadRequestException('Cannot access file. Check the log file permissions.'); } await (0, fs_extra_1.truncate)(this.configService.ui.log.path); setTimeout(() => { this.logger.warn(`Homebridge log truncated by ${username || 'user'}.`); }, 1000); return { status: 0 }; } }; exports.HbServiceService = HbServiceService; exports.HbServiceService = HbServiceService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [config_service_1.ConfigService, logger_service_1.Logger]) ], HbServiceService); //# sourceMappingURL=hb-service.service.js.map