Support specifying file encoding
This commit is contained in:
@@ -2,10 +2,10 @@ import { Action, ActionTypes } from '../reducers/Publish'
|
|||||||
import { AppState } from '../reducers'
|
import { AppState } from '../reducers'
|
||||||
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
||||||
import { Dispatch } from 'redux'
|
import { Dispatch } from 'redux'
|
||||||
import { promises as fsPromise } from 'fs'
|
import { MqttMessage, makePublishEvent, rendererEvents, rendererRpc, readFromFile } from '../../../events'
|
||||||
import { MqttMessage, makePublishEvent, rendererEvents, rendererRpc } from '../../../events'
|
|
||||||
import { makeOpenDialogRpc } from '../../../events/OpenDialogRequest'
|
import { makeOpenDialogRpc } from '../../../events/OpenDialogRequest'
|
||||||
import { showError } from './Global'
|
import { showError } from './Global'
|
||||||
|
import { Base64 } from 'js-base64'
|
||||||
|
|
||||||
export const setTopic = (topic?: string): Action => {
|
export const setTopic = (topic?: string): Action => {
|
||||||
return {
|
return {
|
||||||
@@ -14,13 +14,12 @@ export const setTopic = (topic?: string): Action => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const openFile = () => async (dispatch: Dispatch<any>, getState: () => AppState) => {
|
export const openFile = (encoding: 'utf8' = 'utf8') => async (dispatch: Dispatch<any>, getState: () => AppState) => {
|
||||||
try {
|
try {
|
||||||
const file = await getFileContent()
|
const file = await getFileContent(encoding)
|
||||||
if (file) {
|
if (file) {
|
||||||
dispatch(
|
dispatch(
|
||||||
setPayload(Base64Message.fromBuffer(file.data).toUnicodeString()
|
setPayload(file.data))
|
||||||
))
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
dispatch(showError(error))
|
dispatch(showError(error))
|
||||||
@@ -29,9 +28,9 @@ export const openFile = () => async (dispatch: Dispatch<any>, getState: () => Ap
|
|||||||
|
|
||||||
type FileParameters = {
|
type FileParameters = {
|
||||||
name: string,
|
name: string,
|
||||||
data: Buffer
|
data: string
|
||||||
}
|
}
|
||||||
async function getFileContent(): Promise<FileParameters | undefined> {
|
async function getFileContent(encoding: string): Promise<FileParameters | undefined> {
|
||||||
const rejectReasons = {
|
const rejectReasons = {
|
||||||
noFileSelected: 'No file selected',
|
noFileSelected: 'No file selected',
|
||||||
errorReadingFile: 'Error reading file'
|
errorReadingFile: 'Error reading file'
|
||||||
@@ -51,8 +50,8 @@ async function getFileContent(): Promise<FileParameters | undefined> {
|
|||||||
throw rejectReasons.noFileSelected
|
throw rejectReasons.noFileSelected
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const data = await fsPromise.readFile(selectedFile)
|
const data = await rendererRpc.call(readFromFile, { filePath: selectedFile, encoding })
|
||||||
return { name: selectedFile, data }
|
return { name: selectedFile, data: data.toString(encoding) }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw rejectReasons.errorReadingFile
|
throw rejectReasons.errorReadingFile
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import CustomIconButton from './CustomIconButton'
|
|||||||
|
|
||||||
import { SaveAlt } from '@material-ui/icons'
|
import { SaveAlt } from '@material-ui/icons'
|
||||||
import { bindActionCreators } from 'redux'
|
import { bindActionCreators } from 'redux'
|
||||||
import { rendererRpc, writeFile } from '../../../../events'
|
import { rendererRpc, writeToFile } from '../../../../events'
|
||||||
import { makeSaveDialogRpc } from '../../../../events/OpenDialogRequest'
|
import { makeSaveDialogRpc } from '../../../../events/OpenDialogRequest'
|
||||||
|
|
||||||
import { globalActions } from '../../actions'
|
import { globalActions } from '../../actions'
|
||||||
@@ -21,7 +21,7 @@ export async function saveToFile(data: string): Promise<string | undefined> {
|
|||||||
|
|
||||||
if (!canceled && filePath !== undefined) {
|
if (!canceled && filePath !== undefined) {
|
||||||
try {
|
try {
|
||||||
const filename = await rendererRpc.call(writeFile, { filePath, data })
|
const filename = await rendererRpc.call(writeToFile, { filePath, data })
|
||||||
return filePath
|
return filePath
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw rejectReasons.errorWritingFile
|
throw rejectReasons.errorWritingFile
|
||||||
|
|||||||
@@ -55,6 +55,10 @@ export const getAppVersion: RpcEvent<void, string> = {
|
|||||||
topic: 'getAppVersion',
|
topic: 'getAppVersion',
|
||||||
}
|
}
|
||||||
|
|
||||||
export const writeFile: RpcEvent<{ filePath: string, data: string }, void> = {
|
export const writeToFile: RpcEvent<{ filePath: string, data: string, encoding?: string }, void> = {
|
||||||
topic: 'writeFile',
|
topic: 'writeFile',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const readFromFile: RpcEvent<{ filePath: string, encoding?: string }, Buffer> = {
|
||||||
|
topic: 'readFromFile',
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@ import { waitForDevServer, isDev, runningUiTestOnCi, loadDevTools } from './deve
|
|||||||
import { shouldAutoUpdate, handleAutoUpdate } from './autoUpdater'
|
import { shouldAutoUpdate, handleAutoUpdate } from './autoUpdater'
|
||||||
import { registerCrashReporter } from './registerCrashReporter'
|
import { registerCrashReporter } from './registerCrashReporter'
|
||||||
import { makeOpenDialogRpc, makeSaveDialogRpc } from '../events/OpenDialogRequest'
|
import { makeOpenDialogRpc, makeSaveDialogRpc } from '../events/OpenDialogRequest'
|
||||||
import { backendRpc, getAppVersion, writeFile } from '../events'
|
import { backendRpc, getAppVersion, writeToFile, readFromFile } from '../events'
|
||||||
|
|
||||||
registerCrashReporter()
|
registerCrashReporter()
|
||||||
|
|
||||||
@@ -33,8 +33,12 @@ app.whenReady().then(() => {
|
|||||||
|
|
||||||
backendRpc.on(getAppVersion, async () => app.getVersion())
|
backendRpc.on(getAppVersion, async () => app.getVersion())
|
||||||
|
|
||||||
backendRpc.on(writeFile, async ({ filePath, data }) => {
|
backendRpc.on(writeToFile, async ({ filePath, data, encoding }) => {
|
||||||
await fsPromise.writeFile(filePath, Buffer.from(data, 'base64'))
|
await fsPromise.writeFile(filePath, Buffer.from(data, 'base64'), { encoding })
|
||||||
|
})
|
||||||
|
|
||||||
|
backendRpc.on(readFromFile, async ({ filePath, encoding }) => {
|
||||||
|
return fsPromise.readFile(filePath, { encoding })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user