mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-12-05 10:31:56 +00:00
fix:linux上路径问题
This commit is contained in:
parent
8d62d0a979
commit
2bb0bcf5ea
0
nest-cli.json
Normal file → Executable file
0
nest-cli.json
Normal file → Executable file
0
package.json
Normal file → Executable file
0
package.json
Normal file → Executable file
0
pnpm-lock.yaml
generated
Normal file → Executable file
0
pnpm-lock.yaml
generated
Normal file → Executable file
@ -53,78 +53,96 @@ export class FilesService {
|
|||||||
remoteApiPath: string,
|
remoteApiPath: string,
|
||||||
replacPath: string,
|
replacPath: string,
|
||||||
) {
|
) {
|
||||||
const remoteBasePath = this.configService.get(`OPENLIST_API_BASE_PATH`);
|
const remoteBasePath = this.configService.get(`OPENLIST_API_BASE_PATH`) as
|
||||||
|
| string
|
||||||
|
| undefined;
|
||||||
|
|
||||||
const normalizedLocalFiles = localFiles.map((f) =>
|
const normalizedLocalFiles = localFiles.map((f) =>
|
||||||
path.normalize(f).replace(/\\/g, '/'),
|
path.normalize(f).replace(/\\/g, '/'),
|
||||||
);
|
);
|
||||||
for (const remoteFile of remoteFiles) {
|
|
||||||
let relativePath = path.relative(remoteApiPath, remoteFile.path);
|
|
||||||
//this.logger.debug(`relativePath: ${relativePath}`);
|
|
||||||
//this.logger.debug(remoteBasePath);
|
|
||||||
if (remoteBasePath) {
|
|
||||||
let remoteRelativePath = relativePath.replace(remoteBasePath, ''); //服务器下载用目录
|
|
||||||
//this.logger.debug(`remoteRelativePath: ${remoteRelativePath}`); //√\
|
|
||||||
remoteRelativePath = path
|
|
||||||
.normalize(remoteRelativePath)
|
|
||||||
.replace(/\\/g, '/');
|
|
||||||
replacPath = path.normalize(replacPath).replace(/\\/g, '/');
|
|
||||||
relativePath = remoteRelativePath.replace(replacPath, ''); //本地储存用
|
|
||||||
this.logger.debug(`replacPath: ${relativePath}`);
|
|
||||||
relativePath = path.normalize(relativePath).replace(/\\/g, '/');
|
|
||||||
this.logger.debug(`relativePathEd: ${relativePath}`);
|
|
||||||
const localFilePath = path
|
|
||||||
.normalize(path.join(localPath, relativePath))
|
|
||||||
.replace(/\\/g, '/');
|
|
||||||
//this.logger.debug(`localFilePath: ${localFilePath}`);
|
|
||||||
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);
|
|
||||||
if (subRemoteFiles.code === 200 && subRemoteFiles.data.content) {
|
|
||||||
await this.compareAndDownloadFiles(
|
|
||||||
localPath,
|
|
||||||
normalizedLocalFiles,
|
|
||||||
subRemoteFiles.data.content,
|
|
||||||
remoteApiPath,
|
|
||||||
replacPath,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error(`递归处理文件夹失败: ${localFilePath}`, error);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const normalizedLocalFiles = localFiles.map((f) =>
|
|
||||||
path.normalize(f).replace(/\\/g, '/'),
|
|
||||||
);
|
|
||||||
//this.logger.debug(
|
|
||||||
//`normalizedLocalFiles: ${JSON.stringify(normalizedLocalFiles)}`,
|
|
||||||
//);
|
|
||||||
|
|
||||||
if (!normalizedLocalFiles.includes(localFilePath)) {
|
const remoteApiNorm = (remoteApiPath || '')
|
||||||
this.logger.log(`文件缺失: ${localFilePath}, 开始下载..`);
|
.replace(/\\/g, '/')
|
||||||
try {
|
.replace(/\/+$/, '');
|
||||||
await this.openListService.downloadFile(
|
const remoteBaseNorm = (remoteBasePath || '')
|
||||||
remoteRelativePath,
|
.replace(/\\/g, '/')
|
||||||
localFilePath,
|
.replace(/\/+$/, '');
|
||||||
);
|
let replacPathNorm = (replacPath || '').replace(/\\/g, '/');
|
||||||
this.logger.log(`文件下载成功: ${localFilePath}`);
|
if (replacPathNorm && !replacPathNorm.startsWith('/'))
|
||||||
normalizedLocalFiles.push(localFilePath);
|
replacPathNorm = '/' + replacPathNorm;
|
||||||
this.logger.debug(
|
replacPathNorm = replacPathNorm.replace(/\/+$/, '');
|
||||||
`localFilePath: ${JSON.stringify(normalizedLocalFiles)}`,
|
|
||||||
);
|
for (const remoteFile of remoteFiles) {
|
||||||
} catch (error) {
|
const rawRemotePath = String(remoteFile.path || '').replace(/\\/g, '/');
|
||||||
this.logger.error(`下载文件失败: ${localFilePath}`, error);
|
|
||||||
}
|
let remoteRelativePath = '';
|
||||||
} else {
|
|
||||||
this.logger.log('本地文件已是最新..');
|
if (remoteBaseNorm && rawRemotePath.startsWith(remoteBaseNorm)) {
|
||||||
|
remoteRelativePath = rawRemotePath.slice(remoteBaseNorm.length);
|
||||||
|
} else if (remoteApiNorm && rawRemotePath.includes(remoteApiNorm)) {
|
||||||
|
remoteRelativePath = rawRemotePath.slice(
|
||||||
|
rawRemotePath.indexOf(remoteApiNorm),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const rel = path.posix.relative(remoteApiNorm || '/', rawRemotePath);
|
||||||
|
remoteRelativePath = rel ? '/' + rel.replace(/\/+/g, '/') : '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteRelativePath = remoteRelativePath.replace(/\/+/g, '/');
|
||||||
|
if (!remoteRelativePath.startsWith('/'))
|
||||||
|
remoteRelativePath = '/' + remoteRelativePath;
|
||||||
|
|
||||||
|
let localRelative = remoteRelativePath;
|
||||||
|
if (replacPathNorm && localRelative.startsWith(replacPathNorm)) {
|
||||||
|
localRelative = localRelative.slice(replacPathNorm.length);
|
||||||
|
} else if (replacPathNorm && localRelative.includes(replacPathNorm)) {
|
||||||
|
localRelative = localRelative.replace(replacPathNorm, '');
|
||||||
|
}
|
||||||
|
localRelative = localRelative.replace(/\/+/g, '/').replace(/^\/+/, '');
|
||||||
|
const localFilePathRaw = path.join(localPath, localRelative);
|
||||||
|
const localFilePath = path.normalize(localFilePathRaw);
|
||||||
|
const localFilePathForCompare = localFilePath.replace(/\\/g, '/');
|
||||||
|
|
||||||
|
this.logger.debug(`replacPath: ${replacPath}`);
|
||||||
|
this.logger.debug(`remoteBaseNorm: ${remoteBaseNorm}`);
|
||||||
|
this.logger.debug(`rawRemotePath: ${rawRemotePath}`);
|
||||||
|
this.logger.debug(`remoteRelativePath: ${remoteRelativePath}`);
|
||||||
|
this.logger.debug(`localRelative: ${localRelative}`);
|
||||||
|
this.logger.debug(`localFilePath: ${localFilePathForCompare}`);
|
||||||
|
|
||||||
|
if (remoteFile.is_dir) {
|
||||||
|
try {
|
||||||
|
const subRemote =
|
||||||
|
await this.openListService.listFiles(remoteRelativePath);
|
||||||
|
if (subRemote.code === 200 && subRemote.data?.content) {
|
||||||
|
await this.compareAndDownloadFiles(
|
||||||
|
localPath,
|
||||||
|
normalizedLocalFiles,
|
||||||
|
subRemote.data.content,
|
||||||
|
remoteApiPath,
|
||||||
|
replacPath,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`递归处理文件夹失败: ${localFilePath}`, error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.logger.error(`未配置远程根路径..`);
|
if (!normalizedLocalFiles.includes(localFilePathForCompare)) {
|
||||||
|
this.logger.log(`文件缺失: ${localFilePath}, 开始下载..`);
|
||||||
|
try {
|
||||||
|
await fs.mkdir(path.dirname(localFilePath), { recursive: true });
|
||||||
|
await this.openListService.downloadFile(
|
||||||
|
remoteRelativePath,
|
||||||
|
localFilePath,
|
||||||
|
);
|
||||||
|
this.logger.log(`文件下载成功: ${localFilePath}`);
|
||||||
|
normalizedLocalFiles.push(localFilePathForCompare);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`下载文件失败: ${localFilePath}`, error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.logger.log('本地文件已是最新..');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,7 +33,6 @@ async function bootstrap() {
|
|||||||
{ path: 'public/(.*)', method: RequestMethod.ALL },
|
{ path: 'public/(.*)', method: RequestMethod.ALL },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
app.useGlobalInterceptors(new ResponseInterceptor());
|
app.useGlobalInterceptors(new ResponseInterceptor());
|
||||||
app.useGlobalFilters(new AllExceptionsFilter());
|
app.useGlobalFilters(new AllExceptionsFilter());
|
||||||
const systemService = app.get(SystemService);
|
const systemService = app.get(SystemService);
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import { FilesService } from '../../core/files/files.service';
|
|||||||
export class CdnService {
|
export class CdnService {
|
||||||
private readonly logger = new Logger(CdnService.name);
|
private readonly logger = new Logger(CdnService.name);
|
||||||
private filePath: string;
|
private filePath: string;
|
||||||
private readonly updateMs = 15 * 60 * 1000; // 15min
|
private readonly updateMs = 2 * 60 * 100; // 15min
|
||||||
@Inject(PathService)
|
@Inject(PathService)
|
||||||
private readonly paths: PathService;
|
private readonly paths: PathService;
|
||||||
constructor(
|
constructor(
|
||||||
@ -24,7 +24,7 @@ export class CdnService {
|
|||||||
private readonly filesService: FilesService,
|
private readonly filesService: FilesService,
|
||||||
) {
|
) {
|
||||||
this.startAutoUpdate();
|
this.startAutoUpdate();
|
||||||
this.logger.log(`晶灵云图数据中心初始化.. 数据存储在: ${this.filePath}`);
|
this.logger.log(`晶灵云数据中心初始化.. 数据存储在: ${this.filePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private startAutoUpdate() {
|
private startAutoUpdate() {
|
||||||
@ -55,7 +55,7 @@ export class CdnService {
|
|||||||
this.logger.error(`晶灵cdn检查更新失败: ${error}`);
|
this.logger.error(`晶灵cdn检查更新失败: ${error}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.logger.warn('未配置远程表情包地址..');
|
this.logger.warn('未配置远程cdn地址..');
|
||||||
}
|
}
|
||||||
}, this.updateMs);
|
}, this.updateMs);
|
||||||
}
|
}
|
||||||
|
|||||||
0
tsconfig.build.json
Normal file → Executable file
0
tsconfig.build.json
Normal file → Executable file
0
tsconfig.json
Normal file → Executable file
0
tsconfig.json
Normal file → Executable file
Loading…
x
Reference in New Issue
Block a user