This commit is contained in:
蒋小陌 2024-10-15 11:31:12 +08:00
parent bf6cebe31a
commit 930236657a

View File

@ -195,13 +195,13 @@ const fetchApiData = (reqPath, sign) => {
// 从真实 URL 获取数据并写入缓存
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
https.get(data.realUrl, { timeout: requestTimeout * 10,rejectUnauthorized: false }, (realRes) => {
https.get(data.realUrl, { timeout: requestTimeout * 10, rejectUnauthorized: false }, (realRes) => {
const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' });
let isVideo = data.path && typeof data.path === 'string' && data.path.includes('.mp4');
data.headers['content-length'] = realRes.headers['content-length'];
res.writeHead(realRes.statusCode, {
...data.headers,
const defaultHeaders = {
'Cloud-Type': data.cloudtype,
'Cloud-Expiration': data.expiration,
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
@ -212,7 +212,8 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
'Connection': 'keep-alive',
'Date': new Date().toUTCString(),
'Last-Modified': new Date().toUTCString(),
});
};
res.writeHead(realRes.statusCode, Object.assign({}, defaultHeaders, data.headers));
realRes.pipe(cacheStream);
realRes.pipe(res);
@ -250,8 +251,8 @@ const serveFromCache = (cacheMetaFile, cacheContentFile, res) => {
cacheData.headers['content-length'] = fs.statSync(cacheContentFile).size;
readStream.on('open', () => {
res.writeHead(200, {
...cacheData.headers,
const defaultHeaders = {
'Cloud-Type': cacheData.cloudtype,
'Cloud-Expiration': cacheData.expiration,
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
@ -262,7 +263,9 @@ const serveFromCache = (cacheMetaFile, cacheContentFile, res) => {
'Connection': 'keep-alive',
'Date': new Date().toUTCString(),
'Last-Modified': new Date().toUTCString(),
});
}
res.writeHead(200, Object.assign({}, defaultHeaders, cacheData.headers));
readStream.pipe(res);
});