feat:增加非图片格式的文件处理
This commit is contained in:
49
app.js
49
app.js
@@ -9,6 +9,35 @@ const {sendResponse, isSafePath, processImage} = require("./utils");
|
|||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
const IMAGE_BASE_DIR = process.env.IMAGE_BASE_DIR || '/app/images';
|
const IMAGE_BASE_DIR = process.env.IMAGE_BASE_DIR || '/app/images';
|
||||||
|
|
||||||
|
// 定义支持的图片扩展名
|
||||||
|
const IMAGE_EXTENSIONS = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg', 'tiff', 'tif']);
|
||||||
|
|
||||||
|
// MIME 类型映射
|
||||||
|
const MIME_TYPES = {
|
||||||
|
'jpg': 'image/jpeg',
|
||||||
|
'jpeg': 'image/jpeg',
|
||||||
|
'png': 'image/png',
|
||||||
|
'gif': 'image/gif',
|
||||||
|
'webp': 'image/webp',
|
||||||
|
'bmp': 'image/bmp',
|
||||||
|
'svg': 'image/svg+xml',
|
||||||
|
'tiff': 'image/tiff',
|
||||||
|
'tif': 'image/tiff',
|
||||||
|
'mp4': 'video/mp4',
|
||||||
|
'webm': 'video/webm',
|
||||||
|
'ogg': 'video/ogg',
|
||||||
|
'avi': 'video/x-msvideo',
|
||||||
|
'mov': 'video/quicktime',
|
||||||
|
'mp3': 'audio/mpeg',
|
||||||
|
'wav': 'audio/wav',
|
||||||
|
'pdf': 'application/pdf',
|
||||||
|
'txt': 'text/plain',
|
||||||
|
'html': 'text/html',
|
||||||
|
'css': 'text/css',
|
||||||
|
'js': 'application/javascript',
|
||||||
|
'json': 'application/json'
|
||||||
|
};
|
||||||
|
|
||||||
const server = http.createServer(async (req, res) => {
|
const server = http.createServer(async (req, res) => {
|
||||||
if (req.method !== 'GET') {
|
if (req.method !== 'GET') {
|
||||||
return sendResponse(res, 405, '仅支持GET请求');
|
return sendResponse(res, 405, '仅支持GET请求');
|
||||||
@@ -33,15 +62,23 @@ const server = http.createServer(async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(fullImagePath)) {
|
if (!fs.existsSync(fullImagePath)) {
|
||||||
return sendResponse(res, 404, `图片不存在:${requestedFilePath}`);
|
return sendResponse(res, 404, `文件不存在:${requestedFilePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果没有压缩参数,直接返回原图
|
// 获取文件扩展名
|
||||||
if (!query.q && !query.w && !query.width && !query.h && !query.height) {
|
const ext = path.extname(fullImagePath).toLowerCase().slice(1);
|
||||||
|
const isImage = IMAGE_EXTENSIONS.has(ext);
|
||||||
|
const hasImageParams = query.q || query.w || query.width || query.h || query.height;
|
||||||
|
|
||||||
|
// 如果不是图片文件,或者图片文件但没有处理参数,直接返回原文件
|
||||||
|
if (!isImage || !hasImageParams) {
|
||||||
const fileStream = fs.createReadStream(fullImagePath);
|
const fileStream = fs.createReadStream(fullImagePath);
|
||||||
const ext = path.extname(fullImagePath).slice(1);
|
|
||||||
res.writeHead(200, {'Content-Type': `image/${ext}`});
|
// 设置正确的 Content-Type
|
||||||
// 流式传输文件(适合大文件)
|
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
|
||||||
|
res.writeHead(200, {'Content-Type': contentType});
|
||||||
|
|
||||||
|
// 流式传输文件
|
||||||
fileStream.pipe(res);
|
fileStream.pipe(res);
|
||||||
fileStream.on('error', (err) => {
|
fileStream.on('error', (err) => {
|
||||||
sendResponse(res, 500, `读取文件失败:${err.message}`);
|
sendResponse(res, 500, `读取文件失败:${err.message}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user