From f83f2324dd7cf12c74d0c46b3a1754c284266202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E4=B8=AD=E5=90=9B?= Date: Tue, 26 Mar 2024 01:00:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9exiftool=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 ++ scripts/install-exiftool.ts | 54 ++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 8cfd340..4606787 100644 --- a/package.json +++ b/package.json @@ -24,10 +24,12 @@ "devDependencies": { "@ggchivalrous/db-ui": "^1.3.1", "@sveltejs/vite-plugin-svelte": "^3.0.1", + "@types/adm-zip": "^0.5.5", "@types/fluent-ffmpeg": "^2.1.21", "@types/tar": "^6.1.11", "@typescript-eslint/eslint-plugin": "^5.57.0", "@typescript-eslint/parser": "^5.57.0", + "adm-zip": "^0.5.12", "axios": "^1.6.5", "electron": "24.2.0", "electron-builder": "^24.6.3", diff --git a/scripts/install-exiftool.ts b/scripts/install-exiftool.ts index 4c3e5f6..93dde6d 100644 --- a/scripts/install-exiftool.ts +++ b/scripts/install-exiftool.ts @@ -4,9 +4,10 @@ import os from 'os'; import path from 'path'; import zlib from 'zlib'; +import AdmZip from 'adm-zip'; import tar from 'tar'; -import { usePromise } from '../common/utils'; +import { tryCatch, usePromise } from '../common/utils'; const platform = os.platform(); const __dirname = path.parse(import.meta.url.slice(platform === 'win32' ? 8 : 7)).dir; @@ -14,7 +15,6 @@ const WindowsExifToolPath = path.join(__dirname, '../static/windows-exiftool.zip const CommondExifToolPath = path.join(__dirname, '../static/commond-exiftool.tar.gz'); export default async (outPath: string) => { - const [promise, r] = usePromise(); const outDir = path.join(outPath, 'exiftool'); const exiftoolCommodPath = path.join(outDir, 'exiftool'); @@ -23,39 +23,55 @@ export default async (outPath: string) => { } if (fs.existsSync(exiftoolCommodPath)) { - r(true); - return promise; + return; } - const exiftoolPath = platform === 'win32' ? WindowsExifToolPath : CommondExifToolPath; - fs.createReadStream(exiftoolPath) - .pipe(platform === 'win32' ? zlib.createUnzip() : zlib.createGunzip()) + if (platform === 'win32') { + await installWinExiftool(outDir); + } + else { + await installCommondExiftool(outDir); + } +}; + +function installWinExiftool(outDir: string) { + const [p, r] = usePromise(); + + tryCatch(() => { + const admzip = new AdmZip(WindowsExifToolPath); + admzip.extractAllTo(outDir); + fs.renameSync(path.join(outDir, 'exiftool(-k).exe'), path.join(outDir, 'exiftool')); + r(true); + }, null, () => r(false)); + + return p; +} + +function installCommondExiftool(outDir: string) { + const [p, r] = usePromise(); + + fs.createReadStream(CommondExifToolPath) + .pipe(zlib.createGunzip()) + .pipe(tar.extract({ cwd: outDir })) .on('error', (e) => { console.log('Exiftool模块解压异常', e); r(false); }) - .pipe( - platform === 'darwin' - ? tar.extract({ cwd: outDir }) - : fs.createWriteStream(exiftoolCommodPath), - ) .on('finish', () => { console.log('Exiftool工具解压完成'); // mac的压缩包套了一层文件夹,需要将文件夹的内容全部复制到上一层 const fileList = fs.readdirSync(outDir); - if (fileList.length === 1) { - const filePath = path.join(outDir, fileList[0]); - if (fs.statSync(filePath)?.isDirectory()) { - cpDirAllFile(filePath, outDir); - } + const filePath = path.join(outDir, fileList[0]); + if (fs.statSync(filePath)?.isDirectory()) { + cpDirAllFile(filePath, outDir); } r(true); }); - return promise; -}; + return p; +} function cpDirAllFile(origin: string, target: string) { if (!fs.statSync(origin).isDirectory()) return;