|
| 1 | +import * as path from "node:path"; |
| 2 | +import { path7za } from "7zip-bin"; |
| 3 | +import { ILogger } from "@spt/models/spt/utils/ILogger"; |
| 4 | +import * as fs from "fs-extra"; |
| 5 | +import * as Seven from "node-7z"; |
| 6 | +import { inject, injectable } from "tsyringe"; |
| 7 | + |
| 8 | +@injectable() |
| 9 | +export class DatabaseDecompressionUtil { |
| 10 | + private compressedDir: string; |
| 11 | + private assetsDir: string; |
| 12 | + |
| 13 | + constructor(@inject("PrimaryLogger") protected logger: ILogger) { |
| 14 | + this.compressedDir = path.resolve(__dirname, "../../assets/compressed/database"); |
| 15 | + this.assetsDir = path.resolve(__dirname, "../../assets/database"); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Initializes the database compression utility. |
| 20 | + * |
| 21 | + * This method will decompress all 7-zip archives within the compressed database directory. |
| 22 | + * The decompressed files are placed in their respective directories based on the name |
| 23 | + * and location of the compressed file. |
| 24 | + */ |
| 25 | + public async initialize(): Promise<void> { |
| 26 | + try { |
| 27 | + const compressedFiles = await this.getCompressedFiles(); |
| 28 | + if (compressedFiles.length === 0) { |
| 29 | + this.logger.debug("No database archives found"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + for (const compressedFile of compressedFiles) { |
| 34 | + await this.processCompressedFile(compressedFile); |
| 35 | + } |
| 36 | + this.logger.info("Database archives processed"); |
| 37 | + } catch (error) { |
| 38 | + this.logger.error(`Error handling database archives: ${error}`); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Retrieves a list of all 7-zip archives within the compressed database directory. |
| 44 | + */ |
| 45 | + private async getCompressedFiles(): Promise<string[]> { |
| 46 | + try { |
| 47 | + const files = await fs.readdir(this.compressedDir); |
| 48 | + const compressedFiles = files.filter((file) => file.endsWith(".7z")); |
| 49 | + return compressedFiles; |
| 50 | + } catch (error) { |
| 51 | + this.logger.error(`Error reading database archive directory: ${error}`); |
| 52 | + return []; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Processes a compressed file by checking if the target directory is empty, |
| 58 | + * and if so, decompressing the file into the target directory. |
| 59 | + */ |
| 60 | + private async processCompressedFile(compressedFileName: string): Promise<void> { |
| 61 | + this.logger.info("Processing database archives..."); |
| 62 | + |
| 63 | + const compressedFilePath = path.join(this.compressedDir, compressedFileName); |
| 64 | + const relativeTargetPath = compressedFileName.replace(".7z", ""); |
| 65 | + const targetDir = path.join(this.assetsDir, relativeTargetPath); |
| 66 | + |
| 67 | + try { |
| 68 | + this.logger.debug(`Processing: ${compressedFileName}`); |
| 69 | + |
| 70 | + const isTargetDirEmpty = await this.isDirectoryEmpty(targetDir); |
| 71 | + if (!isTargetDirEmpty) { |
| 72 | + this.logger.debug(`Archive target directory not empty, skipping: ${targetDir}`); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + await this.decompressFile(compressedFilePath, targetDir); |
| 77 | + |
| 78 | + this.logger.debug(`Successfully processed: ${compressedFileName}`); |
| 79 | + } catch (error) { |
| 80 | + this.logger.error(`Error processing ${compressedFileName}: ${error}`); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Checks if a directory exists and is empty. |
| 86 | + */ |
| 87 | + private async isDirectoryEmpty(directoryPath: string): Promise<boolean> { |
| 88 | + try { |
| 89 | + const exists = await fs.pathExists(directoryPath); |
| 90 | + if (!exists) { |
| 91 | + return true; // Directory doesn't exist, consider it empty. |
| 92 | + } |
| 93 | + const files = await fs.readdir(directoryPath); |
| 94 | + return files.length === 0; |
| 95 | + } catch (error) { |
| 96 | + this.logger.error(`Error checking if directory is empty ${directoryPath}: ${error}`); |
| 97 | + throw error; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Decompresses a 7-zip archive to the target directory. |
| 103 | + */ |
| 104 | + private decompressFile(archivePath: string, destinationPath: string): Promise<void> { |
| 105 | + return new Promise((resolve, reject) => { |
| 106 | + const myStream = Seven.extractFull(archivePath, destinationPath, { |
| 107 | + $bin: path7za, |
| 108 | + overwrite: "a", |
| 109 | + }); |
| 110 | + |
| 111 | + let hadError = false; |
| 112 | + |
| 113 | + myStream.on("end", () => { |
| 114 | + if (!hadError) { |
| 115 | + this.logger.debug(`Decompressed ${archivePath} to ${destinationPath}`); |
| 116 | + resolve(); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + myStream.on("error", (err) => { |
| 121 | + hadError = true; |
| 122 | + this.logger.error(`Error decompressing ${archivePath}: ${err}`); |
| 123 | + reject(err); |
| 124 | + }); |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
0 commit comments