1111
This commit is contained in:
parent
1ffa3f36b0
commit
15a9a009a3
25
index.js
25
index.js
@ -56,8 +56,8 @@ const server = http.createServer(async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const apiData = await fetchApiData(reqPath, sign);
|
const apiData = await fetchApiData(reqPath, sign);
|
||||||
if (apiData.code === 200 && apiData.data && apiData.data.url) {
|
if (apiData.code === 200 && apiData.data && apiData.data.url) {
|
||||||
const { url: realUrl, cloudtype, expiration, path } = apiData.data;
|
const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
|
||||||
const data = { realUrl, cloudtype, expiration: expiration * 1000, path };
|
const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid };
|
||||||
|
|
||||||
if (expiration > 0) {
|
if (expiration > 0) {
|
||||||
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
||||||
@ -120,21 +120,22 @@ const fetchApiData = (reqPath, sign) => {
|
|||||||
|
|
||||||
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
||||||
https.get(data.realUrl, { timeout: requestTimeout * 10 }, (realRes) => {
|
https.get(data.realUrl, { timeout: requestTimeout * 10 }, (realRes) => {
|
||||||
|
|
||||||
// 创建临时缓存文件流
|
// 创建临时缓存文件流
|
||||||
const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' });
|
const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' });
|
||||||
// 通过 data.path 判断是否是视频.mp4
|
// 通过 data.path 判断是否是视频.mp4
|
||||||
const isVideo = data.path.includes('.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, {
|
res.writeHead(realRes.statusCode, {
|
||||||
...realRes.headers,
|
...data.headers,
|
||||||
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
|
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
|
||||||
'Cloud-Type': data.cloudtype,
|
'Cloud-Type': data.cloudtype,
|
||||||
'Cloud-Expiration': data.expiration,
|
'Cloud-Expiration': data.expiration,
|
||||||
|
// 添加缓存, 浏览器缓存不再重复请求
|
||||||
|
'Cache-Control': 'public, max-age=31536000',
|
||||||
|
'Expires': new Date(Date.now() + 31536000000).toUTCString(),
|
||||||
|
'Last-Modified': new Date().toUTCString(),
|
||||||
|
'ETag': data.uniqid || ''
|
||||||
});
|
});
|
||||||
|
|
||||||
realRes.pipe(cacheStream);
|
realRes.pipe(cacheStream);
|
||||||
@ -165,15 +166,19 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
|||||||
const serveFromCache = (cacheMetaFile, cacheContentFile, res) => {
|
const serveFromCache = (cacheMetaFile, cacheContentFile, res) => {
|
||||||
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
|
const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
|
||||||
const readStream = fs.createReadStream(cacheContentFile);
|
const readStream = fs.createReadStream(cacheContentFile);
|
||||||
|
|
||||||
// 判断是否是视频
|
// 判断是否是视频
|
||||||
const isVideo = cacheData.path.includes('.mp4');
|
const isVideo = cacheData.path.includes('.mp4');
|
||||||
|
|
||||||
readStream.on('open', () => {
|
readStream.on('open', () => {
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
|
...cacheData.headers,
|
||||||
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
|
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
|
||||||
'Cloud-Type': cacheData.cloudtype,
|
'Cloud-Type': cacheData.cloudtype,
|
||||||
'Cloud-Expiration': cacheData.expiration,
|
'Cloud-Expiration': cacheData.expiration,
|
||||||
|
// 添加缓存, 浏览器缓存不再重复请求
|
||||||
|
'Cache-Control': 'public, max-age=31536000',
|
||||||
|
'Expires': new Date(Date.now() + 31536000000).toUTCString(),
|
||||||
|
'Last-Modified': new Date().toUTCString(),
|
||||||
|
'ETag': cacheData.uniqid || ''
|
||||||
});
|
});
|
||||||
readStream.pipe(res);
|
readStream.pipe(res);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user