aboutsummaryrefslogtreecommitdiff
path: root/main.js
blob: 9220a09f7e9368c1e32d79f2953895ebbc9f3733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const {app, dialog, BrowserWindow, ipcMain, protocol} = require('electron');
const zlib = require('zlib');
const fs = require('fs');
const util = require('util');
const {userInfo} = require('os');
const remote = require("@electron/remote/main");

protocol.registerSchemesAsPrivileged([
    { scheme: 'pbip', privileges: { bypassCSP: true, secure: true, stream: true, corsEnabled: false, supportFetchAPI: true } }
])

remote.initialize();

function open() {
    let win = global.win = new BrowserWindow({
        width: 1500,
        minWidth: 800,
        title: "Faunerie",
        height: 800,
        minHeight: 450,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            backgroundThrottling: false
        },
        titleBarStyle: "hidden",
        titleBarOverlay: {
            color: "#00000000",
            symbolColor: "#ffffff",
            height: 43
        },
        backgroundColor: "#222222",
        trafficLightPosition: {
            x: 13,
            y: 13
        },
    });

    remote.enable(win.webContents);

    win.on('close', async (e) => {
        e.preventDefault();
        if (await win.webContents.executeJavaScript("instance?.dataStore.unloaded;")) win.destroy();
        await win.webContents.executeJavaScript("instance?.safeUnload().then(() => { require('electron').ipcRenderer.send('destroy'); });");
    });

    win.loadFile("./dom/index.html");
    win.on('ready-to-show', () => {
        //@ts-ignore
        win.send("path", app.getPath("userData"));
    });

    ipcMain.handle('openFolder', () => {
        return dialog.showOpenDialogSync({
            title: "Select the directory where your database has been saved",
            message: "Select the directory where your database has been saved",
            defaultPath: userInfo().homedir,
            buttonLabel: "Open",
            properties: ["openDirectory", "treatPackageAsDirectory", "dontAddToRecent", "createDirectory"]
        });
    });

    ipcMain.handle('openPreprocessed', () => {
        return dialog.showOpenDialogSync({
            title: "Select the file corresponding to your preprocessed database",
            message: "Select the file corresponding to your preprocessed database",
            defaultPath: userInfo().homedir,
            buttonLabel: "Open",
            properties: ["openFile", "treatPackageAsDirectory", "dontAddToRecent"]
        });
    });

    ipcMain.on('destroy', () => {
        win.destroy();
    });
}

if (app.getName() !== "Electron") {
    const gotTheLock = app.requestSingleInstanceLock();

    if (!gotTheLock) {
        app.quit();
    }
}

app.whenReady().then(() => {
    protocol.handle('pbip', async (req) => {
        let { pathname, searchParams } = new URL(req.url);
        let mime = searchParams.get("mime") ?? "application/octet-stream";

        if (process.platform === "win32") pathname = pathname.substring(1);

        const inflateRawSync = util.promisify(zlib.inflateRaw);

        try {
            let file = await fs.promises.readFile(pathname);
            let data = await inflateRawSync(file);

            return new Response(data, {
                status: 200,
                headers: { 'content-type': mime }
            });
        } catch (e) {
            console.error(e);

            return new Response(e.stack, {
                status: 500,
                headers: { 'content-type': 'text/plain' }
            });
        }
    });

    open();

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) open();
    });
});

app.on('window-all-closed', () => {
    app.quit();
});