add check sync

This commit is contained in:
2025-05-16 02:54:30 +08:00
parent aab0662fbc
commit 4e007d48a4
7 changed files with 186 additions and 18 deletions

View File

@@ -1,11 +1,13 @@
import { program as app, Command } from '@/program.ts';
import { SyncBase } from './modules/base.ts';
import { baseURL, storage } from '@/module/query.ts';
import { fetchLink } from '@/module/download/install.ts';
import { fetchLink, fetchAiList } from '@/module/download/install.ts';
import fs from 'node:fs';
import { upload } from '@/module/download/upload.ts';
import { logger } from '@/module/logger.ts';
import { chalk } from '@/module/chalk.ts';
import path, { relative } from 'node:path';
import { fileIsExist } from '@/uitls/file.ts';
const command = new Command('sync')
.option('-d --dir <dir>')
@@ -33,7 +35,7 @@ const syncUpload = new Command('upload')
if (opts.share) {
meta.share = opts.share;
}
const filepath = sync.getRelativeFile(opts.file);
const filepath = sync.getRelativePath(opts.file);
for (const item of syncList) {
if (!item.auth || !item.exist) {
nodonwArr.push(item);
@@ -79,7 +81,7 @@ const syncDownload = new Command('download')
const syncList = await sync.getSyncList();
logger.debug(syncList);
const nodonwArr: (typeof syncList)[number][] = [];
const filepath = sync.getRelativeFile(opts.file);
const filepath = sync.getRelativePath(opts.file);
for (const item of syncList) {
if (!sync.canDone(item.type, 'download')) {
nodonwArr.push(item);
@@ -134,7 +136,7 @@ const syncCreateList = new Command('create')
});
const newJson = { ...sync.config };
newJson.sync = newSync;
const filepath = sync.getRelativeFile(opts.output);
const filepath = sync.getRelativePath(opts.output);
if (filepath) {
logger.debug('输出文件', filepath);
fs.writeFileSync(filepath.absolute, JSON.stringify(newJson, null, 2));
@@ -144,9 +146,78 @@ const syncCreateList = new Command('create')
}
});
const checkDir = new Command('check')
.option('-d --dir <dir>', '配置目录')
.option('-c --config <config>', '配置文件的名字', 'kevisual.json')
.description('检查目录')
.action(async (opts) => {
const sync = new SyncBase({ dir: opts.dir, baseURL: baseURL, configFilename: opts.config });
const syncList = await sync.getSyncList();
logger.debug(syncList);
logger.info('检查目录\n');
const checkList = await sync.getCheckList();
for (const item of checkList) {
if (!item.auth) {
continue;
}
if (!item.enabled) {
logger.info('提示:', item.key, chalk.yellow('未启用'));
continue;
}
const res = await fetchAiList(item.url, { recursive: true });
if (res.code === 200) {
const data = res?.data || [];
let matchObjectList = data.filter((dataItem) => {
// 把 pathname 和 path 合并成一个路径
dataItem.pathname = path.join(item.key || '', dataItem.path);
return dataItem;
});
matchObjectList = sync.getMatchList({ ignore: item.ignore, matchObjectList }).matchObjectList;
const matchList = matchObjectList
.map((item2) => {
const rp = sync.getRelativePath(item2.pathname);
if (!rp) return false;
return { ...item2, relative: rp.relative, absolute: rp.absolute };
})
.filter((i) => i);
for (const matchItem of matchList) {
if (!matchItem) continue;
let needDownload = true;
let hash = '';
await sync.getDir(matchItem.absolute, true);
logger.debug('文件路径', matchItem.absolute);
if (fileIsExist(matchItem.absolute)) {
hash = sync.getHash(matchItem.absolute);
if (hash !== matchItem.etag) {
logger.error('文件不一致', matchItem.pathname, chalk.red(matchItem.url), chalk.red('文件不一致'));
} else {
needDownload = false;
logger.info('文件一致', matchItem.pathname, chalk.green(matchItem.url), chalk.green('文件一致'));
}
}
if (needDownload) {
const { content, status } = await fetchLink(matchItem.url, { setToken: item.auth, returnContent: true, hash });
if (status === 200) {
fs.writeFileSync(matchItem.absolute, content);
logger.info('下载成功', matchItem.pathname, chalk.green(matchItem.url));
} else if (status === 304) {
logger.info('文件未修改', matchItem.pathname, chalk.green(matchItem.url));
} else {
logger.error('下载失败', matchItem.pathname, chalk.red(matchItem.url));
}
}
}
} else {
logger.error('检查失败', item.url, res.code);
break;
}
}
});
command.addCommand(syncUpload);
command.addCommand(syncDownload);
command.addCommand(syncList);
command.addCommand(syncCreateList);
command.addCommand(checkDir);
app.addCommand(command);