Skip to content

Commit

Permalink
more robust for rclone
Browse files Browse the repository at this point in the history
  • Loading branch information
fyears committed Jun 28, 2024
1 parent f306445 commit a7cc4e2
Showing 1 changed file with 68 additions and 8 deletions.
76 changes: 68 additions & 8 deletions src/fsOnedrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ const fromDriveItemToEntity = (x: DriveItem, remoteBaseDir: string): Entity => {
// another possibile prefix
const FOURTH_COMMON_PREFIX_RAW = `/drive/items/`;

// when to use decode?
const remoteBaseDirEncoded = encodeURIComponent(remoteBaseDir);

if (
x.parentReference === undefined ||
x.parentReference === null ||
Expand All @@ -279,31 +282,77 @@ const fromDriveItemToEntity = (x: DriveItem, remoteBaseDir: string): Entity => {
SECOND_COMMON_PREFIX_REGEX
);
const matchThirdPrefixRes = fullPathOriginal.match(THIRD_COMMON_PREFIX_REGEX);

// first
if (
matchFirstPrefixRes !== null &&
fullPathOriginal.startsWith(`${matchFirstPrefixRes[0]}${remoteBaseDir}`)
) {
const foundPrefix = `${matchFirstPrefixRes[0]}${remoteBaseDir}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
} else if (
matchFirstPrefixRes !== null &&
fullPathOriginal.startsWith(
`${matchFirstPrefixRes[0]}${remoteBaseDirEncoded}`
)
) {
const foundPrefix = `${matchFirstPrefixRes[0]}${remoteBaseDirEncoded}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
}

// fifth
else if (
matchFifthPrefixRes !== null &&
fullPathOriginal.startsWith(`${matchFifthPrefixRes[0]}${remoteBaseDir}`)
) {
const foundPrefix = `${matchFifthPrefixRes[0]}${remoteBaseDir}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
} else if (
matchFifthPrefixRes !== null &&
fullPathOriginal.startsWith(
`${matchFifthPrefixRes[0]}${remoteBaseDirEncoded}`
)
) {
const foundPrefix = `${matchFifthPrefixRes[0]}${remoteBaseDirEncoded}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
}

// second
else if (
matchSecondPrefixRes !== null &&
fullPathOriginal.startsWith(`${matchSecondPrefixRes[0]}${remoteBaseDir}`)
) {
const foundPrefix = `${matchSecondPrefixRes[0]}${remoteBaseDir}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
} else if (
matchSecondPrefixRes !== null &&
fullPathOriginal.startsWith(
`${matchSecondPrefixRes[0]}${remoteBaseDirEncoded}`
)
) {
const foundPrefix = `${matchSecondPrefixRes[0]}${remoteBaseDirEncoded}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
}

// third
else if (
matchThirdPrefixRes !== null &&
fullPathOriginal.startsWith(`${matchThirdPrefixRes[0]}${remoteBaseDir}`)
) {
const foundPrefix = `${matchThirdPrefixRes[0]}${remoteBaseDir}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
} else if (x.parentReference.path.startsWith(FOURTH_COMMON_PREFIX_RAW)) {
} else if (
matchThirdPrefixRes !== null &&
fullPathOriginal.startsWith(
`${matchThirdPrefixRes[0]}${remoteBaseDirEncoded}`
)
) {
const foundPrefix = `${matchThirdPrefixRes[0]}${remoteBaseDirEncoded}`;
key = fullPathOriginal.substring(foundPrefix.length + 1);
}

// fourth
else if (x.parentReference.path.startsWith(FOURTH_COMMON_PREFIX_RAW)) {
// it's something like
// /drive/items/<some_id>!<another_id>:/${remoteBaseDir}/<subfolder>
// with uri encoded!
Expand All @@ -321,16 +370,27 @@ const fromDriveItemToEntity = (x: DriveItem, remoteBaseDir: string): Entity => {
key = x.name;
} else {
throw Error(
`we meet file/folder and do not know how to deal with it:\n${constructFromDriveItemToEntityError(
x
)}`
`file/folder with /drive/items/, no idea how to deal with it:
fullPathOriginal=${fullPathOriginal}
matchFirstPrefixRes=${matchFirstPrefixRes}
matchFifthPrefixRes=${matchFifthPrefixRes}
matchSecondPrefixRes=${matchSecondPrefixRes}
matchThirdPrefixRes=${matchThirdPrefixRes}
${constructFromDriveItemToEntityError(x)}`
);
}
} else {
}

// others
else {
throw Error(
`we meet file/folder and do not know how to deal with it:\n${constructFromDriveItemToEntityError(
x
)}`
`file/folder, no idea how to deal with it without known prefix:
fullPathOriginal=${fullPathOriginal}
matchFirstPrefixRes=${matchFirstPrefixRes}
matchFifthPrefixRes=${matchFifthPrefixRes}
matchSecondPrefixRes=${matchSecondPrefixRes}
matchThirdPrefixRes=${matchThirdPrefixRes}
${constructFromDriveItemToEntityError(x)}`
);
}

Expand Down

0 comments on commit a7cc4e2

Please sign in to comment.