File size: 5,297 Bytes
32c5da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const { app, BrowserWindow, dialog, ipcMain, shell, nativeImage } = require("electron");
const path = require("path");
const fs = require("fs");
const { spawn } = require("child_process");

const BACKEND_URL = "http://127.0.0.1:8008/health";
let backendProcess = null;

function projectRoot() {
  return path.resolve(__dirname, "..", "..");
}

function isDev() {
  return process.env.IMAGEFORGE_DEV === "1";
}

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function waitForBackend(timeoutMs = 20000) {
  const start = Date.now();
  while (Date.now() - start < timeoutMs) {
    try {
      const response = await fetch(BACKEND_URL);
      if (response.ok) {
        return true;
      }
    } catch (_) {}
    await sleep(500);
  }
  return false;
}

function startBackend() {
  const root = projectRoot();
  const backendModule = "backend.app.main";
  const pythonCandidates = process.platform === "win32"
    ? [
        path.join(root, ".venv", "Scripts", "python.exe"),
        path.join(root, "..", ".venv", "Scripts", "python.exe"),
        "py",
        "python",
      ]
    : [
        path.join(root, ".venv", "bin", "python"),
        path.join(root, "..", ".venv", "bin", "python"),
        "python3",
        "python",
      ];

  for (const command of pythonCandidates) {
    if (command.includes(path.sep) && !fs.existsSync(command)) {
      continue;
    }
    try {
      const args = command === "py" ? ["-3", "-m", backendModule] : ["-m", backendModule];
      const child = spawn(command, args, {
        cwd: root,
        env: {
          ...process.env,
          IMAGEFORGE_HOST: process.env.IMAGEFORGE_HOST || "127.0.0.1",
          IMAGEFORGE_PORT: process.env.IMAGEFORGE_PORT || "8008",
          IMAGEFORGE_CORS_ORIGINS: process.env.IMAGEFORGE_CORS_ORIGINS || "http://localhost:5173",
          IMAGEFORGE_CACHE_ROOT: process.env.IMAGEFORGE_CACHE_ROOT || path.join(root, ".cache"),
          IMAGEFORGE_TMP_ROOT: process.env.IMAGEFORGE_TMP_ROOT || path.join(root, ".cache", "tmp"),
        },
        stdio: "ignore",
        detached: false,
      });
      child.once("error", () => {});
      backendProcess = child;
      return;
    } catch (_) {}
  }
}

async function createWindow() {
  startBackend();
  const healthy = await waitForBackend();
  if (!healthy) {
    dialog.showErrorBox(
      "Backend not reachable",
      "ImageForge konnte das lokale Backend nicht starten. Bitte Python und requirements installieren."
    );
  }

  const win = new BrowserWindow({
    width: 1600,
    height: 920,
    minWidth: 1100,
    minHeight: 700,
    webPreferences: {
      contextIsolation: true,
      preload: path.join(__dirname, "preload.js"),
      nodeIntegration: false,
    },
  });

  const devServerUrl = process.env.VITE_DEV_SERVER_URL;
  if (devServerUrl) {
    await win.loadURL(devServerUrl);
  } else {
    await win.loadFile(path.join(__dirname, "..", "dist", "index.html"));
  }
}

ipcMain.handle("open-folder", async (_event, folderPath) => {
  await shell.openPath(folderPath);
});

ipcMain.handle("save-image", async (_event, sourcePath, defaultName) => {
  const result = await dialog.showSaveDialog({
    title: "Save image",
    defaultPath: defaultName,
    filters: [
      { name: "PNG", extensions: ["png"] },
      { name: "JPEG", extensions: ["jpg", "jpeg"] },
    ],
  });
  if (result.canceled || !result.filePath) {
    return false;
  }

  const ext = path.extname(result.filePath).toLowerCase();
  if (ext === ".jpg" || ext === ".jpeg") {
    const img = nativeImage.createFromPath(sourcePath);
    fs.writeFileSync(result.filePath, img.toJPEG(92));
  } else {
    fs.copyFileSync(sourcePath, result.filePath);
  }
  return true;
});

ipcMain.handle("show-error", async (_event, title, message) => {
  await dialog.showMessageBox({
    type: "error",
    title,
    message,
  });
});

ipcMain.handle("pick-image", async () => {
  const result = await dialog.showOpenDialog({
    title: "Select source image",
    properties: ["openFile"],
    filters: [{ name: "Images", extensions: ["png", "jpg", "jpeg", "webp", "bmp"] }],
  });
  if (result.canceled || result.filePaths.length === 0) {
    return null;
  }
  return result.filePaths[0];
});

ipcMain.handle("read-image-data-url", async (_event, sourcePath) => {
  try {
    const root = projectRoot();
    const outputRoot = path.resolve(root, "output");
    const resolved = path.resolve(String(sourcePath || ""));
    if (resolved !== outputRoot && !resolved.startsWith(`${outputRoot}${path.sep}`)) {
      return null;
    }
    if (!fs.existsSync(resolved)) {
      return null;
    }
    const ext = path.extname(resolved).toLowerCase();
    const mimeMap = {
      ".png": "image/png",
      ".jpg": "image/jpeg",
      ".jpeg": "image/jpeg",
      ".webp": "image/webp",
      ".bmp": "image/bmp",
    };
    const mime = mimeMap[ext] || "application/octet-stream";
    const data = fs.readFileSync(resolved);
    return `data:${mime};base64,${data.toString("base64")}`;
  } catch {
    return null;
  }
});

app.whenReady().then(createWindow);

app.on("window-all-closed", () => {
  if (backendProcess && !backendProcess.killed) {
    backendProcess.kill();
  }
  if (process.platform !== "darwin") {
    app.quit();
  }
});