|
| 1 | +import Archive from './archive'; |
| 2 | +import fs, { promises as fsPromises } from 'fs'; |
| 3 | +import JSZip, { InputType } from 'jszip'; |
| 4 | +import { ArchiveParams } from '../../types/types'; |
| 5 | +import IArchive, { ArchivedFile } from '../../interfaces/iarchive'; |
| 6 | +import { XmlDocument } from '../../types/xml-types'; |
| 7 | +import ArchiveJszip from './archive-jszip'; |
| 8 | +import { |
| 9 | + copyDir, |
| 10 | + ensureDirectoryExistence, |
| 11 | + exists, |
| 12 | + FileHelper, |
| 13 | + makeDirIfNotExists, |
| 14 | +} from '../file-helper'; |
| 15 | +import { vd } from '../general-helper'; |
| 16 | +import extract from 'extract-zip'; |
| 17 | + |
| 18 | +export default class ArchiveFs extends Archive implements IArchive { |
| 19 | + archive: boolean; |
| 20 | + params: ArchiveParams; |
| 21 | + dir: string = undefined; |
| 22 | + templatesDir: string; |
| 23 | + templateDir: string; |
| 24 | + outputDir: string; |
| 25 | + workDir: string; |
| 26 | + isActive: boolean; |
| 27 | + isRoot: boolean; |
| 28 | + |
| 29 | + constructor(filename: string, params: ArchiveParams) { |
| 30 | + super(filename); |
| 31 | + |
| 32 | + this.params = params; |
| 33 | + } |
| 34 | + |
| 35 | + private async initialize() { |
| 36 | + this.setPaths(); |
| 37 | + |
| 38 | + await this.assertDirs(); |
| 39 | + await this.extractFile(this.filename); |
| 40 | + |
| 41 | + if (!this.params.name) { |
| 42 | + await this.prepareWorkDir(this.filename); |
| 43 | + this.isRoot = true; |
| 44 | + } |
| 45 | + |
| 46 | + this.archive = true; |
| 47 | + |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + setPaths(): void { |
| 52 | + this.dir = this.params.baseDir + '/'; |
| 53 | + this.templatesDir = this.dir + 'templates' + '/'; |
| 54 | + this.outputDir = this.dir + 'output' + '/'; |
| 55 | + this.templateDir = undefined; |
| 56 | + this.workDir = this.outputDir + this.params.workDir + '/'; |
| 57 | + } |
| 58 | + |
| 59 | + async assertDirs(): Promise<void> { |
| 60 | + makeDirIfNotExists(this.dir); |
| 61 | + makeDirIfNotExists(this.templatesDir); |
| 62 | + makeDirIfNotExists(this.outputDir); |
| 63 | + makeDirIfNotExists(this.workDir); |
| 64 | + } |
| 65 | + |
| 66 | + async extractFile(file: string) { |
| 67 | + const targetDir = this.getTemplateDir(file); |
| 68 | + |
| 69 | + if (exists(targetDir)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + await extract(file, { dir: targetDir }).catch((err) => { |
| 74 | + throw err; |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + getTemplateDir(file: string): string { |
| 79 | + const info = FileHelper.getFileInfo(file); |
| 80 | + this.templateDir = this.templatesDir + info.base + '/'; |
| 81 | + return this.templateDir; |
| 82 | + } |
| 83 | + |
| 84 | + async prepareWorkDir(templateDir: string) { |
| 85 | + await this.cleanupWorkDir(); |
| 86 | + |
| 87 | + const fromTemplate = this.getTemplateDir(templateDir); |
| 88 | + await copyDir(fromTemplate, this.workDir); |
| 89 | + } |
| 90 | + |
| 91 | + fileExists(file: string) { |
| 92 | + return exists(this.getPath(file)); |
| 93 | + } |
| 94 | + |
| 95 | + async folder(dir: string): Promise<ArchivedFile[]> { |
| 96 | + const path = this.getPath(dir); |
| 97 | + const files = []; |
| 98 | + |
| 99 | + if (!exists(path)) { |
| 100 | + return files; |
| 101 | + } |
| 102 | + |
| 103 | + let entries = await fsPromises.readdir(path, { withFileTypes: true }); |
| 104 | + for (let entry of entries) { |
| 105 | + if (!entry.isDirectory()) { |
| 106 | + files.push({ |
| 107 | + name: dir + '/' + entry.name, |
| 108 | + relativePath: entry.name, |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return files; |
| 114 | + } |
| 115 | + |
| 116 | + async read(file: string): Promise<string | Buffer> { |
| 117 | + if (!this.archive) { |
| 118 | + await this.initialize(); |
| 119 | + } |
| 120 | + |
| 121 | + const path = this.getPath(file); |
| 122 | + return await fsPromises.readFile(path); |
| 123 | + } |
| 124 | + |
| 125 | + getPath(file: string): string { |
| 126 | + if (this.isRoot) { |
| 127 | + return this.workDir + file; |
| 128 | + } |
| 129 | + return this.templateDir + file; |
| 130 | + } |
| 131 | + |
| 132 | + async write(file: string, data: string | Buffer): Promise<this> { |
| 133 | + const filename = this.workDir + file; |
| 134 | + ensureDirectoryExistence(filename); |
| 135 | + await fsPromises.writeFile(filename, data); |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + async remove(file: string): Promise<void> { |
| 140 | + const path = this.getPath(file); |
| 141 | + if (exists(path)) { |
| 142 | + await fsPromises.unlink(path); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + async output(location: string): Promise<void> { |
| 147 | + await this.writeBuffer(this); |
| 148 | + |
| 149 | + let exec = require('child_process').exec; |
| 150 | + const command = `(cd ${this.workDir} && zip -r - .) > ${location}`; |
| 151 | + const script = await exec(command); |
| 152 | + |
| 153 | + script.on('exit', async (code) => { |
| 154 | + if (this.params.cleanupWorkDir === true) { |
| 155 | + await this.cleanupWorkDir(); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + async cleanupWorkDir(): Promise<void> { |
| 161 | + if (!exists(this.workDir)) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + await fsPromises.rm(this.workDir, { recursive: true, force: true }); |
| 166 | + } |
| 167 | + |
| 168 | + async readXml(file: string): Promise<XmlDocument> { |
| 169 | + const isBuffered = this.fromBuffer(file); |
| 170 | + |
| 171 | + if (!isBuffered) { |
| 172 | + const buffer = await this.read(file); |
| 173 | + if (!buffer) { |
| 174 | + throw 'no buffer: ' + file; |
| 175 | + } |
| 176 | + |
| 177 | + const xmlString = buffer.toString(); |
| 178 | + const XmlDocument = this.parseXml(xmlString); |
| 179 | + this.toBuffer(file, XmlDocument); |
| 180 | + |
| 181 | + return XmlDocument; |
| 182 | + } else { |
| 183 | + return isBuffered.content; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + writeXml(file: string, XmlDocument: XmlDocument): void { |
| 188 | + this.toBuffer(file, XmlDocument); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Used for worksheets only |
| 193 | + **/ |
| 194 | + async extract(file: string): Promise<ArchiveJszip> { |
| 195 | + const contents = (await this.read(file)) as Buffer; |
| 196 | + const zip = new JSZip(); |
| 197 | + const newArchive = new ArchiveJszip(file); |
| 198 | + newArchive.archive = await zip.loadAsync(contents as unknown as InputType); |
| 199 | + return newArchive; |
| 200 | + } |
| 201 | +} |
0 commit comments