Initial
This commit is contained in:
40
server/utils/pathUtils.js
Normal file
40
server/utils/pathUtils.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import path from 'path';
|
||||
|
||||
export { path };
|
||||
|
||||
export function basenameAny(value, fallback = '') {
|
||||
const normalized = String(value || fallback || '').replace(/\\/g, '/');
|
||||
return path.posix.basename(normalized);
|
||||
}
|
||||
|
||||
export function extnameAny(value) {
|
||||
return path.extname(basenameAny(value)).toLowerCase();
|
||||
}
|
||||
|
||||
export function nameWithoutExtensionAny(value, fallback = '') {
|
||||
return path.parse(basenameAny(value, fallback)).name;
|
||||
}
|
||||
|
||||
export function sanitizeFileName(name, fallback = 'asset.bin') {
|
||||
return basenameAny(name, fallback)
|
||||
.replace(/[/\\?%*:|"<>]/g, '-')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim()
|
||||
.slice(0, 180) || fallback;
|
||||
}
|
||||
|
||||
export function normalizePackagePath(value, fallbackName = '') {
|
||||
const clean = String(value || fallbackName || '')
|
||||
.replace(/\\/g, '/')
|
||||
.split('/')
|
||||
.filter((part) => part && part !== '.' && part !== '..')
|
||||
.join('/');
|
||||
return clean.slice(0, 500);
|
||||
}
|
||||
|
||||
export function isPathInside(parentDir, candidatePath) {
|
||||
const parent = path.resolve(parentDir);
|
||||
const candidate = path.resolve(candidatePath);
|
||||
const relative = path.relative(parent, candidate);
|
||||
return relative === '' || (relative && !relative.startsWith('..') && !path.isAbsolute(relative));
|
||||
}
|
||||
Reference in New Issue
Block a user