From beebcc14f9902cd72caf333a35632b0e8134da46 Mon Sep 17 00:00:00 2001 From: Jerryplusy Date: Wed, 17 Sep 2025 13:57:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:meme=E6=9C=8D=E5=8A=A1=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/config.service.ts | 4 +-- src/core/openlist/openlist.service.ts | 2 +- src/core/openlist/openlist.types.ts | 20 +++++++------ src/core/openlist/openlist.utils.ts | 18 +++++++++--- src/core/path/path.service.ts | 1 - src/modules/meme/meme.module.ts | 9 +++++- src/modules/meme/meme.service.ts | 42 +++++++++++++++++---------- 7 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/config/config.service.ts b/src/config/config.service.ts index 466146d..295983f 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -36,7 +36,7 @@ export class AppConfigService implements OnModuleInit { * 检查并补全 .env 文件 */ private checkAndSyncEnv(): void { - this.logger.log('检查并同步 .env 与 .env.example ...'); + this.logger.log('检查并同步 .env 与 .envExample ...'); if (!fs.existsSync(this.envExamplePath)) { this.logger.error(`缺少 ${this.envExamplePath} 文件,无法校验`); @@ -67,7 +67,7 @@ export class AppConfigService implements OnModuleInit { }); this.logger.log('.env 已自动补全缺失项'); } else { - this.logger.log('.env 已与 .env.example 保持一致'); + this.logger.log('.env 已与 .envExample 保持一致'); } } diff --git a/src/core/openlist/openlist.service.ts b/src/core/openlist/openlist.service.ts index 82e5ebe..7cc24f0 100644 --- a/src/core/openlist/openlist.service.ts +++ b/src/core/openlist/openlist.service.ts @@ -66,7 +66,7 @@ export class OpenListService { /** * 列出目录下的所有文件 - * @param path 目录路径 + * @param path 服务器目录路径 * @returns 目录下的文件列表 */ public async listFiles(path: string): Promise { diff --git a/src/core/openlist/openlist.types.ts b/src/core/openlist/openlist.types.ts index 60ca161..912ef41 100644 --- a/src/core/openlist/openlist.types.ts +++ b/src/core/openlist/openlist.types.ts @@ -5,15 +5,17 @@ export interface FsList { code: number; message: string; data: { - content: [ - name: string, - size: number, - is_dir: boolean, - modified: string, //修改时间 - sign: string, //签名 - thumb: string, //略缩图 - type: number, //类型 - ]; + content: + | [ + name: string, + size: number, + is_dir: boolean, + modified: string, //修改时间 + sign: string, //签名 + thumb: string, //略缩图 + type: number, //类型 + ] + | null; total: number; //总数 readme: string; //说明? write: boolean; //是否可写入 diff --git a/src/core/openlist/openlist.utils.ts b/src/core/openlist/openlist.utils.ts index 1893cf7..0b4f1d7 100644 --- a/src/core/openlist/openlist.utils.ts +++ b/src/core/openlist/openlist.utils.ts @@ -50,10 +50,20 @@ export class OpenListUtils { static async listDirectory(token: string, path: string): Promise { const url = `${this.apiBaseUrl}/api/fs/list`; try { - const response = await axios.get(url, { - params: { path }, - headers: { Authorization: `Bearer ${token}` }, + let data = JSON.stringify({ + path: path, }); + //this.logger.debug(path); + let config = { + method: 'post', + url: `${url}`, + headers: { + 'Content-Type': 'application/json', + Authorization: `${token}`, + }, + data: data, + }; + let response = await axios(config); this.logger.log(`列出目录${path}成功..`); return response.data; } catch (error) { @@ -72,7 +82,7 @@ export class OpenListUtils { try { const response = await axios.get(url, { params: { path: filePath }, - headers: { Authorization: `Bearer ${token}` }, + headers: { Authorization: `${token}` }, }); this.logger.log('获取文件信息成功..'); return response.data; diff --git a/src/core/path/path.service.ts b/src/core/path/path.service.ts index f43bd90..20c342b 100644 --- a/src/core/path/path.service.ts +++ b/src/core/path/path.service.ts @@ -71,7 +71,6 @@ export class PathService { this.logger.debug(`成功创建目录: ${dirToCreate}`); } catch (err) { this.logger.error(`创建目录失败: ${err}`); - throw err; } } } diff --git a/src/modules/meme/meme.module.ts b/src/modules/meme/meme.module.ts index c9fe6f2..479f613 100644 --- a/src/modules/meme/meme.module.ts +++ b/src/modules/meme/meme.module.ts @@ -5,9 +5,16 @@ import { PathModule } from '../../core/path/path.module'; import { ToolsModule } from '../../core/tools/tools.module'; import { RedisModule } from '../../core/redis/redis.module'; import { OpenListModule } from '../../core/openlist/openlist.module'; +import { AppConfigModule } from '../../config/config.module'; @Module({ - imports: [PathModule, OpenListModule, ToolsModule, RedisModule], + imports: [ + PathModule, + OpenListModule, + ToolsModule, + RedisModule, + AppConfigModule, + ], providers: [MemeService], controllers: [MemeController], }) diff --git a/src/modules/meme/meme.service.ts b/src/modules/meme/meme.service.ts index d1d66e0..3b9b75d 100644 --- a/src/modules/meme/meme.service.ts +++ b/src/modules/meme/meme.service.ts @@ -3,17 +3,20 @@ import * as path from 'path'; import * as fs from 'fs/promises'; import { PathService } from '../../core/path/path.service'; import { OpenListService } from '../../core/openlist/openlist.service'; +import { AppConfigService } from '../../config/config.service'; @Injectable() export class MemeService { private readonly logger = new Logger(MemeService.name); - private readonly updateMs = 15 * 60 * 1000; // 15min + private readonly updateMs = 1 * 60 * 1000; // 15min constructor( @Inject(PathService) private readonly pathService: PathService, @Inject(OpenListService) private readonly openListService: OpenListService, + @Inject(AppConfigService) + private readonly configService: AppConfigService, ) { this.startAutoUpdate(); } @@ -21,22 +24,29 @@ export class MemeService { private startAutoUpdate() { setInterval(async () => { const memePath = path.join(this.pathService.get('meme')); - this.logger.log('定时检查表情仓库更新..'); - 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, - localFiles, - remoteFileList, - ); - } else { - this.logger.error('获取远程表情仓库文件失败..'); + const remoteMemePath = this.configService.get('OPENLIST_API_MEME_PATH'); + if (remoteMemePath) { + this.logger.log('定时检查表情仓库更新..'); + try { + const remoteFiles = + await this.openListService.listFiles(remoteMemePath); + this.logger.debug(remoteFiles); + if (remoteFiles.code === 200 && remoteFiles.data.content) { + const remoteFileList = remoteFiles.data.content; + const localFiles = await this.getLocalFileList(memePath); + await this.compareAndDownloadFiles( + memePath, + localFiles, + remoteFileList, + ); + } else { + this.logger.error('获取远程表情仓库文件失败..'); + } + } catch (error) { + this.logger.error('定时检查表情仓库更新失败..', error); } - } catch (error) { - this.logger.error('定时检查表情仓库更新失败..', error); + } else { + this.logger.warn('未配置远程表情包地址..'); } }, this.updateMs); }