server: migrate add torrents functions to clientGatewayService

This commit is contained in:
Jesse Chan
2020-10-08 19:29:13 +08:00
parent 2940c2379b
commit 79a7c9c6f0
9 changed files with 211 additions and 267 deletions
+13 -9
View File
@@ -4,6 +4,12 @@ import path from 'path';
import config from '../../config';
export const accessDeniedError = () => {
const error = new Error() as NodeJS.ErrnoException;
error.code = 'EACCES';
return error;
};
export const isAllowedPath = (resolvedPath: string) => {
if (config.allowedPaths == null) {
return true;
@@ -17,20 +23,18 @@ export const isAllowedPath = (resolvedPath: string) => {
};
export const sanitizePath = (input: string) => {
if (typeof input !== 'string') {
throw accessDeniedError();
}
// eslint-disable-next-line no-control-regex
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
return path.resolve(input).replace(controlRe, '');
};
export const accessDeniedError = () => {
const error = new Error() as NodeJS.ErrnoException;
error.code = 'EACCES';
return error;
};
export const createDirectory = (options: {path: string}) => {
if (options.path) {
fs.mkdir(options.path, {recursive: true}, (error) => {
export const createDirectory = (directoryPath: string) => {
if (directoryPath) {
fs.mkdir(directoryPath, {recursive: true}, (error) => {
if (error) {
console.trace('Error creating directory.', error);
}