mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-10-14 13:29:19 +00:00
Compare commits
2 Commits
5c062172f3
...
4fb3e00df5
Author | SHA1 | Date | |
---|---|---|---|
4fb3e00df5 | |||
b9d57255ef |
@ -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,17 +80,27 @@ export class OpenListUtils {
|
||||
* @param filePath 文件路径
|
||||
*/
|
||||
static async getFileInfo(token: string, filePath: string): Promise<FileInfo> {
|
||||
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,
|
||||
});
|
||||
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;
|
||||
} catch (error) {
|
||||
this.logger.error('获取文件信息失败..', error);
|
||||
throw new Error('获取文件信息失败..');
|
||||
this.logger.error(`获取文件信息失败: ${filePath}`, error);
|
||||
throw new Error(`获取文件信息失败 ${filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,27 +117,31 @@ 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 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);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
writer.on('finish', () => {
|
||||
this.logger.log(`文件下载成功: ${downloadPath}`);
|
||||
resolve();
|
||||
});
|
||||
writer.on('error', (error) => {
|
||||
this.logger.error('下载文件失败', error);
|
||||
throw new Error('下载文件失败..');
|
||||
this.logger.error(`下载文件失败: ${filePath}`, error);
|
||||
reject(new Error(`下载文件失败: ${filePath}`));
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error('下载文件失败..', error);
|
||||
throw new Error('下载文件失败..');
|
||||
this.logger.error(`下载文件失败: ${filePath}`, error);
|
||||
throw new Error(`下载文件失败: ${filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,7 +160,7 @@ export class OpenListUtils {
|
||||
): Promise<FileUpload> {
|
||||
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),
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user