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 { 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);
@ -80,27 +78,17 @@ export class OpenListUtils {
* @param filePath
*/
static async getFileInfo(token: string, filePath: string): Promise<FileInfo> {
const url = `${this.apiBaseUrl}/api/fs/get`;
const url = `${this.apiBaseUrl}/fs/info`;
try {
let data = JSON.stringify({
path: filePath,
const response = await axios.get(url, {
params: { path: filePath },
headers: { Authorization: `${token}` },
});
let config = {
method: 'post',
url: `${url}`,
headers: {
'Content-Type': 'application/json',
Authorization: `${token}`,
},
data: data,
};
const response = await axios(config);
this.logger.log(`获取文件信息成功: ${filePath}`);
this.logger.log('获取文件信息成功..');
return response.data;
} catch (error) {
this.logger.error(`获取文件信息失败: ${filePath}`, error);
throw new Error(`获取文件信息失败 ${filePath}`);
this.logger.error('获取文件信息失败..', error);
throw new Error('获取文件信息失败..');
}
}
@ -117,31 +105,27 @@ export class OpenListUtils {
): Promise<void> {
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 dir = path.dirname(downloadPath);
await fsp.mkdir(dir, { recursive: true });
const response = await axios.get(rawUrl, { responseType: 'stream' });
const response = await axios.get(rawUrl, {
responseType: 'stream',
});
const writer = fs.createWriteStream(downloadPath);
response.data.pipe(writer);
await new Promise<void>((resolve, reject) => {
writer.on('finish', () => {
this.logger.log(`文件下载成功: ${downloadPath}`);
resolve();
});
writer.on('error', (error) => {
this.logger.error(`下载文件失败: ${filePath}`, error);
reject(new Error(`下载文件失败: ${filePath}`));
});
this.logger.error('下载文件失败', error);
throw new Error('下载文件失败..');
});
} catch (error) {
this.logger.error(`下载文件失败: ${filePath}`, error);
throw new Error(`下载文件失败: ${filePath}`);
this.logger.error('下载文件失败..', error);
throw new Error('下载文件失败..');
}
}
@ -160,7 +144,7 @@ export class OpenListUtils {
): Promise<FileUpload> {
const url = `${this.apiBaseUrl}/api/fs/put`;
const headers = {
Authorization: `${token}`,
Authorization: `Bearer ${token}`,
'Content-Type': 'application/octet-stream',
'Content-Length': file.bytesRead,
'File-Path': encodeURIComponent(filePathOnServer),

View File

@ -8,7 +8,7 @@ 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 = 150 * 60 * 1000; // 15min
constructor(
@Inject(PathService)
@ -96,22 +96,19 @@ export class MemeService {
) {
for (const remoteFile of remoteFiles) {
let relativePath = path.relative(remoteMemePath, remoteFile.path);
//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}`);
relativePath = relativePath.replace(/D:\\alist\\crystelf\\meme/g, '');
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}`);
//相关逻辑已在oplist工具中处理
const subRemoteFiles =
await this.openListService.listFiles(remoteRelativePath);
const localDirPath = path.dirname(localFilePath);
await fs.mkdir(localDirPath, { recursive: true });
this.logger.log(`文件夹已创建: ${localDirPath}`);
const subRemoteFiles = await this.openListService.listFiles(
remoteFile.path,
);
if (subRemoteFiles.code === 200 && subRemoteFiles.data.content) {
await this.compareAndDownloadFiles(
localPath,
@ -121,19 +118,19 @@ export class MemeService {
);
}
} catch (error) {
this.logger.error(`递归处理文件夹失败: ${localFilePath}`, error);
this.logger.error(`创建文件夹失败: ${remoteFile.path}`, error);
}
} else {
if (!localFiles.includes(localFilePath)) {
this.logger.log(`文件缺失: ${localFilePath}, 开始下载..`);
this.logger.log(`文件缺失: ${remoteFile.path}, 开始下载..`);
try {
await this.openListService.downloadFile(
remoteRelativePath,
remoteFile.path,
localFilePath,
);
this.logger.log(`文件下载成功: ${localFilePath}`);
this.logger.log(`文件下载成功: ${remoteFile.path}`);
} catch (error) {
this.logger.error(`下载文件失败: ${localFilePath}`, error);
this.logger.error(`下载文件失败: ${remoteFile.path}`, error);
}
}
}