From 2e5249d30b6d756ef79f7d055b02757ea542c844 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Fri, 1 Mar 2019 20:11:37 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8,=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/FileDecompressor.js | 37 ++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/server/core/FileDecompressor.js b/server/core/FileDecompressor.js index 7d7d3853..99f201db 100644 --- a/server/core/FileDecompressor.js +++ b/server/core/FileDecompressor.js @@ -31,7 +31,7 @@ class FileDecompressor { return result; } - result.files = await this.decompressTar(fileType.ext, filename, outputDir); + result.files = await this.decompressTarZZ(fileType.ext, filename, outputDir); let sel = filename; let max = 0; @@ -53,8 +53,39 @@ class FileDecompressor { return result; } - async decompressTar(fileExt, filename, outputDir) { - return await this.decompress(fileExt, filename, outputDir); + async unpack(filename, outputDir) { + const fileType = await this.detector.detectFile(filename); + if (!fileType) + throw new Error('Не удалось определить формат файла'); + + return await this.decompress(fileType.ext, filename, outputDir); + } + + async unpackTarZZ(filename, outputDir) { + const fileType = await this.detector.detectFile(filename); + if (!fileType) + throw new Error('Не удалось определить формат файла'); + + return await this.decompressTarZZ(fileType.ext, filename, outputDir); + } + + async decompressTarZZ(fileExt, filename, outputDir) { + const files = await this.decompress(fileExt, filename, outputDir); + if (fileExt == 'tar' || files.length != 1) + return files; + + const tarFilename = `${outputDir}/${files[0].path}`; + const fileType = await this.detector.detectFile(tarFilename); + if (!fileType || fileType.ext != 'tar') + return files; + + const newTarFilename = `${outputDir}/${utils.randomHexString(30)}`; + await fs.rename(tarFilename, newTarFilename); + + const tarFiles = await this.decompress('tar', newTarFilename, outputDir); + await fs.remove(newTarFilename); + + return tarFiles; } async decompress(fileExt, filename, outputDir) {