ZZY 73917d6001 dev feat(markcard, static): 实现书签卡片功能和静态资源获取
- 书签卡片相关的组件和页面
- 实现书签数据的获取、添加、编辑和删除功能
- 添加用户验证和权限控制
- 优化页面布局和样式
2024-12-22 15:21:07 +08:00

53 lines
1.2 KiB
TypeScript

import fs from 'fs';
import path from 'path';
export default function resolveFilePath(
urlPath: string[] | undefined,
basePath: string,
missingExtname: string[] = [],
missingFilename: string[] = []
): string | null {
// If urlPath is undefined, set it to an empty array
const segments = urlPath || []
// Construct the base file path
let filePath: string = path.join(basePath, ...segments)
let find:boolean = false
do {
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
break
}
for (const extend of missingExtname) {
const newPath = filePath + extend;
if (fs.existsSync(newPath) && fs.statSync(newPath).isFile()) {
filePath = newPath
find = true
break
}
}
if (find) break
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
for (const extend of missingFilename) {
const newPath = path.join(filePath, extend);
if (fs.existsSync(newPath) && fs.statSync(newPath).isFile()) {
filePath = newPath
find = true
break
}
}
}
if (find) break
return null
} while(0)
if (!filePath.startsWith(basePath)) {
return null
}
return filePath
}