163 lines
4.9 KiB
JavaScript
163 lines
4.9 KiB
JavaScript
const fs = require('fs')
|
|
const base = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/'
|
|
function patch(file, pairs) {
|
|
const p = base + file
|
|
let c = fs.readFileSync(p, 'utf8')
|
|
for (const [from, to] of pairs) {
|
|
if (!c.includes(from)) {
|
|
console.log('MISS in', file, '::', from.slice(0, 70).replace(/\n/g, '\\n'))
|
|
continue
|
|
}
|
|
c = c.replace(from, to)
|
|
}
|
|
fs.writeFileSync(p, c, 'utf8')
|
|
}
|
|
|
|
// main:本地 HTTP 音乐服务(Chromium 对自定义协议的媒体加载会拒绝,HTTP 才稳)
|
|
patch('main/index.ts', [
|
|
[
|
|
"import { shell, dialog } from 'electron'",
|
|
"import { shell, dialog } from 'electron'\nimport * as http from 'node:http'"
|
|
],
|
|
[
|
|
`function createMusicWindow(): void {`,
|
|
`let musicServerPort = 0
|
|
|
|
function startMusicServer(): void {
|
|
const server = http.createServer((req, res) => {
|
|
try {
|
|
const dir = readMusicDir()
|
|
if (!dir) {
|
|
res.writeHead(404)
|
|
res.end()
|
|
return
|
|
}
|
|
const rel = decodeURIComponent((req.url ?? '/').split('?')[0]).replace(/^\\/+/, '')
|
|
const abs = path.join(dir, rel)
|
|
if (!isInside(dir, abs) || !fs.existsSync(abs) || !fs.statSync(abs).isFile()) {
|
|
res.writeHead(404)
|
|
res.end()
|
|
return
|
|
}
|
|
const ext = path.extname(abs).toLowerCase()
|
|
const mime =
|
|
ext === '.mp3'
|
|
? 'audio/mpeg'
|
|
: ext === '.flac'
|
|
? 'audio/flac'
|
|
: ext === '.wav'
|
|
? 'audio/wav'
|
|
: ext === '.ogg'
|
|
? 'audio/ogg'
|
|
: ext === '.m4a' || ext === '.aac'
|
|
? 'audio/mp4'
|
|
: 'application/octet-stream'
|
|
const size = fs.statSync(abs).size
|
|
const range = req.headers.range
|
|
if (range) {
|
|
const m = /bytes=(\\d*)-(\\d*)/.exec(range)
|
|
const start = m && m[1] ? Number(m[1]) : 0
|
|
const end = m && m[2] ? Math.min(Number(m[2]), size - 1) : size - 1
|
|
res.writeHead(206, {
|
|
'Content-Type': mime,
|
|
'Accept-Ranges': 'bytes',
|
|
'Content-Range': \`bytes \${start}-\${end}/\${size}\`,
|
|
'Content-Length': String(end - start + 1)
|
|
})
|
|
fs.createReadStream(abs, { start, end }).pipe(res)
|
|
} else {
|
|
res.writeHead(200, {
|
|
'Content-Type': mime,
|
|
'Accept-Ranges': 'bytes',
|
|
'Content-Length': String(size)
|
|
})
|
|
fs.createReadStream(abs).pipe(res)
|
|
}
|
|
} catch (err) {
|
|
console.error('[music server]', err)
|
|
try {
|
|
res.writeHead(500)
|
|
res.end()
|
|
} catch {
|
|
/* 忽略 */
|
|
}
|
|
}
|
|
})
|
|
server.listen(0, '127.0.0.1', () => {
|
|
const addr = server.address()
|
|
if (addr && typeof addr === 'object') {
|
|
musicServerPort = addr.port
|
|
console.log('[music server] http://127.0.0.1:' + musicServerPort)
|
|
}
|
|
})
|
|
}
|
|
|
|
function createMusicWindow(): void {`
|
|
],
|
|
[
|
|
` scheduleBarks()
|
|
scheduleSuggestions()
|
|
scheduleEvents()`,
|
|
` scheduleBarks()
|
|
scheduleSuggestions()
|
|
scheduleEvents()
|
|
startMusicServer()`
|
|
],
|
|
[
|
|
` ipcMain.handle('pet:music:list', () => scanMusic())`,
|
|
` ipcMain.handle('pet:music:list', () => scanMusic())
|
|
ipcMain.handle('pet:music:base', () => (musicServerPort ? 'http://127.0.0.1:' + musicServerPort + '/' : ''))`
|
|
]
|
|
])
|
|
|
|
// preload / api
|
|
patch('preload/index.ts', [
|
|
[
|
|
" listMusic: () => Promise<{ dir: string; tracks: Array<{ name: string; file: string }> }>",
|
|
" listMusic: () => Promise<{ dir: string; tracks: Array<{ name: string; file: string }> }>\n getMusicBase: () => Promise<string>"
|
|
],
|
|
[
|
|
" listMusic: () => ipcRenderer.invoke('pet:music:list'),",
|
|
" listMusic: () => ipcRenderer.invoke('pet:music:list'),\n getMusicBase: () => ipcRenderer.invoke('pet:music:base'),"
|
|
]
|
|
])
|
|
patch('renderer/src/api.ts', [
|
|
[
|
|
" listMusic: () => Promise<{ dir: string; tracks: Array<{ name: string; file: string }> }>",
|
|
" listMusic: () => Promise<{ dir: string; tracks: Array<{ name: string; file: string }> }>\n getMusicBase: () => Promise<string>"
|
|
]
|
|
])
|
|
|
|
// Music.tsx:用 HTTP 地址
|
|
patch('renderer/src/music/Music.tsx', [
|
|
[
|
|
" const [theme, setTheme] = useState('night')",
|
|
" const [theme, setTheme] = useState('night')\n const [base, setBase] = useState('')"
|
|
],
|
|
[
|
|
` void api()
|
|
?.listMusic()
|
|
.then((d) => {
|
|
setDir(d.dir)
|
|
setTracks(d.tracks)
|
|
})`,
|
|
` void api()?.getMusicBase().then(setBase)
|
|
void api()
|
|
?.listMusic()
|
|
.then((d) => {
|
|
setDir(d.dir)
|
|
setTracks(d.tracks)
|
|
})`
|
|
],
|
|
[
|
|
` src={'pet://music/' + encodeURIComponent(current.file)}`,
|
|
` src={
|
|
base
|
|
? base + current.file.split('/').map(encodeURIComponent).join('/')
|
|
: 'pet://music/' + current.file.split('/').map(encodeURIComponent).join('/')
|
|
}`
|
|
]
|
|
])
|
|
|
|
console.log('http music server patched')
|