From ac11f3190b13245e5b4609d4ce07b5368b3be971 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Sat, 14 Nov 2020 01:30:01 +0800 Subject: [PATCH] server: rTorrent: ensure the temporary file is world-readable This allows rTorrent to read the file when the Flood and rTorrent are running in different users and when the system has a restrictive default umask. Bug: #86 --- server/services/rTorrent/clientGatewayService.ts | 2 +- server/util/tempFileUtil.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/server/services/rTorrent/clientGatewayService.ts b/server/services/rTorrent/clientGatewayService.ts index abd80f45..3130cabc 100644 --- a/server/services/rTorrent/clientGatewayService.ts +++ b/server/services/rTorrent/clientGatewayService.ts @@ -63,7 +63,7 @@ class RTorrentClientGatewayService extends ClientGatewayService { }: Required): Promise { const torrentPaths = await Promise.all( files.map(async (file) => { - return saveBufferToTempFile(Buffer.from(file, 'base64'), 'torrent'); + return saveBufferToTempFile(Buffer.from(file, 'base64'), 'torrent', {mode: 0o664}); }), ); diff --git a/server/util/tempFileUtil.ts b/server/util/tempFileUtil.ts index 79cd22d8..c16e3002 100644 --- a/server/util/tempFileUtil.ts +++ b/server/util/tempFileUtil.ts @@ -1,6 +1,6 @@ import axios, {AxiosError, AxiosResponse} from 'axios'; import crypto from 'crypto'; -import fs from 'fs'; +import fs, {WriteFileOptions} from 'fs'; import {getTempPath} from '../models/TemporaryStorage'; @@ -33,10 +33,14 @@ const delayedDelete = (tempPath: string): void => { * @param {string} extension - file extension of temp file * @return {string} - path of saved temporary file. deleted after 5 minutes. */ -export const saveBufferToTempFile = async (buffer: Buffer, extension?: string): Promise => { +export const saveBufferToTempFile = async ( + buffer: Buffer, + extension?: string, + options?: WriteFileOptions, +): Promise => { const tempPath = getTempFilePath(extension); - fs.writeFileSync(tempPath, buffer); + fs.writeFileSync(tempPath, buffer, options); delayedDelete(tempPath);