111
This commit is contained in:
parent
41ad8b7267
commit
1ffa3f36b0
17
index.js
17
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 } = apiData.data;
|
const { url: realUrl, cloudtype, expiration, path } = apiData.data;
|
||||||
const data = { realUrl, cloudtype, expiration: expiration * 1000 };
|
const data = { realUrl, cloudtype, expiration: expiration * 1000, path };
|
||||||
|
|
||||||
if (expiration > 0) {
|
if (expiration > 0) {
|
||||||
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
||||||
@ -122,9 +122,17 @@ 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
|
||||||
|
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,
|
...realRes.headers,
|
||||||
|
'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
|
||||||
'Cloud-Type': data.cloudtype,
|
'Cloud-Type': data.cloudtype,
|
||||||
'Cloud-Expiration': data.expiration,
|
'Cloud-Expiration': data.expiration,
|
||||||
});
|
});
|
||||||
@ -158,9 +166,12 @@ 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');
|
||||||
|
|
||||||
readStream.on('open', () => {
|
readStream.on('open', () => {
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'Content-Type': '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,
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user