From b9d57255efd6e411a7836363aab49f519579220c Mon Sep 17 00:00:00 2001 From: Jerryplusy Date: Wed, 17 Sep 2025 18:28:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:meme=E6=9C=8D=E5=8A=A1=E6=9C=80=E7=BB=88?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/openlist/openlist.utils.ts | 46 +++++++++++++++++++---------- src/modules/meme/meme.service.ts | 29 ++++++++++-------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/core/openlist/openlist.utils.ts b/src/core/openlist/openlist.utils.ts index bbc2739..414b6f3 100644 --- a/src/core/openlist/openlist.utils.ts +++ b/src/core/openlist/openlist.utils.ts @@ -2,7 +2,9 @@ import axios from 'axios'; import { AppConfigService } from '../../config/config.service'; import { Inject, Logger } from '@nestjs/common'; import * as fs from 'fs'; +import * as fsp from 'fs/promises'; import { FileInfo, FileUpload, FsList } from './openlist.types'; +import * as path from 'node:path'; export class OpenListUtils { private static readonly logger = new Logger(OpenListUtils.name); @@ -78,12 +80,22 @@ export class OpenListUtils { * @param filePath 文件路径 */ static async getFileInfo(token: string, filePath: string): Promise { - const url = `${this.apiBaseUrl}/fs/info`; + const url = `${this.apiBaseUrl}/api/fs/get`; try { - const response = await axios.get(url, { - params: { path: filePath }, - headers: { Authorization: `${token}` }, + let data = JSON.stringify({ + path: filePath, }); + + let config = { + method: 'post', + url: `${url}`, + headers: { + 'Content-Type': 'application/json', + Authorization: `${token}`, + }, + data: data, + }; + const response = await axios(config); this.logger.log('获取文件信息成功..'); return response.data; } catch (error) { @@ -105,23 +117,27 @@ export class OpenListUtils { ): Promise { try { const fileInfo = await this.getFileInfo(token, filePath); + //this.logger.debug(fileInfo); const rawUrl = fileInfo.data.raw_url; - this.logger.debug(`rawUrl: ${rawUrl}`); + //this.logger.debug(`rawUrl: ${rawUrl}`); if (!rawUrl) { this.logger.error('文件没有找到 raw_url 地址..'); throw new Error('文件没有找到 raw_url 地址..'); } - const response = await axios.get(rawUrl, { - responseType: 'stream', - }); + const dir = path.dirname(downloadPath); + await fsp.mkdir(dir, { recursive: true }); + const response = await axios.get(rawUrl, { responseType: 'stream' }); const writer = fs.createWriteStream(downloadPath); response.data.pipe(writer); - writer.on('finish', () => { - this.logger.log(`文件下载成功: ${downloadPath}`); - }); - writer.on('error', (error) => { - this.logger.error('下载文件失败', error); - throw new Error('下载文件失败..'); + await new Promise((resolve, reject) => { + writer.on('finish', () => { + this.logger.log(`文件下载成功: ${downloadPath}`); + resolve(); + }); + writer.on('error', (error) => { + this.logger.error('下载文件失败', error); + reject(new Error('下载文件失败..')); + }); }); } catch (error) { this.logger.error('下载文件失败..', error); @@ -144,7 +160,7 @@ export class OpenListUtils { ): Promise { const url = `${this.apiBaseUrl}/api/fs/put`; const headers = { - Authorization: `Bearer ${token}`, + Authorization: `${token}`, 'Content-Type': 'application/octet-stream', 'Content-Length': file.bytesRead, 'File-Path': encodeURIComponent(filePathOnServer), diff --git a/src/modules/meme/meme.service.ts b/src/modules/meme/meme.service.ts index c6527a1..364b244 100644 --- a/src/modules/meme/meme.service.ts +++ b/src/modules/meme/meme.service.ts @@ -8,7 +8,7 @@ import { AppConfigService } from '../../config/config.service'; @Injectable() export class MemeService { private readonly logger = new Logger(MemeService.name); - private readonly updateMs = 150 * 60 * 1000; // 15min + private readonly updateMs = 15 * 60 * 1000; // 15min constructor( @Inject(PathService) @@ -96,19 +96,22 @@ export class MemeService { ) { for (const remoteFile of remoteFiles) { let relativePath = path.relative(remoteMemePath, remoteFile.path); - relativePath = relativePath.replace(/D:\\alist\\crystelf\\meme/g, ''); + //this.logger.debug(`relativePath: ${relativePath}`); + let remoteRelativePath = relativePath.replace(/D:\\alist/g, ''); //服务器下载用目录 + relativePath = relativePath.replace(/D:\\alist\\crystelf\\meme/g, ''); //本地储存用 + //this.logger.debug(`relativeEdPath: ${relativePath}`); const localFilePath = path.join( localPath, relativePath.replace(/\\/g, '/'), ); if (remoteFile.is_dir) { try { - const localDirPath = path.dirname(localFilePath); - await fs.mkdir(localDirPath, { recursive: true }); - this.logger.log(`文件夹已创建: ${localDirPath}`); - const subRemoteFiles = await this.openListService.listFiles( - remoteFile.path, - ); + //const localDirPath = path.dirname(localFilePath); + //await fs.mkdir(localDirPath, { recursive: true }); + //this.logger.log(`文件夹已创建: ${localDirPath}`); + //相关逻辑已在oplist工具中处理 + const subRemoteFiles = + await this.openListService.listFiles(remoteRelativePath); if (subRemoteFiles.code === 200 && subRemoteFiles.data.content) { await this.compareAndDownloadFiles( localPath, @@ -118,19 +121,19 @@ export class MemeService { ); } } catch (error) { - this.logger.error(`创建文件夹失败: ${remoteFile.path}`, error); + this.logger.error(`递归处理文件夹失败: ${localFilePath}`, error); } } else { if (!localFiles.includes(localFilePath)) { - this.logger.log(`文件缺失: ${remoteFile.path}, 开始下载..`); + this.logger.log(`文件缺失: ${localFilePath}, 开始下载..`); try { await this.openListService.downloadFile( - remoteFile.path, + remoteRelativePath, localFilePath, ); - this.logger.log(`文件下载成功: ${remoteFile.path}`); + this.logger.log(`文件下载成功: ${localFilePath}`); } catch (error) { - this.logger.error(`下载文件失败: ${remoteFile.path}`, error); + this.logger.error(`下载文件失败: ${localFilePath}`, error); } } }