server: replace fs callbacks with promises

This commit is contained in:
Jesse Chan
2020-12-09 22:24:28 +08:00
parent 37a94912f4
commit fd971ddcd8
3 changed files with 22 additions and 40 deletions
+21 -21
View File
@@ -242,28 +242,28 @@ router.post<unknown, unknown, CreateTorrentOptions>('/create', async (req, res)
return;
}
fs.writeFile(torrentPath, torrent, (writeErr) => {
if (writeErr) {
fs.promises.writeFile(torrentPath, torrent).then(
() => {
res.attachment(torrentFileName);
res.download(torrentPath);
req.services?.clientGatewayService
?.addTorrentsByFile({
files: [torrent.toString('base64')],
destination: fs.lstatSync(sanitizedPath).isDirectory() ? sanitizedPath : path.dirname(sanitizedPath),
tags: tags ?? [],
isBasePath: true,
isCompleted: true,
start: start || false,
})
.catch(() => {
// do nothing.
});
},
(writeErr) => {
callback(null, writeErr);
return;
}
res.attachment(torrentFileName);
res.download(torrentPath);
req.services?.clientGatewayService
?.addTorrentsByFile({
files: [torrent.toString('base64')],
destination: fs.lstatSync(sanitizedPath).isDirectory() ? sanitizedPath : path.dirname(sanitizedPath),
tags: tags ?? [],
isBasePath: true,
isCompleted: true,
start: start || false,
})
.catch(() => {
// do nothing.
});
});
},
);
},
);
});
@@ -25,7 +25,6 @@ import type {TorrentTracker} from '@shared/types/TorrentTracker';
import type {TransferSummary} from '@shared/types/TransferData';
import type {SetClientSettingsOptions} from '@shared/types/api/client';
import {createDirectory} from '../../util/fileUtil';
import ClientGatewayService from '../interfaces/clientGatewayService';
import ClientRequestManager from './clientRequestManager';
import {getMethodCalls, processMethodCallResponse} from './util/rTorrentMethodCallUtil';
@@ -95,7 +94,7 @@ class RTorrentClientGatewayService extends ClientGatewayService {
isCompleted,
start,
}: Required<AddTorrentByURLOptions>): Promise<void> {
await createDirectory(destination);
await fs.promises.mkdir(destination, {recursive: true});
const torrentPaths: Array<string> = (
await Promise.all(
-17
View File
@@ -53,23 +53,6 @@ export const sanitizePath = (input?: string): string => {
return path.resolve(input).replace(controlRe, '');
};
export const createDirectory = (directoryPath: string): Promise<void> => {
return new Promise<void>((resolve, reject) => {
if (directoryPath) {
fs.mkdir(directoryPath, {recursive: true}, (error) => {
if (error) {
console.trace('Error creating directory.', error);
reject();
return;
}
resolve();
});
} else {
reject();
}
});
};
export const getDirectoryList = async (inputPath: string) => {
if (typeof inputPath !== 'string') {
throw fileNotFoundError();