mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-07-23 04:36:52 +00:00
added preview mode
This commit is contained in:
@@ -14,7 +14,11 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
contextBridge.exposeInMainWorld('electron', {
|
||||
onUpdateAvailable: (callback) => ipcRenderer.on('update_available', callback),
|
||||
onUpdateDownloaded: (callback) => ipcRenderer.on('update_downloaded', callback),
|
||||
restartApp: () => ipcRenderer.send('restart_app')
|
||||
restartApp: () => ipcRenderer.send('restart_app'),
|
||||
selectFile: async () => ipcRenderer.invoke('dialog:openFile'),
|
||||
readFile: async (filePath) => ipcRenderer.invoke('fs:readFile', filePath),
|
||||
selectAndZipDirectory: async (filePath) => ipcRenderer.invoke('fs:selectAndZip', filePath),
|
||||
|
||||
});
|
||||
|
||||
ipcRenderer.send('test-ipc');
|
@@ -6,13 +6,15 @@ import {
|
||||
} from '@capacitor-community/electron';
|
||||
import chokidar from 'chokidar';
|
||||
import type { MenuItemConstructorOptions } from 'electron';
|
||||
import { app, BrowserWindow, Menu, MenuItem, nativeImage, Tray, session, ipcMain } from 'electron';
|
||||
import { app, BrowserWindow, Menu, MenuItem, nativeImage, Tray, session, ipcMain, dialog } from 'electron';
|
||||
import electronIsDev from 'electron-is-dev';
|
||||
import electronServe from 'electron-serve';
|
||||
import windowStateKeeper from 'electron-window-state';
|
||||
const AdmZip = require('adm-zip');
|
||||
import { join } from 'path';
|
||||
import { myCapacitorApp } from '.';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path')
|
||||
|
||||
const defaultDomains = [
|
||||
'capacitor-electron://-',
|
||||
@@ -361,3 +363,70 @@ ipcMain.on('set-allowed-domains', (event, domains: string[]) => {
|
||||
});
|
||||
|
||||
|
||||
ipcMain.handle('dialog:openFile', async () => {
|
||||
const result = await dialog.showOpenDialog({
|
||||
properties: ['openFile'],
|
||||
filters: [
|
||||
{ name: 'ZIP Files', extensions: ['zip'] } // Restrict to ZIP files
|
||||
],
|
||||
});
|
||||
return result.filePaths[0];
|
||||
});
|
||||
|
||||
ipcMain.handle('fs:readFile', async (_, filePath) => {
|
||||
try {
|
||||
// Ensure the file exists
|
||||
if (!fs.existsSync(filePath)) {
|
||||
throw new Error('File does not exist.');
|
||||
}
|
||||
|
||||
// Ensure the filePath is an absolute path (optional but recommended for safety)
|
||||
const absolutePath = path.resolve(filePath);
|
||||
|
||||
// Read the file as a Buffer
|
||||
const fileBuffer = fs.readFileSync(absolutePath);
|
||||
|
||||
return fileBuffer
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error reading file:', error.message);
|
||||
return null; // Return null on error
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('fs:selectAndZip', async (_, path) => {
|
||||
let directoryPath = path
|
||||
if(!directoryPath){
|
||||
const { canceled, filePaths } = await dialog.showOpenDialog({
|
||||
properties: ['openDirectory'],
|
||||
});
|
||||
if (canceled || filePaths.length === 0) {
|
||||
console.log('No directory selected');
|
||||
return null;
|
||||
}
|
||||
|
||||
directoryPath = filePaths[0];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
|
||||
// Add the entire directory to the zip
|
||||
const zip = new AdmZip();
|
||||
|
||||
// Add the entire directory to the zip
|
||||
zip.addLocalFolder(directoryPath);
|
||||
|
||||
// Generate the zip file as a buffer
|
||||
const zipBuffer = zip.toBuffer();
|
||||
|
||||
return {buffer: zipBuffer, directoryPath}
|
||||
} catch (error) {
|
||||
return null
|
||||
}
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user