fix: 兼容获取appKey和version

This commit is contained in:
xion 2024-10-10 01:16:51 +08:00
parent 44d8a831c2
commit 15e8c9eea9
4 changed files with 63 additions and 6 deletions

View File

@ -110,6 +110,7 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
multiples: true, // 支持多文件上传 multiples: true, // 支持多文件上传
uploadDir: filePath, // 上传文件存储目录 uploadDir: filePath, // 上传文件存储目录
}); });
// 解析上传的文件 // 解析上传的文件
form.parse(req, async (err, fields, files) => { form.parse(req, async (err, fields, files) => {
if (err) { if (err) {
@ -122,7 +123,18 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
fs.unlinkSync(file.filepath); fs.unlinkSync(file.filepath);
}); });
}; };
const { appKey, version } = fields; let appKey, version;
const { appKey: _appKey, version: _version } = fields;
if (Array.isArray(_appKey)) {
appKey = _appKey?.[0];
} else {
appKey = _appKey;
}
if (Array.isArray(_version)) {
version = _version?.[0];
} else {
version = _version;
}
if (!appKey) { if (!appKey) {
res.end(error('appKey is required')); res.end(error('appKey is required'));
clearFiles(); clearFiles();
@ -133,6 +145,8 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
clearFiles(); clearFiles();
return; return;
} }
console.log('Appkey', appKey, version);
// 逐个处理每个上传的文件 // 逐个处理每个上传的文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
const uploadResults = []; const uploadResults = [];

View File

@ -148,6 +148,19 @@ app
if (!files || !files.length) { if (!files || !files.length) {
throw new CustomError('files is required'); throw new CustomError('files is required');
} }
let am = await AppModel.findOne({ where: { key: appKey, uid: tokenUser.id } });
if (!am) {
am = await AppModel.create({
user: tokenUser.username,
key: appKey,
uid: tokenUser.id,
version: '0.0.0',
title: appKey,
data: {
files: [],
},
});
}
let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } }); let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
if (!app) { if (!app) {
// throw new CustomError('app not found'); // throw new CustomError('app not found');

View File

@ -114,6 +114,7 @@ app
const list = await AppListModel.findAll({ where: { key: am.key, uid: tokenUser.id } }); const list = await AppListModel.findAll({ where: { key: am.key, uid: tokenUser.id } });
await am.destroy({ force: true }); await am.destroy({ force: true });
await Promise.all(list.map((item) => item.destroy({ force: true }))); await Promise.all(list.map((item) => item.destroy({ force: true })));
ctx.body = 'success';
return ctx; return ctx;
}) })
.addTo(app); .addTo(app);

View File

@ -3,17 +3,46 @@ import path from 'path';
export const getContentType = (filePath: string) => { export const getContentType = (filePath: string) => {
const extname = path.extname(filePath); const extname = path.extname(filePath);
const contentType = { const contentType = {
'.html': 'text/html', '.html': 'text/html; charset=utf-8',
'.js': 'text/javascript', '.js': 'text/javascript; charset=utf-8',
'.css': 'text/css', '.css': 'text/css; charset=utf-8',
'.txt': 'text/plain', '.txt': 'text/plain; charset=utf-8',
'.json': 'application/json', '.json': 'application/json; charset=utf-8',
'.png': 'image/png', '.png': 'image/png',
'.jpg': 'image/jpg', '.jpg': 'image/jpg',
'.gif': 'image/gif', '.gif': 'image/gif',
'.svg': 'image/svg+xml', '.svg': 'image/svg+xml',
'.wav': 'audio/wav', '.wav': 'audio/wav',
'.mp4': 'video/mp4', '.mp4': 'video/mp4',
'.md': 'text/markdown; charset=utf-8', // utf-8配置
'.ico': 'image/x-icon', // Favicon 图标
'.webp': 'image/webp', // WebP 图像格式
'.webm': 'video/webm', // WebM 视频格式
'.ogg': 'audio/ogg', // Ogg 音频格式
'.mp3': 'audio/mpeg', // MP3 音频格式
'.m4a': 'audio/mp4', // M4A 音频格式
'.m3u8': 'application/vnd.apple.mpegurl', // HLS 播放列表
'.ts': 'video/mp2t', // MPEG Transport Stream
'.pdf': 'application/pdf', // PDF 文档
'.doc': 'application/msword', // Word 文档
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // Word 文档 (新版)
'.ppt': 'application/vnd.ms-powerpoint', // PowerPoint 演示文稿
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', // PowerPoint (新版)
'.xls': 'application/vnd.ms-excel', // Excel 表格
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // Excel 表格 (新版)
'.csv': 'text/csv; charset=utf-8', // CSV 文件
'.xml': 'application/xml; charset=utf-8', // XML 文件
'.rtf': 'application/rtf', // RTF 文本文件
'.eot': 'application/vnd.ms-fontobject', // Embedded OpenType 字体
'.ttf': 'font/ttf', // TrueType 字体
'.woff': 'font/woff', // Web Open Font Format 1.0
'.woff2': 'font/woff2', // Web Open Font Format 2.0
'.otf': 'font/otf', // OpenType 字体
'.wasm': 'application/wasm', // WebAssembly 文件
'.pem': 'application/x-pem-file', // PEM 证书文件
'.crt': 'application/x-x509-ca-cert', // CRT 证书文件
'.yaml': 'application/x-yaml; charset=utf-8', // YAML 文件
'.yml': 'application/x-yaml; charset=utf-8', // YAML 文件(别名)
'.zip': 'application/octet-stream', '.zip': 'application/octet-stream',
}; };
return contentType[extname] || 'application/octet-stream'; return contentType[extname] || 'application/octet-stream';