diff --git a/assets/electron/template/app/src/handlers/fs/write-base64.js b/assets/electron/template/app/src/handlers/fs/write-base64.js new file mode 100644 index 00000000..25b0dc34 --- /dev/null +++ b/assets/electron/template/app/src/handlers/fs/write-base64.js @@ -0,0 +1,32 @@ +// @ts-check + +import { mkdir, writeFile } from 'node:fs/promises' +import { dirname } from 'node:path' + +/** + * @param {{url: string, correlationId?: string, body: {path: string, base64Data: string, flag?: string}}} json + * @param {import('ws').WebSocket} ws + */ +export default async (json, ws) => { + const destDirName = dirname(json.body.path) + await mkdir(destDirName, { recursive: true }) + + // Decode base64 string to buffer + const buffer = Buffer.from(json.body.base64Data, 'base64') + + await writeFile(json.body.path, buffer, { + flag: json.body.flag + }) + + /** + * @type {{url: string, correlationId?: string, body: {success: boolean}}} + */ + const writeFileResult = { + correlationId: json.correlationId, + url: json.url, + body: { + success: true + } + } + ws.send(JSON.stringify(writeFileResult)) +} diff --git a/assets/electron/template/app/src/index.js b/assets/electron/template/app/src/index.js index 29cd4129..fa164aec 100644 --- a/assets/electron/template/app/src/index.js +++ b/assets/electron/template/app/src/index.js @@ -17,6 +17,7 @@ import userFolder from './handlers/user/folder.js' // fs import fsWrite from './handlers/fs/write.js' +import fsWriteBase64 from './handlers/fs/write-base64.js' import fsRead from './handlers/fs/read.js' import fsReadBinary from './handlers/fs/read-binary.js' import fsFolderCreate from './handlers/fs/folder-create.js' @@ -368,6 +369,10 @@ const createAppServer = (mainWindow, serveStatic = true) => { await fsWrite(json, ws) break + case '/fs/file/write-base64': + await fsWriteBase64(json, ws) + break + case '/fs/file/read': await fsRead(json, ws) break diff --git a/src/shared/libs/plugin-poki/export.ts b/src/shared/libs/plugin-poki/export.ts index b4313a81..646cc213 100644 --- a/src/shared/libs/plugin-poki/export.ts +++ b/src/shared/libs/plugin-poki/export.ts @@ -1,4 +1,5 @@ import { createAction, createActionRunner, createPathParam, createStringParam, runWithLiveLogs } from '@pipelab/plugin-core' +import { dirname } from 'node:path' export const ID = 'poki-upload' @@ -42,8 +43,8 @@ export const uploadToPoki = createAction({ export const uploadToPokiRunner = createActionRunner( async ({ log, inputs, paths, abortSignal, cwd }) => { - const { join } = await import('node:path') - const { writeFile , cp, mkdir } = await import('node:fs/promises') + const { join, basename, delimiter } = await import('node:path') + const { writeFile, cp, mkdir } = await import('node:fs/promises') const { shell } = await import('electron') const { unpack } = paths @@ -82,6 +83,10 @@ export const uploadToPokiRunner = createActionRunner( ['upload', '--name', inputs.name, '--notes', inputs.notes], { cwd, + env: { + // DEBUG: '*', + PATH: `${dirname(poki)}${delimiter}${process.env.PATH}`, + }, cancelSignal: abortSignal, }, log,