From 1ffa3f36b08a13a22ba116f4ba8f9c90fff6dcbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=B0=8F=E9=99=8C?= Date: Fri, 27 Sep 2024 18:36:23 +0800 Subject: [PATCH] 111 --- index.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 10bb09a..269b9c8 100644 --- a/index.js +++ b/index.js @@ -56,8 +56,8 @@ const server = http.createServer(async (req, res) => { try { const apiData = await fetchApiData(reqPath, sign); if (apiData.code === 200 && apiData.data && apiData.data.url) { - const { url: realUrl, cloudtype, expiration } = apiData.data; - const data = { realUrl, cloudtype, expiration: expiration * 1000 }; + const { url: realUrl, cloudtype, expiration, path } = apiData.data; + const data = { realUrl, cloudtype, expiration: expiration * 1000, path }; if (expiration > 0) { fs.writeFileSync(cacheMetaFile, JSON.stringify(data)); @@ -122,9 +122,17 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => { https.get(data.realUrl, { timeout: requestTimeout * 10 }, (realRes) => { // 创建临时缓存文件流 const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' }); + // 通过 data.path 判断是否是视频.mp4 + const isVideo = data.path.includes('.mp4'); + + // realRes.headers['content-type'] 有的话 去掉 + if (realRes.headers['content-type']) { + delete realRes.headers['content-type']; + } res.writeHead(realRes.statusCode, { ...realRes.headers, + 'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream', 'Cloud-Type': data.cloudtype, 'Cloud-Expiration': data.expiration, }); @@ -158,9 +166,12 @@ const serveFromCache = (cacheMetaFile, cacheContentFile, res) => { const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8')); const readStream = fs.createReadStream(cacheContentFile); + // 判断是否是视频 + const isVideo = cacheData.path.includes('.mp4'); + readStream.on('open', () => { res.writeHead(200, { - 'Content-Type': 'application/octet-stream', + 'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream', 'Cloud-Type': cacheData.cloudtype, 'Cloud-Expiration': cacheData.expiration, });