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 { 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);
|
||||||
@ -78,17 +80,27 @@ 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}/fs/info`;
|
const url = `${this.apiBaseUrl}/api/fs/get`;
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(url, {
|
let data = JSON.stringify({
|
||||||
params: { path: filePath },
|
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('获取文件信息失败..', error);
|
this.logger.error(`获取文件信息失败: ${filePath}`, error);
|
||||||
throw new Error('获取文件信息失败..');
|
throw new Error(`获取文件信息失败 ${filePath}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,27 +117,31 @@ 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 response = await axios.get(rawUrl, {
|
const dir = path.dirname(downloadPath);
|
||||||
responseType: 'stream',
|
await fsp.mkdir(dir, { recursive: true });
|
||||||
});
|
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('下载文件失败', error);
|
this.logger.error(`下载文件失败: ${filePath}`, error);
|
||||||
throw new Error('下载文件失败..');
|
reject(new Error(`下载文件失败: ${filePath}`));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error('下载文件失败..', error);
|
this.logger.error(`下载文件失败: ${filePath}`, error);
|
||||||
throw new Error('下载文件失败..');
|
throw new Error(`下载文件失败: ${filePath}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +160,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: `Bearer ${token}`,
|
Authorization: `${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),
|
||||||
|
@ -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 = 150 * 60 * 1000; // 15min
|
private readonly updateMs = 15 * 60 * 1000; // 15min
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(PathService)
|
@Inject(PathService)
|
||||||
@ -96,19 +96,22 @@ 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);
|
||||||
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(
|
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}`);
|
||||||
const subRemoteFiles = await this.openListService.listFiles(
|
//相关逻辑已在oplist工具中处理
|
||||||
remoteFile.path,
|
const subRemoteFiles =
|
||||||
);
|
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,
|
||||||
@ -118,19 +121,19 @@ export class MemeService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(`创建文件夹失败: ${remoteFile.path}`, error);
|
this.logger.error(`递归处理文件夹失败: ${localFilePath}`, error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!localFiles.includes(localFilePath)) {
|
if (!localFiles.includes(localFilePath)) {
|
||||||
this.logger.log(`文件缺失: ${remoteFile.path}, 开始下载..`);
|
this.logger.log(`文件缺失: ${localFilePath}, 开始下载..`);
|
||||||
try {
|
try {
|
||||||
await this.openListService.downloadFile(
|
await this.openListService.downloadFile(
|
||||||
remoteFile.path,
|
remoteRelativePath,
|
||||||
localFilePath,
|
localFilePath,
|
||||||
);
|
);
|
||||||
this.logger.log(`文件下载成功: ${remoteFile.path}`);
|
this.logger.log(`文件下载成功: ${localFilePath}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(`下载文件失败: ${remoteFile.path}`, error);
|
this.logger.error(`下载文件失败: ${localFilePath}`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user