refactor: 移除未使用的依赖和功能,优化代码结构
移除sharp模块及相关缩略图功能 删除webpack配置文件和安装脚本 清理未使用的依赖项 重构PHP服务端代码,优化缓存处理逻辑
This commit is contained in:
40
source.js
40
source.js
@@ -5,11 +5,10 @@ const querystring = require('querystring');
|
||||
const fs = require('fs');
|
||||
const pathModule = require('path');
|
||||
const crypto = require('crypto');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const CACHE_DIR_NAME = '.cache';
|
||||
const DEFAULT_PORT = 9001;
|
||||
const DEFAULT_API_ENDPOINT = 'http://183.6.121.121:9521/alist';
|
||||
const DEFAULT_API_ENDPOINT = 'http://183.6.121.121:9519/api';
|
||||
|
||||
const cacheDir = pathModule.join(__dirname, CACHE_DIR_NAME);
|
||||
const pathIndex = {};
|
||||
@@ -375,23 +374,6 @@ async function fetchApiData(reqPath, token, sign) {
|
||||
});
|
||||
}
|
||||
|
||||
// createThumbnail
|
||||
function createThumbnail(data, cacheContentFile) {
|
||||
const { path, thumb } = data;
|
||||
|
||||
const thumbCacheFile = pathModule.join(cacheDir, `thumb_${thumb.uniqid}.jpeg`);
|
||||
if (fs.existsSync(thumbCacheFile)) return thumbCacheFile;
|
||||
|
||||
const isVideo = path && typeof path === 'string' && path.includes('.mp4');
|
||||
if (isVideo || !thumb) return;
|
||||
const width = thumb.width && thumb.width > 0 ? thumb.width : undefined;
|
||||
const height = thumb.height && thumb.height > 0 ? thumb.height : undefined;
|
||||
if (!width) return;
|
||||
sharp(cacheContentFile).resize(width, height).toFile(thumbCacheFile);
|
||||
return thumbCacheFile;
|
||||
}
|
||||
|
||||
|
||||
// 从真实 URL 获取数据并写入缓存
|
||||
const REAL_URL_FETCH_TIMEOUT_MS = 0; // 0 means no timeout for the actual file download
|
||||
|
||||
@@ -457,10 +439,6 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, cacheMetaFi
|
||||
fs.renameSync(tempCacheContentFile, cacheContentFile);
|
||||
console.log(`Successfully cached: ${cacheContentFile}`);
|
||||
|
||||
// 生成缩略图
|
||||
if (data.thumb) {
|
||||
createThumbnail(data, cacheContentFile);
|
||||
}
|
||||
} catch (renameError) {
|
||||
console.error(`Error renaming temp cache file ${tempCacheContentFile} to ${cacheContentFile}:`, renameError);
|
||||
// If rename fails, try to remove the temp file to avoid clutter
|
||||
@@ -505,22 +483,6 @@ function serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res) {
|
||||
'Date': new Date().toUTCString(),
|
||||
'Last-Modified': (cacheData.headers && cacheData.headers['last-modified']) || new Date(fs.statSync(cacheMetaFile).mtime).toUTCString(),
|
||||
};
|
||||
if (cacheData.thumb) {
|
||||
var thumbCacheFile = createThumbnail(cacheData, cacheContentFile)
|
||||
if (thumbCacheFile && fs.existsSync(thumbCacheFile)) {
|
||||
cacheData.headers['content-length'] = fs.statSync(thumbCacheFile).size;
|
||||
const responseHeaders = {
|
||||
...baseHeaders,
|
||||
...(cacheData.headers || {}),
|
||||
'ETag': (cacheData.thumb.uniqid || cacheData.uniqid) + '_thumb',
|
||||
'Content-Type': 'image/jpeg',
|
||||
};
|
||||
res.writeHead(HTTP_STATUS.OK, responseHeaders);
|
||||
const thumbStream = fs.createReadStream(thumbCacheFile);
|
||||
thumbStream.pipe(res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
viewsInfo.increment('cacheCall');
|
||||
const readStream = fs.createReadStream(cacheContentFile);
|
||||
|
||||
Reference in New Issue
Block a user