diff --git a/server/routes/api/torrents.ts b/server/routes/api/torrents.ts index af6c0a4d..2ca34e2f 100644 --- a/server/routes/api/torrents.ts +++ b/server/routes/api/torrents.ts @@ -242,28 +242,28 @@ router.post('/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. - }); - }); + }, + ); }, ); }); diff --git a/server/services/rTorrent/clientGatewayService.ts b/server/services/rTorrent/clientGatewayService.ts index bd4fbeb7..185c07e2 100644 --- a/server/services/rTorrent/clientGatewayService.ts +++ b/server/services/rTorrent/clientGatewayService.ts @@ -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): Promise { - await createDirectory(destination); + await fs.promises.mkdir(destination, {recursive: true}); const torrentPaths: Array = ( await Promise.all( diff --git a/server/util/fileUtil.ts b/server/util/fileUtil.ts index 39603bb0..26b305cf 100644 --- a/server/util/fileUtil.ts +++ b/server/util/fileUtil.ts @@ -53,23 +53,6 @@ export const sanitizePath = (input?: string): string => { return path.resolve(input).replace(controlRe, ''); }; -export const createDirectory = (directoryPath: string): Promise => { - return new Promise((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();