upload data change

This commit is contained in:
2025-03-29 20:56:03 +08:00
parent d8d78d184b
commit 80d16b5f76
5 changed files with 135 additions and 411 deletions

View File

@@ -23,6 +23,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
if (!tokenUser) return;
const url = new URL(req.url || '', 'http://localhost');
const share = !!url.searchParams.get('public');
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
// 使用 formidable 解析 multipart/form-data
const form = new IncomingForm({
multiples: false, // 改为单文件上传
@@ -133,34 +134,48 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
// Clean up the final file
fs.unlinkSync(finalFilePath);
// Notify the app
const r = await app.call({
path: 'app',
key: 'detect-version-list',
payload: {
token: token,
data: {
appKey,
version,
username,
if (!noCheckAppFiles) {
// Notify the app
const r = await app.call({
path: 'app',
key: 'detect-version-list',
payload: {
token: token,
data: {
appKey,
version,
username,
},
},
},
});
const downloadBase = '/api/s1/share';
const data: any = {
code: r.code,
data: {
app: r.body,
resource: `${downloadBase}/${minioPath}`,
},
};
if (r.message) {
data.message = r.message;
});
const downloadBase = '/api/s1/share';
const data: any = {
code: r.code,
data: {
app: r.body,
upload: [
{
name: relativePath,
path: `${downloadBase}/${minioPath}`,
},
],
},
};
if (r.message) {
data.message = r.message;
}
console.log('upload data', data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
message: 'Chunk uploaded successfully',
data: { chunkIndex, totalChunks },
}),
);
}
console.log('upload data', data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(

View File

@@ -20,8 +20,14 @@ router.get('/api/s1/resources/upload', async (req, res) => {
// /api/s1/resources/upload
router.post('/api/s1/resources/upload', async (req, res) => {
const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) return;
// 使用 formidable 解析 multipart/form-data
if (!tokenUser) {
res.end(error('Token is invalid.'));
return;
}
const url = new URL(req.url || '', 'http://localhost');
const share = !!url.searchParams.get('public');
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
// 使用 formi dable 解析 multipart/form-data
const form = new IncomingForm({
multiples: true, // 支持多文件上传
uploadDir: cacheFilePath, // 上传文件存储目录
@@ -96,39 +102,61 @@ router.post('/api/s1/resources/upload', async (req, res) => {
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
const metadata: any = {};
if (share) {
metadata.share = 'public';
}
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
'Content-Type': getContentType(relativePath),
'app-source': 'user-app',
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
...metadata,
});
uploadResults.push({
name: relativePath,
path: minioPath,
});
fs.unlinkSync(tempPath); // 删除临时文件
} // 受控
const r = await app.call({
path: 'app',
key: 'uploadFiles',
payload: {
token: token,
data: {
appKey,
version,
username,
files: uploadResults,
},
},
});
const data: any = {
code: r.code,
data: r.body,
};
if (r.message) {
data.message = r.message;
}
console.log('upload data', data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
if (!noCheckAppFiles) {
// 受控
const r = await app.call({
path: 'app',
key: 'uploadFiles',
payload: {
token: token,
data: {
appKey,
version,
username,
files: uploadResults,
},
},
});
const data: any = {
code: r.code,
data: {
app: r.body,
upload: uploadResults,
},
};
if (r.message) {
data.message = r.message;
}
console.log('upload data', data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
code: 200,
data: {
detect: [],
upload: uploadResults,
},
}),
);
}
});
});

View File

@@ -1,56 +1,57 @@
import { app } from '@/app.ts'
import { User } from '@/models/user.ts'
import { app } from '@/app.ts';
import { User } from '@/models/user.ts';
app
.route({
path: 'user',
key: 'getUpdateInfo',
middleware: ['auth']
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state?.tokenUser || {}
const user = await User.findByPk(tokenUser.id)
const tokenUser = ctx.state?.tokenUser || {};
const user = await User.findByPk(tokenUser.id);
if (!user) {
ctx.throw(500, 'user not found')
ctx.throw(500, 'user not found');
}
ctx.body = {
nickname: user.nickname,
avatar: user.avatar,
data: user.data
}
data: user.data,
};
})
.addTo(app)
.addTo(app);
app
.route('user', 'updateInfo', {
middleware: ['auth']
middleware: ['auth'],
})
.define(async (ctx) => {
const { nickname, avatar, data } = ctx.query.data || {}
const tokenUser = ctx.state?.tokenUser || {}
const { id } = tokenUser
const user = await User.findByPk(id)
let updateData: any = {}
const { nickname, avatar, data } = ctx.query.data || {};
const tokenUser = ctx.state?.tokenUser || {};
const { id, uid } = tokenUser;
const user = await User.findByPk(id);
let updateData: any = {};
if (!user) {
ctx.throw(500, 'user not found')
ctx.throw(500, 'user not found');
}
if (nickname) {
updateData.nickname = nickname
updateData.nickname = nickname;
}
if (avatar) {
updateData.avatar = avatar
updateData.avatar = avatar;
}
await user.update(
{
...updateData,
data: {
...user.data,
...data
}
...data,
},
},
{
fields: ['nickname', 'avatar', 'data']
}
)
ctx.body = await user.getInfo()
fields: ['nickname', 'avatar', 'data'],
},
);
user.setTokenUser(tokenUser);
ctx.body = await user.getInfo();
})
.addTo(app)
.addTo(app);