Первая версия
This commit is contained in:
25
src/core/PathUtils.js
Normal file
25
src/core/PathUtils.js
Normal file
@@ -0,0 +1,25 @@
|
||||
export class PathUtils {
|
||||
static normalizeRelative(path) {
|
||||
if (!path || path.startsWith("/") || path.includes("\\")) {
|
||||
throw new Error("Invalid path format");
|
||||
}
|
||||
const parts = path.split("/");
|
||||
const out = [];
|
||||
for (const part of parts) {
|
||||
if (!part || part === ".") continue;
|
||||
if (part === "..") throw new Error("Path traversal is forbidden");
|
||||
out.push(part);
|
||||
}
|
||||
return out.join("/");
|
||||
}
|
||||
|
||||
static dirname(path) {
|
||||
const index = path.lastIndexOf("/");
|
||||
return index === -1 ? "" : path.slice(0, index);
|
||||
}
|
||||
|
||||
static basename(path) {
|
||||
const index = path.lastIndexOf("/");
|
||||
return index === -1 ? path : path.slice(index + 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user