feat: add publish app

This commit is contained in:
2024-10-07 22:28:21 +08:00
parent 8beb65e637
commit 0fc580aa38
17 changed files with 1393 additions and 722 deletions

View File

@@ -1,11 +1,11 @@
import { useFileStore } from '@abearxiong/use-file-store';
import http from 'http';
import fs from 'fs';
import fs, { rm } from 'fs';
import path from 'path';
import { IncomingForm } from 'formidable';
import { checkToken } from '@abearxiong/auth';
import { useConfig } from '@abearxiong/use-config';
import { minioClient } from '@/app.ts';
import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts';
import { User } from '@/models/user.ts';
@@ -83,6 +83,8 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
});
}
if (req.method === 'POST' && req.url === '/api/app/upload') {
if (res.headersSent) return; // 如果响应已发送,不再处理
res.writeHead(200, { 'Content-Type': 'application/json' });
const authroization = req.headers?.['authorization'] as string;
const error = (msg: string) => {
@@ -114,8 +116,23 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
res.end(error(`Upload error: ${err.message}`));
return;
}
console.log('fields', fields);
const clearFiles = () => {
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
uploadedFiles.forEach((file) => {
fs.unlinkSync(file.filepath);
});
};
const { appKey, version } = fields;
if (!appKey) {
res.end(error('appKey is required'));
clearFiles();
return;
}
if (!version) {
res.end(error('version is required'));
clearFiles();
return;
}
// 逐个处理每个上传的文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
const uploadResults = [];
@@ -139,12 +156,25 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
});
fs.unlinkSync(tempPath); // 删除临时文件
}
// 修改header
// res.writeHead(200, { 'Content-Type': 'text/plain' });
const data = {
code: 200,
data: uploadResults,
const r = await app.call({
path: 'app',
key: 'uploadFiles',
payload: {
token: token,
data: {
appKey,
version,
files: uploadResults,
},
},
});
const data: any = {
code: r.code,
data: r.body,
};
if (r.message) {
data.message = r.message;
}
res.end(JSON.stringify(data));
});
}