Skip to content

Commit

Permalink
Merge pull request #59 from ggchivalrous/feat/v1.5
Browse files Browse the repository at this point in the history
feat: 修改exiftool安装脚本
  • Loading branch information
ggchivalrous authored Mar 25, 2024
2 parents ba6f297 + f83f232 commit c52a4eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
54 changes: 35 additions & 19 deletions scripts/install-exiftool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ 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;
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');

Expand All @@ -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;
Expand Down

0 comments on commit c52a4eb

Please sign in to comment.