handle relative path correctly (#226)

Co-authored-by: fyears <1142836+fyears@users.noreply.github.com>
This commit is contained in:
Yesterday17 2024-04-05 10:45:23 +08:00 committed by GitHub
parent 220fd07a8b
commit 8f68ac4ded
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 4 deletions

View File

@ -173,6 +173,52 @@ const getWebdavPath = (fileOrFolderPath: string, remoteBaseDir: string) => {
return key; return key;
}; };
function extractPrefix(path: string) {
let startsWithSlash = false;
if (path.startsWith("/")) {
startsWithSlash = true;
path = path.slice(1);
}
let endsWithSlash = false;
if (path.endsWith("/")) {
endsWithSlash = true;
path = path.slice(0, -1);
}
const segments = path.split("/");
const prefix: string[] = [];
let pathString = "/";
let level = 0;
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
if (segment === ".") {
continue;
} else if (segment === "..") {
level--;
prefix.push(segment);
} else if (level < 0) {
level++;
prefix.push(segment);
} else {
const pathSegments = segments.slice(i);
pathString += pathSegments.join("/");
if (endsWithSlash && pathSegments.length > 0) {
pathString += "/";
}
break;
}
}
const prefixString = (startsWithSlash ? "/" : "") + prefix.join("/");
return {
path: pathString,
prefix: prefixString,
};
}
const getNormPath = (fileOrFolderPath: string, remoteBaseDir: string) => { const getNormPath = (fileOrFolderPath: string, remoteBaseDir: string) => {
if ( if (
!( !(
@ -184,14 +230,13 @@ const getNormPath = (fileOrFolderPath: string, remoteBaseDir: string) => {
`"${fileOrFolderPath}" doesn't starts with "/${remoteBaseDir}/"` `"${fileOrFolderPath}" doesn't starts with "/${remoteBaseDir}/"`
); );
} }
// if (fileOrFolderPath.startsWith("/")) {
// return fileOrFolderPath.slice(1);
// }
return fileOrFolderPath.slice(`/${remoteBaseDir}/`.length); return fileOrFolderPath.slice(`/${remoteBaseDir}/`.length);
}; };
const fromWebdavItemToEntity = (x: FileStat, remoteBaseDir: string) => { const fromWebdavItemToEntity = (x: FileStat, remoteBaseDir: string) => {
let key = getNormPath(x.filename, remoteBaseDir); const { shortPath, prefix } = extractPrefix(x.filename);
let key = getNormPath(shortPath, remoteBaseDir);
if (x.type === "directory" && !key.endsWith("/")) { if (x.type === "directory" && !key.endsWith("/")) {
key = `${key}/`; key = `${key}/`;
} }

View File

@ -147,6 +147,7 @@ const isSkipItemByName = (
return ( return (
isHiddenPath(key, true, false) || isHiddenPath(key, true, false) ||
(!syncUnderscoreItems && isHiddenPath(key, false, true)) || (!syncUnderscoreItems && isHiddenPath(key, false, true)) ||
key === "/" ||
key === DEFAULT_FILE_NAME_FOR_METADATAONREMOTE || key === DEFAULT_FILE_NAME_FOR_METADATAONREMOTE ||
key === DEFAULT_FILE_NAME_FOR_METADATAONREMOTE2 key === DEFAULT_FILE_NAME_FOR_METADATAONREMOTE2
); );