Compare commits

..

No commits in common. "4fb3e00df516ca6e375c3c09755b136238fefd1a" and "5c062172f317e12513d9573a7239c6c3e4a85e20" have entirely different histories.

2 changed files with 33 additions and 52 deletions

View File

@ -2,9 +2,7 @@ import axios from 'axios';
import { AppConfigService } from '../../config/config.service'; import { AppConfigService } from '../../config/config.service';
import { Inject, Logger } from '@nestjs/common'; import { Inject, Logger } from '@nestjs/common';
import * as fs from 'fs'; import * as fs from 'fs';
import * as fsp from 'fs/promises';
import { FileInfo, FileUpload, FsList } from './openlist.types'; import { FileInfo, FileUpload, FsList } from './openlist.types';
import * as path from 'node:path';
export class OpenListUtils { export class OpenListUtils {
private static readonly logger = new Logger(OpenListUtils.name); private static readonly logger = new Logger(OpenListUtils.name);
@ -80,27 +78,17 @@ export class OpenListUtils {
* @param filePath * @param filePath
*/ */
static async getFileInfo(token: string, filePath: string): Promise<FileInfo> { static async getFileInfo(token: string, filePath: string): Promise<FileInfo> {
const url = `${this.apiBaseUrl}/api/fs/get`; const url = `${this.apiBaseUrl}/fs/info`;
try { try {
let data = JSON.stringify({ const response = await axios.get(url, {
path: filePath, params: { path: filePath },
headers: { Authorization: `${token}` },
}); });
this.logger.log('获取文件信息成功..');
let config = {
method: 'post',
url: `${url}`,
headers: {
'Content-Type': 'application/json',
Authorization: `${token}`,
},
data: data,
};
const response = await axios(config);
this.logger.log(`获取文件信息成功: ${filePath}`);
return response.data; return response.data;
} catch (error) { } catch (error) {
this.logger.error(`获取文件信息失败: ${filePath}`, error); this.logger.error('获取文件信息失败..', error);
throw new Error(`获取文件信息失败 ${filePath}`); throw new Error('获取文件信息失败..');
} }
} }
@ -117,31 +105,27 @@ export class OpenListUtils {
): Promise<void> { ): Promise<void> {
try { try {
const fileInfo = await this.getFileInfo(token, filePath); const fileInfo = await this.getFileInfo(token, filePath);
//this.logger.debug(fileInfo);
const rawUrl = fileInfo.data.raw_url; const rawUrl = fileInfo.data.raw_url;
//this.logger.debug(`rawUrl: ${rawUrl}`); this.logger.debug(`rawUrl: ${rawUrl}`);
if (!rawUrl) { if (!rawUrl) {
this.logger.error('文件没有找到 raw_url 地址..'); this.logger.error('文件没有找到 raw_url 地址..');
throw new Error('文件没有找到 raw_url 地址..'); throw new Error('文件没有找到 raw_url 地址..');
} }
const dir = path.dirname(downloadPath); const response = await axios.get(rawUrl, {
await fsp.mkdir(dir, { recursive: true }); responseType: 'stream',
const response = await axios.get(rawUrl, { responseType: 'stream' }); });
const writer = fs.createWriteStream(downloadPath); const writer = fs.createWriteStream(downloadPath);
response.data.pipe(writer); response.data.pipe(writer);
await new Promise<void>((resolve, reject) => {
writer.on('finish', () => { writer.on('finish', () => {
this.logger.log(`文件下载成功: ${downloadPath}`); this.logger.log(`文件下载成功: ${downloadPath}`);
resolve();
}); });
writer.on('error', (error) => { writer.on('error', (error) => {
this.logger.error(`下载文件失败: ${filePath}`, error); this.logger.error('下载文件失败', error);
reject(new Error(`下载文件失败: ${filePath}`)); throw new Error('下载文件失败..');
});
}); });
} catch (error) { } catch (error) {
this.logger.error(`下载文件失败: ${filePath}`, error); this.logger.error('下载文件失败..', error);
throw new Error(`下载文件失败: ${filePath}`); throw new Error('下载文件失败..');
} }
} }
@ -160,7 +144,7 @@ export class OpenListUtils {
): Promise<FileUpload> { ): Promise<FileUpload> {
const url = `${this.apiBaseUrl}/api/fs/put`; const url = `${this.apiBaseUrl}/api/fs/put`;
const headers = { const headers = {
Authorization: `${token}`, Authorization: `Bearer ${token}`,
'Content-Type': 'application/octet-stream', 'Content-Type': 'application/octet-stream',
'Content-Length': file.bytesRead, 'Content-Length': file.bytesRead,
'File-Path': encodeURIComponent(filePathOnServer), 'File-Path': encodeURIComponent(filePathOnServer),

View File

@ -8,7 +8,7 @@ import { AppConfigService } from '../../config/config.service';
@Injectable() @Injectable()
export class MemeService { export class MemeService {
private readonly logger = new Logger(MemeService.name); private readonly logger = new Logger(MemeService.name);
private readonly updateMs = 15 * 60 * 1000; // 15min private readonly updateMs = 150 * 60 * 1000; // 15min
constructor( constructor(
@Inject(PathService) @Inject(PathService)
@ -96,22 +96,19 @@ export class MemeService {
) { ) {
for (const remoteFile of remoteFiles) { for (const remoteFile of remoteFiles) {
let relativePath = path.relative(remoteMemePath, remoteFile.path); let relativePath = path.relative(remoteMemePath, remoteFile.path);
//this.logger.debug(`relativePath: ${relativePath}`); relativePath = relativePath.replace(/D:\\alist\\crystelf\\meme/g, '');
let remoteRelativePath = relativePath.replace(/D:\\alist/g, ''); //服务器下载用目录
relativePath = relativePath.replace(/D:\\alist\\crystelf\\meme/g, ''); //本地储存用
//this.logger.debug(`relativeEdPath: ${relativePath}`);
const localFilePath = path.join( const localFilePath = path.join(
localPath, localPath,
relativePath.replace(/\\/g, '/'), relativePath.replace(/\\/g, '/'),
); );
if (remoteFile.is_dir) { if (remoteFile.is_dir) {
try { try {
//const localDirPath = path.dirname(localFilePath); const localDirPath = path.dirname(localFilePath);
//await fs.mkdir(localDirPath, { recursive: true }); await fs.mkdir(localDirPath, { recursive: true });
//this.logger.log(`文件夹已创建: ${localDirPath}`); this.logger.log(`文件夹已创建: ${localDirPath}`);
//相关逻辑已在oplist工具中处理 const subRemoteFiles = await this.openListService.listFiles(
const subRemoteFiles = remoteFile.path,
await this.openListService.listFiles(remoteRelativePath); );
if (subRemoteFiles.code === 200 && subRemoteFiles.data.content) { if (subRemoteFiles.code === 200 && subRemoteFiles.data.content) {
await this.compareAndDownloadFiles( await this.compareAndDownloadFiles(
localPath, localPath,
@ -121,19 +118,19 @@ export class MemeService {
); );
} }
} catch (error) { } catch (error) {
this.logger.error(`递归处理文件夹失败: ${localFilePath}`, error); this.logger.error(`创建文件夹失败: ${remoteFile.path}`, error);
} }
} else { } else {
if (!localFiles.includes(localFilePath)) { if (!localFiles.includes(localFilePath)) {
this.logger.log(`文件缺失: ${localFilePath}, 开始下载..`); this.logger.log(`文件缺失: ${remoteFile.path}, 开始下载..`);
try { try {
await this.openListService.downloadFile( await this.openListService.downloadFile(
remoteRelativePath, remoteFile.path,
localFilePath, localFilePath,
); );
this.logger.log(`文件下载成功: ${localFilePath}`); this.logger.log(`文件下载成功: ${remoteFile.path}`);
} catch (error) { } catch (error) {
this.logger.error(`下载文件失败: ${localFilePath}`, error); this.logger.error(`下载文件失败: ${remoteFile.path}`, error);
} }
} }
} }