mirror of
https://github.com/zoriya/flood.git
synced 2026-06-01 18:47:44 +00:00
server: replace fs callbacks with promises
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user