feat: 新增app管理和文件管理
This commit is contained in:
179
src/lib/upload.ts
Normal file
179
src/lib/upload.ts
Normal file
@@ -0,0 +1,179 @@
|
||||
import { useFileStore } from '@abearxiong/use-file-store';
|
||||
import http from 'http';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { IncomingForm } from 'formidable';
|
||||
import { checkToken } from '@abearxiong/auth';
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
const { tokenSecret } = useConfig<{ tokenSecret: string }>();
|
||||
const filePath = useFileStore('upload');
|
||||
// curl -X POST http://localhost:4000/api/upload -F "file=@readme.md"
|
||||
// curl -X POST http://localhost:4000/api/upload \
|
||||
// -F "file=@readme.md" \
|
||||
// -F "file=@types/index.d.ts" \
|
||||
// -F "description=This is a test upload" \
|
||||
// -F "username=testuser"
|
||||
|
||||
export const uploadMiddleware = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
if (req.method === 'GET' && req.url === '/api/upload') {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('Upload API is ready');
|
||||
return;
|
||||
}
|
||||
if (false && req.method === 'POST' && req.url === '/api/upload') {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
|
||||
// 检查 Content-Type 是否为 multipart/form-data
|
||||
const contentType = req.headers['content-type'];
|
||||
if (!contentType || !contentType.startsWith('multipart/form-data')) {
|
||||
res.end('Invalid content type, expecting multipart/form-data');
|
||||
return;
|
||||
}
|
||||
// 提取 boundary (边界) 标识
|
||||
const boundary = contentType.split('boundary=')[1];
|
||||
if (!boundary) {
|
||||
res.end('Invalid multipart/form-data format');
|
||||
return;
|
||||
}
|
||||
|
||||
// 将接收到的所有数据存入临时数组中
|
||||
let rawData = Buffer.alloc(0);
|
||||
req.on('data', (chunk) => {
|
||||
rawData = Buffer.concat([rawData, chunk]);
|
||||
});
|
||||
|
||||
req.on('end', () => {
|
||||
// 解析所有文件部分
|
||||
const parts = parseMultipartData(rawData, boundary);
|
||||
|
||||
// 存储上传文件结果
|
||||
const uploadResults = [];
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const part = parts[i];
|
||||
if (part.filename) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.mkdirSync(filePath, { recursive: true });
|
||||
}
|
||||
const tempFilePath = path.join(filePath, part.filename);
|
||||
fs.writeFileSync(tempFilePath, part.data);
|
||||
uploadResults.push(`File ${part.filename} uploaded successfully.`);
|
||||
// 上传到 MinIO
|
||||
// minioClient.fPutObject(bucketName, part.filename, tempFilePath, {}, (err, etag) => {
|
||||
// fs.unlinkSync(tempFilePath); // 删除临时文件
|
||||
// if (err) {
|
||||
// uploadResults.push(`Upload error for ${part.filename}: ${err.message}`);
|
||||
// } else {
|
||||
// uploadResults.push(`File ${part.filename} uploaded successfully. ETag: ${etag}`);
|
||||
// }
|
||||
|
||||
// // 如果所有文件都处理完毕,返回结果
|
||||
// if (uploadResults.length === parts.length) {
|
||||
// res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
// res.end(uploadResults.join('\n'));
|
||||
// }
|
||||
// });
|
||||
}
|
||||
}
|
||||
res.end(uploadResults.join('\n'));
|
||||
});
|
||||
}
|
||||
if (req.method === 'POST' && req.url === '/api/upload') {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
const authroization = req.headers?.['Authorization'] as string;
|
||||
if (!authroization) {
|
||||
res.statusCode = 401;
|
||||
res.end('Invalid authorization');
|
||||
return;
|
||||
}
|
||||
const token = authroization.split(' ')[1];
|
||||
const tokenUser = await checkToken(token, tokenSecret);
|
||||
if (!tokenUser) {
|
||||
res.statusCode = 401;
|
||||
res.end('Invalid token');
|
||||
return;
|
||||
}
|
||||
//
|
||||
// 使用 formidable 解析 multipart/form-data
|
||||
const form = new IncomingForm({
|
||||
multiples: true, // 支持多文件上传
|
||||
uploadDir: filePath, // 上传文件存储目录
|
||||
});
|
||||
// 解析上传的文件
|
||||
form.parse(req, (err, fields, files) => {
|
||||
if (err) {
|
||||
res.end(`Upload error: ${err.message}`);
|
||||
return;
|
||||
}
|
||||
console.log('fields', fields);
|
||||
// 逐个处理每个上传的文件
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
const uploadResults = [];
|
||||
|
||||
uploadedFiles.forEach((file) => {
|
||||
// @ts-ignore
|
||||
const tempPath = file.filepath; // 文件上传时的临时路径
|
||||
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
|
||||
uploadResults.push(`File ${relativePath} uploaded successfully. ${tempPath}`);
|
||||
// 上传到 MinIO 并保留文件夹结构
|
||||
// minioClient.fPutObject(bucketName, relativePath, tempPath, {}, (err, etag) => {
|
||||
// fs.unlinkSync(tempPath); // 删除临时文件
|
||||
|
||||
// if (err) {
|
||||
// uploadResults.push(`Upload error for ${relativePath}: ${err.message}`);
|
||||
// } else {
|
||||
// uploadResults.push(`File ${relativePath} uploaded successfully. ETag: ${etag}`);
|
||||
// }
|
||||
|
||||
// // 如果所有文件都处理完毕,返回结果
|
||||
// if (uploadResults.length === uploadedFiles.length) {
|
||||
// res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
// res.end(uploadResults.join('\n'));
|
||||
// }
|
||||
// });
|
||||
});
|
||||
res.end(uploadResults.join('\n'));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 解析 multipart/form-data 格式数据,提取各个字段和文件内容
|
||||
* @param {Buffer} buffer - 完整的 HTTP 请求体数据
|
||||
* @param {string} boundary - multipart/form-data 的 boundary 标识符
|
||||
* @returns {Array} 返回包含各个部分数据的数组
|
||||
*/
|
||||
function parseMultipartData(buffer, boundary) {
|
||||
const parts = [];
|
||||
const boundaryBuffer = Buffer.from(`--${boundary}`, 'utf-8');
|
||||
let start = buffer.indexOf(boundaryBuffer) + boundaryBuffer.length + 2; // Skip first boundary and \r\n
|
||||
|
||||
while (start < buffer.length) {
|
||||
// 查找下一个 boundary 的位置
|
||||
const end = buffer.indexOf(boundaryBuffer, start) - 2; // Subtract 2 to remove trailing \r\n
|
||||
if (end <= start) break;
|
||||
|
||||
// 提取单个 part 数据
|
||||
const part = buffer.slice(start, end);
|
||||
start = end + boundaryBuffer.length + 2; // Move start to next part
|
||||
|
||||
// 分割 part 头和内容
|
||||
const headerEndIndex = part.indexOf('\r\n\r\n');
|
||||
const headers = part.slice(0, headerEndIndex).toString();
|
||||
const content = part.slice(headerEndIndex + 4); // Skip \r\n\r\n
|
||||
|
||||
// 解析 headers 以获取字段名称和文件信息
|
||||
const nameMatch = headers.match(/name="([^"]+)"/);
|
||||
const filenameMatch = headers.match(/filename="([^"]+)"/);
|
||||
|
||||
const partData = {
|
||||
name: nameMatch ? nameMatch[1] : null,
|
||||
filename: filenameMatch ? filenameMatch[1] : null,
|
||||
data: content,
|
||||
};
|
||||
|
||||
parts.push(partData);
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
Reference in New Issue
Block a user