UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint manager to set up, configure and monitor 3D printers. Our aim is to provide extremely optimized websocket performance and reliability.

92 lines (91 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "PrinterFilesStore", { enumerable: true, get: function() { return PrinterFilesStore; } }); const _runtimeexceptions = require("../exceptions/runtime.exceptions"); const _node = require("@sentry/node"); class PrinterFilesStore { printerCache; fileCache; printerApiFactory; logger; constructor(loggerFactory, printerCache, fileCache, printerApiFactory){ this.printerCache = printerCache; this.fileCache = fileCache; this.printerApiFactory = printerApiFactory; this.logger = loggerFactory(PrinterFilesStore.name); } async loadFilesStore() { const printers = await this.printerCache.listCachedPrinters(true); for (const printer of printers.filter((p)=>p.enabled)){ try { const printerFiles = await this.loadFiles(printer.id); this.fileCache.cachePrinterFiles(printer.id, printerFiles); } catch (e) { (0, _node.captureException)(e); this.logger.error(`Files store failed to load file list for printer ${printer.name}`); } } } async loadFiles(printerId) { const loginDto = await this.printerCache.getLoginDtoAsync(printerId); const printerApi = this.printerApiFactory.getScopedPrinter(loginDto); const files = await printerApi.getFiles(); this.fileCache.cachePrinterFiles(printerId, files); return files; } getFiles(printerId) { return this.fileCache.getPrinterFiles(printerId); } getOutdatedFiles(printerId, ageDaysMax) { if (!ageDaysMax) throw new _runtimeexceptions.ValidationException("ageDaysMax property is required to get printer's outdated files"); const printerFiles = this.getFiles(printerId); if (!printerFiles?.length) return []; const nowTimestampSeconds = Date.now() / 1000; return printerFiles.filter((file)=>!!file.date && file.date + ageDaysMax * 86400 < nowTimestampSeconds); } async deleteOutdatedFiles(printerId, ageDaysMax) { const printerApi = this.printerApiFactory.getById(printerId); const failedFiles = []; const succeededFiles = []; const nonRecursiveFiles = this.getOutdatedFiles(printerId, ageDaysMax); const name = (await this.printerCache.getCachedPrinterOrThrowAsync(printerId)).name; for (let file of nonRecursiveFiles){ try { await printerApi.deleteFile(file.path); succeededFiles.push(file); } catch (e) { failedFiles.push(file); } } this.logger.log(`Deleted ${succeededFiles.length} successfully and ${failedFiles.length} with failure for printer ${name}.`); return { failedFiles, succeededFiles }; } async purgePrinterFiles(printerId) { const printerState = await this.printerCache.getCachedPrinterOrThrowAsync(printerId); this.logger.log(`Purging file cache from printer`); this.fileCache.purgePrinterId(printerState.id); this.logger.log(`Clearing printer files successful`); } async purgeFiles() { const allPrinters = await this.printerCache.listCachedPrinters(); this.logger.log(`Clearing file caches`); for (let printer of allPrinters){ this.fileCache.purgePrinterId(printer.id); } this.logger.log(`Clearing caches successful.`); } async deleteFile(printerId, filePath) { this.fileCache.purgeFile(printerId, filePath); } } //# sourceMappingURL=printer-files.store.js.map