forked from taskmasterpeace/Directors-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp-handlers.ts
More file actions
162 lines (133 loc) · 4.49 KB
/
Copy pathapp-handlers.ts
File metadata and controls
162 lines (133 loc) · 4.49 KB
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
import { app, ipcMain } from 'electron'
import path from 'path'
import fs from 'fs'
import { checkGPU } from '../gpu'
import { isPythonReady, downloadPythonEmbed } from '../python-setup'
import { getBackendHealthStatus, getBackendUrl, getAuthToken, startPythonBackend } from '../python-backend'
import { getMainWindow } from '../window'
import { getAnalyticsState, setAnalyticsEnabled, sendAnalyticsEvent } from '../analytics'
function getModelsPath(): string {
const modelsPath = path.join(app.getPath('userData'), 'models')
if (!fs.existsSync(modelsPath)) {
fs.mkdirSync(modelsPath, { recursive: true })
}
return modelsPath
}
function getSetupStatus(settingsPath: string): { needsSetup: boolean; needsLicense: boolean } {
if (!fs.existsSync(settingsPath)) {
return { needsSetup: true, needsLicense: true }
}
try {
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'))
return {
needsSetup: !settings.setupComplete,
needsLicense: !settings.licenseAccepted,
}
} catch {
return { needsSetup: true, needsLicense: true }
}
}
function markSetupComplete(settingsPath: string): void {
let settings: Record<string, unknown> = {}
try {
if (fs.existsSync(settingsPath)) {
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'))
}
} catch {
settings = {}
}
settings.setupComplete = true
settings.licenseAccepted = true
settings.licenseAcceptedDate = new Date().toISOString()
settings.setupDate = new Date().toISOString()
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2))
}
function markLicenseAccepted(settingsPath: string): void {
let settings: Record<string, unknown> = {}
try {
if (fs.existsSync(settingsPath)) {
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'))
}
} catch {
settings = {}
}
settings.licenseAccepted = true
settings.licenseAcceptedDate = new Date().toISOString()
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2))
}
export function registerAppHandlers(): void {
ipcMain.handle('get-backend', () => {
return { url: getBackendUrl() ?? '', token: getAuthToken() ?? '' }
})
ipcMain.handle('get-models-path', () => {
return getModelsPath()
})
ipcMain.handle('check-gpu', async () => {
return await checkGPU()
})
ipcMain.handle('get-app-info', () => {
return {
version: app.getVersion(),
isPackaged: app.isPackaged,
modelsPath: getModelsPath(),
userDataPath: app.getPath('userData'),
}
})
ipcMain.handle('get-downloads-path', () => {
return app.getPath('downloads')
})
ipcMain.handle('check-first-run', () => {
const settingsPath = path.join(app.getPath('userData'), 'app_state.json')
return getSetupStatus(settingsPath)
})
ipcMain.handle('accept-license', () => {
const settingsPath = path.join(app.getPath('userData'), 'app_state.json')
markLicenseAccepted(settingsPath)
return true
})
ipcMain.handle('complete-setup', () => {
const settingsPath = path.join(app.getPath('userData'), 'app_state.json')
markSetupComplete(settingsPath)
return true
})
ipcMain.handle('fetch-license-text', async () => {
const resp = await fetch('https://huggingface.co/Lightricks/LTX-2.3/raw/main/LICENSE')
if (!resp.ok) {
throw new Error(`Failed to fetch license (HTTP ${resp.status})`)
}
return await resp.text()
})
ipcMain.handle('get-notices-text', async () => {
const noticesPath = path.join(app.getAppPath(), 'NOTICES.md')
return fs.readFileSync(noticesPath, 'utf-8')
})
ipcMain.handle('get-resource-path', () => {
if (!app.isPackaged) {
return null
}
return process.resourcesPath
})
ipcMain.handle('check-python-ready', () => {
return isPythonReady()
})
ipcMain.handle('start-python-setup', async () => {
await downloadPythonEmbed((progress) => {
getMainWindow()?.webContents.send('python-setup-progress', progress)
})
})
ipcMain.handle('start-python-backend', async () => {
await startPythonBackend()
})
ipcMain.handle('get-backend-health-status', () => {
return getBackendHealthStatus()
})
ipcMain.handle('get-analytics-state', () => {
return getAnalyticsState()
})
ipcMain.handle('set-analytics-enabled', (_event, enabled: boolean) => {
setAnalyticsEnabled(enabled)
})
ipcMain.handle('send-analytics-event', async (_event, eventName: string, extraDetails?: Record<string, unknown> | null) => {
await sendAnalyticsEvent(eventName, extraDetails)
})
}