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
|
import { app, BrowserWindow, protocol, session } from "electron";
import { stat } from "node:fs/promises";
import { createReadStream } from "node:fs";
import path from "node:path";
import { Readable } from "node:stream";
import { cancelAllExports, registerExportHandlers } from "./export.js";
const contentTypes: Record<string, string> = {
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".json": "application/json; charset=utf-8",
".wasm": "application/wasm",
".png": "image/png",
".jpg": "image/jpeg",
".svg": "image/svg+xml",
".ico": "image/x-icon",
".woff2": "font/woff2",
".mp4": "video/mp4",
".webm": "video/webm",
".txt": "text/plain; charset=utf-8",
".xml": "application/xml; charset=utf-8",
".map": "application/json; charset=utf-8",
};
protocol.registerSchemesAsPrivileged([
{
scheme: "opencut",
privileges: {
standard: true,
secure: true,
supportFetchAPI: true,
stream: true,
bypassCSP: false,
},
},
]);
function registerWebProtocol(): void {
const webDir = app.isPackaged
? path.join(process.resourcesPath, "web")
: path.join(__dirname, "..", "web");
protocol.handle("opencut", async (request) => {
const url = new URL(request.url);
const pathname = decodeURIComponent(url.pathname);
const resolved = path.normalize(path.join(webDir, pathname));
if (resolved !== webDir && !resolved.startsWith(`${webDir}${path.sep}`)) {
return new Response("Forbidden", { status: 403 });
}
let filePath = resolved;
let info = await stat(filePath).catch(() => null);
if (info?.isDirectory() || (!info && path.extname(filePath) === "")) {
filePath = path.join(resolved, "index.html");
info = await stat(filePath).catch(() => null);
}
if (!info?.isFile()) {
return new Response("Not found", { status: 404 });
}
const contentType = contentTypes[path.extname(filePath).toLowerCase()];
if (!contentType) {
return new Response("Not found", { status: 404 });
}
const stream = Readable.toWeb(createReadStream(filePath));
return new Response(stream as ReadableStream, {
headers: { "Content-Type": contentType },
});
});
}
function registerMediaPermissions(): void {
session.defaultSession.setPermissionRequestHandler(
(webContents, permission, callback) => {
if (permission !== "media") {
callback(false);
return;
}
const currentUrl = webContents.getURL();
if (!currentUrl) {
callback(false);
return;
}
const url = new URL(currentUrl);
if (url.protocol === "opencut:") {
callback(true);
return;
}
const devServerUrl = process.env.OPENCUT_DEV_SERVER_URL;
if (devServerUrl) {
const devUrl = new URL(devServerUrl);
callback(url.origin === devUrl.origin);
return;
}
callback(false);
},
);
}
function createWindow(): void {
const win = new BrowserWindow({
width: 1440,
height: 900,
minWidth: 960,
minHeight: 600,
backgroundColor: "#0a0a0a",
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
sandbox: false,
},
});
const devServerUrl = process.env.OPENCUT_DEV_SERVER_URL;
if (devServerUrl) {
win.loadURL(devServerUrl);
return;
}
win.loadURL("opencut://app/");
}
const gotSingleInstanceLock = app.requestSingleInstanceLock();
if (!gotSingleInstanceLock) {
app.quit();
} else {
app.on("second-instance", () => {
const [win] = BrowserWindow.getAllWindows();
if (!win) return;
if (win.isMinimized()) win.restore();
win.focus();
});
app.whenReady().then(() => {
registerWebProtocol();
registerMediaPermissions();
registerExportHandlers();
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
}
app.on("window-all-closed", () => {
app.quit();
});
app.on("will-quit", () => {
cancelAllExports();
});
|