关于login重构

This commit is contained in:
2025-03-21 20:41:01 +08:00
parent 0179fe73a3
commit 8053a3db64
28 changed files with 889 additions and 596 deletions

View File

@@ -7,8 +7,6 @@ import { useFileStore } from '@kevisual/use-config/file-store';
import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts';
import { hash } from 'crypto';
import { MicroAppUploadModel } from '@/routes/micro-app/models.ts';
const cacheFilePath = useFileStore('cache-file', { needExists: true });
router.post('/api/micro-app/upload', async (req, res) => {
@@ -119,70 +117,7 @@ router.post('/api/micro-app/upload', async (req, res) => {
});
});
router.get('/api/micro-app/download/:id', async (req, res) => {
const { id } = req.params;
if (!id) {
res.writeHead(200, { 'Content-Type': 'application/javascript; charset=utf-8' });
res.end(error('Key parameter is required'));
return;
}
const query = new URL(req.url || '', 'http://localhost');
const notNeedToken = query.searchParams.get('notNeedToken') || '';
const fileTitle = query.searchParams.get('title') || '';
if (res.headersSent) return; // 如果响应已发送,不再处理
let tokenUser;
if (!DEV_SERVER && !notNeedToken) {
const auth = await checkAuth(req, res);
tokenUser = auth.tokenUser;
if (!tokenUser) return;
}
let file: MicroAppUploadModel | null = null;
if (!DEV_SERVER) {
// file.uid !== tokenUser.id && res.end(error('No permission', 403));
// return;
}
if (fileTitle) {
file = await MicroAppUploadModel.findOne({
where: { title: fileTitle },
});
} else if (id) {
file = await MicroAppUploadModel.findByPk(id);
}
if (!file) {
res.end(error('File not found'));
return;
}
const objectName = file.data?.file?.path;
const fileName = file.data?.file?.name;
if (!objectName) {
res.end(error('File not found'));
return;
}
res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(fileName)}"`);
res.setHeader('app-key', file.data?.key || id);
res.writeHead(200, { 'Content-Type': 'application/octet-stream' });
try {
const stream = await minioClient.getObject(bucketName, objectName);
// 捕获流的错误,防止崩溃
stream.on('error', (err) => {
console.error('Error while streaming file:', err);
if (!res.headersSent) {
res.end(error('Error downloading file'));
}
});
stream.pipe(res).on('finish', () => {
console.log(`File download completed: ${id}`);
});
} catch (err) {
console.error('Error during download:', err);
if (!res.headersSent) {
res.end(error('Error downloading file'));
}
}
});
function parseIfJson(collection: any): any {
try {
return JSON.parse(collection);