This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
fotochallenge/src/lib/index.ts
Valentin Brandl 6615e2788a
All checks were successful
/ Misc Linters (push) Successful in 27s
/ Misc Linters (pull_request) Successful in 20s
/ Build App (pull_request) Successful in 58s
/ Build App (push) Successful in 47s
Platform independent path validation
2024-08-17 14:59:55 +02:00

25 lines
801 B
TypeScript

// place files you want to import through the `$lib` alias in this folder.
import path from 'path';
function safePath(basePath: string, name: string): boolean {
const fullPath = `${basePath}/${name}`;
const relative = path.relative(basePath, fullPath);
return (
!!relative &&
// does move out of `basePath`
!relative.startsWith(`..${path.sep}`) &&
// exactly one layer deep, e.g. no `./uplodas/foo/bar`
!relative.includes(path.sep) &&
// result is not an absolute path
!path.isAbsolute(relative)
);
}
const defaultPath: string = './uploads';
if (!('STORAGE_PATH' in process.env)) {
console.warn(`'STORAGE_PATH' environment variable is not set. Defaulting to ${defaultPath}`);
}
export const storagePath: string = process.env.STORAGE_PATH ?? defaultPath;
export default safePath;