mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Join and normalize paths consistently.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
ee072b038a
commit
f454b781ff
@@ -1,5 +1,46 @@
|
||||
import platform, { posix } from 'path';
|
||||
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
|
||||
|
||||
/**
|
||||
* Changes a potential Windows path into a POSIX path.
|
||||
*
|
||||
* @param path - Path to check (POSIX or Windows).
|
||||
*
|
||||
* @returns The potentially changed path (POSIX).
|
||||
*/
|
||||
const windowsToPosixPath = (path: string): string => path.replace(/\\+/gu, '/');
|
||||
|
||||
/**
|
||||
* Resolves relative segments in the path/
|
||||
*
|
||||
* @param path - Path to check (POSIX or Windows).
|
||||
*
|
||||
* @returns The potentially changed path (POSIX).
|
||||
*/
|
||||
export const normalizeFilePath = (path: string): string =>
|
||||
posix.normalize(windowsToPosixPath(path));
|
||||
|
||||
/**
|
||||
* Adds the paths to the base path.
|
||||
*
|
||||
* @param basePath - The base path (POSIX or Windows).
|
||||
* @param paths - Subpaths to attach (POSIX).
|
||||
*
|
||||
* @returns The potentially changed path (POSIX).
|
||||
*/
|
||||
export const joinFilePath = (basePath: string, ...paths: string[]): string =>
|
||||
posix.join(windowsToPosixPath(basePath), ...paths);
|
||||
|
||||
/**
|
||||
* Converts the path into an OS-dependent path.
|
||||
*
|
||||
* @param path - Path to check (POSIX).
|
||||
*
|
||||
* @returns The potentially changed path (OS-dependent).
|
||||
*/
|
||||
export const toSystemFilePath = (path: string): string =>
|
||||
platform.normalize(path);
|
||||
|
||||
/**
|
||||
* Makes sure the input path has exactly 1 slash at the end.
|
||||
* Multiple slashes will get merged into one.
|
||||
|
||||
Reference in New Issue
Block a user