diff --git a/packages/core/data-transfer/src/file/providers/source/index.ts b/packages/core/data-transfer/src/file/providers/source/index.ts index e3af20e3b2..119d527aee 100644 --- a/packages/core/data-transfer/src/file/providers/source/index.ts +++ b/packages/core/data-transfer/src/file/providers/source/index.ts @@ -15,7 +15,7 @@ import type { IAsset, IMetadata, ISourceProvider, ProviderType } from '../../../ import { createDecryptionCipher } from '../../../utils/encryption'; import { collect } from '../../../utils/stream'; import { ProviderInitializationError, ProviderTransferError } from '../../../errors/providers'; -import { isDirPathEquivalent, isPathEquivalent } from './utils'; +import { isDirPathEquivalent, isPathEquivalent, unknownPathToPosix } from './utils'; type StreamItemArray = Parameters[0]; @@ -143,16 +143,17 @@ class LocalFileSourceProvider implements ISourceProvider { if (entry.type !== 'File') { return false; } - console.log(`${filePath} is assets?`, isDirPathEquivalent('assets/uploads', filePath)); return isDirPathEquivalent('assets/uploads', filePath); }, onentry(entry) { // TODO: Check if we need to handle win32 paths here for the assets const { path: filePath, size = 0 } = entry; - const file = path.basename(filePath); + const normalizedPath = unknownPathToPosix(filePath); + const file = path.basename(normalizedPath); + const asset: IAsset = { filename: file, - filepath: filePath, + filepath: normalizedPath, stats: { size }, stream: entry as unknown as Readable, }; diff --git a/packages/core/data-transfer/src/file/providers/source/utils.ts b/packages/core/data-transfer/src/file/providers/source/utils.ts index d4470ad66f..e1d25aed0c 100644 --- a/packages/core/data-transfer/src/file/providers/source/utils.ts +++ b/packages/core/data-transfer/src/file/providers/source/utils.ts @@ -14,16 +14,30 @@ import path from 'path'; // Check if the directory of a given filePath (which can be either posix or win32) resolves to the same as the given posix-format path posixDirName export const isDirPathEquivalent = (posixDirName: string, filePath: string) => { - // if win32 convert to posix, then get dirname - const normalizedDir = path.posix.dirname(filePath.split(path.win32.sep).join(path.posix.sep)); + const normalizedDir = path.posix.dirname(unknownPathToPosix(filePath)); return isPathEquivalent(posixDirName, normalizedDir); }; // Check if two paths that can be either in posix or win32 format resolves to the same file export const isPathEquivalent = (fileA: string, fileB: string) => { // Check if paths appear to be win32 or posix, and if win32 convert to posix - const normalizedPathA = fileA.split(path.win32.sep).join(path.posix.sep); - const normalizedPathB = fileB.split(path.win32.sep).join(path.posix.sep); + const normalizedPathA = unknownPathToPosix(fileA); + const normalizedPathB = unknownPathToPosix(fileB); return !path.posix.relative(normalizedPathB, normalizedPathA).length; }; + +/** + * + * @param {string} filePath a path that may be either win32 or posix + * + * @returns {string} a posix path + */ +export const unknownPathToPosix = (filePath: string) => { + // if it includes a forward slash, it must be posix already -- we will not support win32 with mixed path separators + if (filePath.includes(path.posix.sep)) { + return filePath; + } + + return path.normalize(filePath).split(path.win32.sep).join(path.posix.sep); +};