feat:meme使用oplist操作

This commit is contained in:
Jerry 2025-09-16 18:42:42 +08:00
parent 83fe8d5e45
commit bdfc7f43d1
3 changed files with 87 additions and 14 deletions

View File

@ -29,7 +29,7 @@ export class PathService {
modules: path.join(this.baseDir, 'src/modules'),
words: path.join(this.baseDir, 'private/words/src'),
private: path.join(this.baseDir, 'private'),
meme: path.join(this.baseDir, 'private/meme/src'),
meme: path.join(this.baseDir, 'private/meme'),
};
return type ? mappings[type] : this.baseDir;

View File

@ -2,12 +2,12 @@ import { Module } from '@nestjs/common';
import { MemeService } from './meme.service';
import { MemeController } from './meme.controller';
import { PathModule } from '../../core/path/path.module';
import { AutoUpdateModule } from '../../core/auto-update/auto-update.module';
import { ToolsModule } from '../../core/tools/tools.module';
import { RedisModule } from '../../core/redis/redis.module';
import { OpenListModule } from '../../core/openlist/openlist.module';
@Module({
imports: [PathModule, AutoUpdateModule, ToolsModule, RedisModule],
imports: [PathModule, OpenListModule, ToolsModule, RedisModule],
providers: [MemeService],
controllers: [MemeController],
})

View File

@ -2,7 +2,7 @@ import { Inject, Injectable, Logger } from '@nestjs/common';
import * as path from 'path';
import * as fs from 'fs/promises';
import { PathService } from '../../core/path/path.service';
import { AutoUpdateService } from '../../core/auto-update/auto-update.service';
import { OpenListService } from '../../core/openlist/openlist.service';
@Injectable()
export class MemeService {
@ -12,27 +12,100 @@ export class MemeService {
constructor(
@Inject(PathService)
private readonly pathService: PathService,
@Inject(AutoUpdateService)
private readonly autoUpdateService: AutoUpdateService,
@Inject(OpenListService)
private readonly openListService: OpenListService,
) {
this.startAutoUpdate();
}
private startAutoUpdate() {
setInterval(async () => {
//const memePath = this.pathService.get('meme');
const memePath = path.join(this.pathService.get('meme'), '..');
const memePath = path.join(this.pathService.get('meme'));
this.logger.log('定时检查表情仓库更新..');
const updated = await this.autoUpdateService.checkRepoForUpdates(
try {
const remoteFiles = await this.openListService.listFiles(memePath);
if (remoteFiles.code === 200) {
const remoteFileList = remoteFiles.data.content;
const localFiles = await this.getLocalFileList(memePath);
await this.compareAndDownloadFiles(
memePath,
'meme 仓库',
localFiles,
remoteFileList,
);
if (updated) {
this.logger.log('表情仓库已更新..');
} else {
this.logger.error('获取远程表情仓库文件失败..');
}
} catch (error) {
this.logger.error('定时检查表情仓库更新失败..', error);
}
}, this.updateMs);
}
/**
*
* @param dir
* @private
*/
private async getLocalFileList(dir: string): Promise<string[]> {
const files: string[] = []; //文件
const dirs: string[] = []; //目录
try {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
dirs.push(fullPath);
} else if (/\.(jpg|jpeg|png|gif|webp)$/i.test(entry.name)) {
files.push(fullPath);
}
}
for (const subDir of dirs) {
const subFiles = await this.getLocalFileList(subDir);
files.push(...subFiles);
}
} catch (error) {
this.logger.error(`读取本地目录失败: ${dir}`, error);
}
return files;
}
/**
* ,
* @param localPath
* @param localFiles
* @param remoteFiles
* @private
*/
private async compareAndDownloadFiles(
localPath: string,
localFiles: string[],
remoteFiles: any[],
) {
for (const remoteFile of remoteFiles) {
const remoteFilePath = path.join(localPath, remoteFile.name);
if (remoteFile.is_dir) {
await fs.mkdir(remoteFilePath, { recursive: true });
this.logger.log(`文件夹已创建: ${remoteFile.name}`);
await this.compareAndDownloadFiles(
remoteFilePath,
[],
remoteFile.content,
);
} else if (!localFiles.includes(remoteFilePath)) {
this.logger.log(`文件缺失: ${remoteFile.name},开始下载..`);
try {
await this.openListService.downloadFile(
remoteFile.raw_url,
remoteFilePath,
);
this.logger.log(`文件下载成功: ${remoteFile.name}`);
} catch (error) {
this.logger.error(`下载文件失败: ${remoteFile.name}`, error);
}
}
}
}
/**
*
* @param character