temp: add eco
This commit is contained in:
@@ -9,6 +9,7 @@ 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 { MicroAppModel } from '@/routes/micro-app/models.ts';
|
||||
const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
||||
|
||||
router.post('/api/micro-app/upload', async (req, res) => {
|
||||
@@ -119,6 +120,61 @@ router.post('/api/micro-app/upload', async (req, res) => {
|
||||
res.end(JSON.stringify(data));
|
||||
});
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (res.headersSent) return; // 如果响应已发送,不再处理
|
||||
let tokenUser;
|
||||
if (!DEV_SERVER) {
|
||||
const auth = await checkAuth(req, res);
|
||||
tokenUser = auth.tokenUser;
|
||||
if (!tokenUser) return;
|
||||
}
|
||||
const file = await MicroAppModel.findByPk(id);
|
||||
if (!DEV_SERVER) {
|
||||
file.uid !== tokenUser.id && res.end(error('No permission', 403));
|
||||
return;
|
||||
}
|
||||
|
||||
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.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);
|
||||
|
||||
Reference in New Issue
Block a user