fix assets output paths and clean up

This commit is contained in:
Ben Irvin 2023-04-18 12:34:54 +02:00
parent 9963f90b1c
commit e429fb7ee9
2 changed files with 23 additions and 8 deletions

View File

@ -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<typeof chain>[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,
};

View File

@ -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);
};