diff --git a/app/src/actions/Publish.ts b/app/src/actions/Publish.ts index a7f73cf..ab75050 100644 --- a/app/src/actions/Publish.ts +++ b/app/src/actions/Publish.ts @@ -2,10 +2,10 @@ import { Action, ActionTypes } from '../reducers/Publish' import { AppState } from '../reducers' import { Base64Message } from '../../../backend/src/Model/Base64Message' import { Dispatch } from 'redux' -import { promises as fsPromise } from 'fs' -import { MqttMessage, makePublishEvent, rendererEvents, rendererRpc } from '../../../events' +import { MqttMessage, makePublishEvent, rendererEvents, rendererRpc, readFromFile } from '../../../events' import { makeOpenDialogRpc } from '../../../events/OpenDialogRequest' import { showError } from './Global' +import { Base64 } from 'js-base64' export const setTopic = (topic?: string): Action => { return { @@ -14,13 +14,12 @@ export const setTopic = (topic?: string): Action => { } } -export const openFile = () => async (dispatch: Dispatch, getState: () => AppState) => { +export const openFile = (encoding: 'utf8' = 'utf8') => async (dispatch: Dispatch, getState: () => AppState) => { try { - const file = await getFileContent() + const file = await getFileContent(encoding) if (file) { dispatch( - setPayload(Base64Message.fromBuffer(file.data).toUnicodeString() - )) + setPayload(file.data)) } } catch (error) { dispatch(showError(error)) @@ -29,9 +28,9 @@ export const openFile = () => async (dispatch: Dispatch, getState: () => Ap type FileParameters = { name: string, - data: Buffer + data: string } -async function getFileContent(): Promise { +async function getFileContent(encoding: string): Promise { const rejectReasons = { noFileSelected: 'No file selected', errorReadingFile: 'Error reading file' @@ -51,8 +50,8 @@ async function getFileContent(): Promise { throw rejectReasons.noFileSelected } try { - const data = await fsPromise.readFile(selectedFile) - return { name: selectedFile, data } + const data = await rendererRpc.call(readFromFile, { filePath: selectedFile, encoding }) + return { name: selectedFile, data: data.toString(encoding) } } catch (error) { throw rejectReasons.errorReadingFile } diff --git a/app/src/components/helper/Save.tsx b/app/src/components/helper/Save.tsx index 25a4cff..cf54819 100644 --- a/app/src/components/helper/Save.tsx +++ b/app/src/components/helper/Save.tsx @@ -5,7 +5,7 @@ import CustomIconButton from './CustomIconButton' import { SaveAlt } from '@material-ui/icons' import { bindActionCreators } from 'redux' -import { rendererRpc, writeFile } from '../../../../events' +import { rendererRpc, writeToFile } from '../../../../events' import { makeSaveDialogRpc } from '../../../../events/OpenDialogRequest' import { globalActions } from '../../actions' @@ -21,7 +21,7 @@ export async function saveToFile(data: string): Promise { if (!canceled && filePath !== undefined) { try { - const filename = await rendererRpc.call(writeFile, { filePath, data }) + const filename = await rendererRpc.call(writeToFile, { filePath, data }) return filePath } catch (error) { throw rejectReasons.errorWritingFile diff --git a/events/Events.ts b/events/Events.ts index a951724..d10b666 100644 --- a/events/Events.ts +++ b/events/Events.ts @@ -55,6 +55,10 @@ export const getAppVersion: RpcEvent = { topic: 'getAppVersion', } -export const writeFile: RpcEvent<{ filePath: string, data: string }, void> = { +export const writeToFile: RpcEvent<{ filePath: string, data: string, encoding?: string }, void> = { topic: 'writeFile', +} + +export const readFromFile: RpcEvent<{ filePath: string, encoding?: string }, Buffer> = { + topic: 'readFromFile', } \ No newline at end of file diff --git a/src/electron.ts b/src/electron.ts index 5df6ca2..db66d85 100644 --- a/src/electron.ts +++ b/src/electron.ts @@ -12,7 +12,7 @@ import { waitForDevServer, isDev, runningUiTestOnCi, loadDevTools } from './deve import { shouldAutoUpdate, handleAutoUpdate } from './autoUpdater' import { registerCrashReporter } from './registerCrashReporter' import { makeOpenDialogRpc, makeSaveDialogRpc } from '../events/OpenDialogRequest' -import { backendRpc, getAppVersion, writeFile } from '../events' +import { backendRpc, getAppVersion, writeToFile, readFromFile } from '../events' registerCrashReporter() @@ -33,8 +33,12 @@ app.whenReady().then(() => { backendRpc.on(getAppVersion, async () => app.getVersion()) - backendRpc.on(writeFile, async ({ filePath, data }) => { - await fsPromise.writeFile(filePath, Buffer.from(data, 'base64')) + backendRpc.on(writeToFile, async ({ filePath, data, encoding }) => { + await fsPromise.writeFile(filePath, Buffer.from(data, 'base64'), { encoding }) + }) + + backendRpc.on(readFromFile, async ({ filePath, encoding }) => { + return fsPromise.readFile(filePath, { encoding }) }) })