From a72af289ad4e6212ef47463aacac27c60963f579 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 10 Jan 2023 18:15:54 +0530 Subject: [PATCH 1/6] wait for stream to finish before returning --- src/services/fs.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/services/fs.ts b/src/services/fs.ts index ba22e39ff..0f56368ad 100644 --- a/src/services/fs.ts +++ b/src/services/fs.ts @@ -192,7 +192,9 @@ export async function isFolder(dirPath: string) { .catch(() => false); } -export const convertBrowserStreamToNode = (fileStream: any) => { +export const convertBrowserStreamToNode = ( + fileStream: ReadableStream +) => { const reader = fileStream.getReader(); const rs = new Readable(); @@ -210,10 +212,17 @@ export const convertBrowserStreamToNode = (fileStream: any) => { return rs; }; -export function writeStream(filePath: string, fileStream: any) { +export function writeStream( + filePath: string, + fileStream: ReadableStream +) { const writeable = fs.createWriteStream(filePath); const readable = convertBrowserStreamToNode(fileStream); readable.pipe(writeable); + return new Promise((resolve, reject) => { + writeable.on('finish', resolve); + writeable.on('error', reject); + }); } export async function readTextFile(filePath: string) { From 69392d9ee940bc6f6d1dcebbcb776bb9f494a860 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 10 Jan 2023 18:18:23 +0530 Subject: [PATCH 2/6] added loop to retry if tempPath already exists --- src/utils/temp.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils/temp.ts b/src/utils/temp.ts index 838d3692f..35a261e63 100644 --- a/src/utils/temp.ts +++ b/src/utils/temp.ts @@ -28,11 +28,11 @@ function generateTempName(length: number) { } export async function generateTempFilePath(formatSuffix: string) { - const tempDirPath = await getTempDirPath(); - const namePrefix = generateTempName(10); - const tempFilePath = path.join( - tempDirPath, - namePrefix + '-' + formatSuffix - ); + let tempFilePath: string; + do { + const tempDirPath = await getTempDirPath(); + const namePrefix = generateTempName(10); + tempFilePath = path.join(tempDirPath, namePrefix + '-' + formatSuffix); + } while (existsSync(tempFilePath)); return tempFilePath; } From 0e8ff75645b6d7d8e2226617d89a9587e696b170 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 10 Jan 2023 18:23:39 +0530 Subject: [PATCH 3/6] use stream to write file instead of loading whole file --- src/api/ffmpeg.ts | 9 +++------ src/api/imageProcessor.ts | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/api/ffmpeg.ts b/src/api/ffmpeg.ts index 1b0f57068..dba1a7577 100644 --- a/src/api/ffmpeg.ts +++ b/src/api/ffmpeg.ts @@ -1,5 +1,6 @@ import { ipcRenderer } from 'electron'; import { existsSync } from 'fs'; +import { writeStream } from '../services/fs'; import { logError } from '../services/logging'; import { ElectronFile } from '../types'; @@ -12,12 +13,8 @@ export async function runFFmpegCmd( let createdTempInputFile = null; try { if (!existsSync(inputFile.path)) { - const inputFileData = new Uint8Array(await inputFile.arrayBuffer()); - inputFilePath = await ipcRenderer.invoke( - 'write-temp-file', - inputFileData, - inputFile.name - ); + const tempFilePath = await ipcRenderer.invoke('get-temp-file-path'); + inputFilePath = writeStream(tempFilePath, await inputFile.stream()); createdTempInputFile = true; } else { inputFilePath = inputFile.path; diff --git a/src/api/imageProcessor.ts b/src/api/imageProcessor.ts index 07389bd71..d85895c00 100644 --- a/src/api/imageProcessor.ts +++ b/src/api/imageProcessor.ts @@ -1,5 +1,6 @@ import { ipcRenderer } from 'electron/renderer'; import { existsSync } from 'fs'; +import { writeStream } from '../services/fs'; import { logError } from '../services/logging'; import { ElectronFile } from '../types'; @@ -20,12 +21,8 @@ export async function generateImageThumbnail( let createdTempInputFile = null; try { if (!existsSync(inputFile.path)) { - const inputFileData = new Uint8Array(await inputFile.arrayBuffer()); - inputFilePath = await ipcRenderer.invoke( - 'write-temp-file', - inputFileData, - inputFile.name - ); + const tempFilePath = await ipcRenderer.invoke('get-temp-file-path'); + inputFilePath = writeStream(tempFilePath, await inputFile.stream()); createdTempInputFile = true; } else { inputFilePath = inputFile.path; From 6773c07f2dd1a08a603d3c8bf3f227f32929b92a Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 10 Jan 2023 18:24:03 +0530 Subject: [PATCH 4/6] change write-temp-file to get-temp-file-path --- src/utils/ipcComms.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/utils/ipcComms.ts b/src/utils/ipcComms.ts index 2bbe39452..3acc87141 100644 --- a/src/utils/ipcComms.ts +++ b/src/utils/ipcComms.ts @@ -23,11 +23,8 @@ import { skipAppVersion, updateAndRestart, } from '../services/appUpdater'; -import { - deleteTempFile, - runFFmpegCmd, - writeTempFile, -} from '../services/ffmpeg'; +import { deleteTempFile, runFFmpegCmd } from '../services/ffmpeg'; +import { generateTempFilePath } from './temp'; export default function setupIpcComs( tray: Tray, @@ -140,12 +137,9 @@ export default function setupIpcComs( return runFFmpegCmd(cmd, inputFilePath, outputFileName); } ); - ipcMain.handle( - 'write-temp-file', - (_, fileStream: Uint8Array, fileName: string) => { - return writeTempFile(fileStream, fileName); - } - ); + ipcMain.handle('get-temp-file-path', (_, formatSuffix) => { + return generateTempFilePath(formatSuffix); + }); ipcMain.handle('remove-temp-file', (_, tempFilePath: string) => { return deleteTempFile(tempFilePath); }); From e61b14c3736fd2d58ae3d2bb282f4498d4295204 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 10 Jan 2023 18:50:30 +0530 Subject: [PATCH 5/6] update writeStream to async function --- src/api/export.ts | 6 +++--- src/services/fs.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/export.ts b/src/api/export.ts index 93e6b4f4d..84239bea3 100644 --- a/src/api/export.ts +++ b/src/api/export.ts @@ -22,11 +22,11 @@ export const checkExistsAndRename = async ( } }; -export const saveStreamToDisk = ( +export const saveStreamToDisk = async ( filePath: string, - fileStream: ReadableStream + fileStream: ReadableStream ) => { - writeStream(filePath, fileStream); + await writeStream(filePath, fileStream); }; export const saveFileToDisk = async (path: string, fileData: any) => { diff --git a/src/services/fs.ts b/src/services/fs.ts index 0f56368ad..9565cb086 100644 --- a/src/services/fs.ts +++ b/src/services/fs.ts @@ -212,14 +212,14 @@ export const convertBrowserStreamToNode = ( return rs; }; -export function writeStream( +export async function writeStream( filePath: string, fileStream: ReadableStream ) { const writeable = fs.createWriteStream(filePath); const readable = convertBrowserStreamToNode(fileStream); readable.pipe(writeable); - return new Promise((resolve, reject) => { + await new Promise((resolve, reject) => { writeable.on('finish', resolve); writeable.on('error', reject); }); From ab09f5e759e33fd8b63ed670a535e11aaf310cec Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 10 Jan 2023 18:50:49 +0530 Subject: [PATCH 6/6] fix api --- src/api/ffmpeg.ts | 8 ++++++-- src/api/imageProcessor.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/api/ffmpeg.ts b/src/api/ffmpeg.ts index dba1a7577..462a63385 100644 --- a/src/api/ffmpeg.ts +++ b/src/api/ffmpeg.ts @@ -13,8 +13,12 @@ export async function runFFmpegCmd( let createdTempInputFile = null; try { if (!existsSync(inputFile.path)) { - const tempFilePath = await ipcRenderer.invoke('get-temp-file-path'); - inputFilePath = writeStream(tempFilePath, await inputFile.stream()); + const tempFilePath = await ipcRenderer.invoke( + 'get-temp-file-path', + inputFile.name + ); + await writeStream(tempFilePath, await inputFile.stream()); + inputFilePath = tempFilePath; createdTempInputFile = true; } else { inputFilePath = inputFile.path; diff --git a/src/api/imageProcessor.ts b/src/api/imageProcessor.ts index d85895c00..61b0ae529 100644 --- a/src/api/imageProcessor.ts +++ b/src/api/imageProcessor.ts @@ -21,8 +21,12 @@ export async function generateImageThumbnail( let createdTempInputFile = null; try { if (!existsSync(inputFile.path)) { - const tempFilePath = await ipcRenderer.invoke('get-temp-file-path'); - inputFilePath = writeStream(tempFilePath, await inputFile.stream()); + const tempFilePath = await ipcRenderer.invoke( + 'get-temp-file-path', + inputFile.name + ); + await writeStream(tempFilePath, await inputFile.stream()); + inputFilePath = tempFilePath; createdTempInputFile = true; } else { inputFilePath = inputFile.path;