forked from miao-moe/QZMusic_PC
feat: 支持不自动将网络资源(音频)缓存到本地
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -22,3 +22,5 @@ dist-ssr
|
|||||||
*.njsproj
|
*.njsproj
|
||||||
*.sln
|
*.sln
|
||||||
*.sw?
|
*.sw?
|
||||||
|
core/mpv.exe
|
||||||
|
.gitignore
|
||||||
|
|||||||
@@ -357,9 +357,6 @@ async function proxyAndCache(req, res, targetUrl, cacheFilePath) {
|
|||||||
const range = parseRangeHeader(requestedRange, downloadState.totalSize);
|
const range = parseRangeHeader(requestedRange, downloadState.totalSize);
|
||||||
if (range && range.end < downloadState.currentSize) {
|
if (range && range.end < downloadState.currentSize) {
|
||||||
console.log(`[Proxy] Serving range from in-progress cache`);
|
console.log(`[Proxy] Serving range from in-progress cache`);
|
||||||
({
|
|
||||||
totalSize: downloadState.totalSize
|
|
||||||
});
|
|
||||||
const { start, end } = range;
|
const { start, end } = range;
|
||||||
const chunksize = end - start + 1;
|
const chunksize = end - start + 1;
|
||||||
res.writeHead(206, {
|
res.writeHead(206, {
|
||||||
@@ -487,7 +484,21 @@ async function proxyAndCache(req, res, targetUrl, cacheFilePath) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function startProxyServer() {
|
let persistCacheEnabled = true;
|
||||||
|
function cleanupCache() {
|
||||||
|
if (!persistCacheEnabled && CACHE_DIR && fs.existsSync(CACHE_DIR)) {
|
||||||
|
console.log(`[Proxy] Cleaning up cache directory: ${CACHE_DIR}`);
|
||||||
|
try {
|
||||||
|
fs.rmSync(CACHE_DIR, { recursive: true, force: true });
|
||||||
|
console.log("[Proxy] Cache cleanup complete");
|
||||||
|
} catch (e) {
|
||||||
|
console.error("[Proxy] Failed to cleanup cache:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function startProxyServer(persistCache = true) {
|
||||||
|
persistCacheEnabled = persistCache;
|
||||||
|
console.log(`[Proxy] Persist cache: ${persistCache}`);
|
||||||
const server = http.createServer(async (req, res) => {
|
const server = http.createServer(async (req, res) => {
|
||||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS");
|
res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS");
|
||||||
@@ -642,6 +653,7 @@ app.on("window-all-closed", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
app.on("will-quit", () => {
|
app.on("will-quit", () => {
|
||||||
|
cleanupCache();
|
||||||
if (mpv) {
|
if (mpv) {
|
||||||
mpv.destroy();
|
mpv.destroy();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url'
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import { MpvController } from './mpvController'
|
import { MpvController } from './mpvController'
|
||||||
import { startProxyServer } from './proxyServer'
|
import { startProxyServer, cleanupCache } from './proxyServer'
|
||||||
import { PluginSystem } from '../src/main/pluginSystem.ts'
|
import { PluginSystem } from '../src/main/pluginSystem.ts'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const require = createRequire(import.meta.url)
|
const require = createRequire(import.meta.url)
|
||||||
@@ -99,6 +99,7 @@ app.on('window-all-closed', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.on('will-quit', () => {
|
app.on('will-quit', () => {
|
||||||
|
cleanupCache()
|
||||||
if (mpv) {
|
if (mpv) {
|
||||||
mpv.destroy()
|
mpv.destroy()
|
||||||
}
|
}
|
||||||
@@ -132,6 +133,7 @@ app.on('activate', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Test
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
// Ensure plugins directory exists
|
// Ensure plugins directory exists
|
||||||
const pluginsPath = path.join(app.getPath('userData'), 'plugins')
|
const pluginsPath = path.join(app.getPath('userData'), 'plugins')
|
||||||
|
|||||||
@@ -397,7 +397,24 @@ async function proxyAndCache(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startProxyServer() {
|
let persistCacheEnabled = true;
|
||||||
|
|
||||||
|
export function cleanupCache() {
|
||||||
|
if (!persistCacheEnabled && CACHE_DIR && fs.existsSync(CACHE_DIR)) {
|
||||||
|
console.log(`[Proxy] Cleaning up cache directory: ${CACHE_DIR}`);
|
||||||
|
try {
|
||||||
|
fs.rmSync(CACHE_DIR, { recursive: true, force: true });
|
||||||
|
console.log('[Proxy] Cache cleanup complete');
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[Proxy] Failed to cleanup cache:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function startProxyServer(persistCache: boolean = true) {
|
||||||
|
persistCacheEnabled = persistCache;
|
||||||
|
console.log(`[Proxy] Persist cache: ${persistCache}`);
|
||||||
|
|
||||||
const server = http.createServer(async (req, res) => {
|
const server = http.createServer(async (req, res) => {
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
res.setHeader('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS');
|
res.setHeader('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS');
|
||||||
|
|||||||
Reference in New Issue
Block a user