feat: file relate kb

This commit is contained in:
archer
2023-09-04 10:51:57 +08:00
parent a3c6d6800b
commit 44e772f0fd
7 changed files with 50 additions and 26 deletions

View File

@@ -26,8 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
const encoding = jschardet.detect(buffer)?.encoding;
res.setHeader('encoding', encoding);
res.setHeader('Content-Type', file.contentType);
res.setHeader('Content-Type', `${file.contentType}; charset=${encoding}`);
res.setHeader('Cache-Control', 'public, max-age=3600');
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(file.filename)}"`);

View File

@@ -38,7 +38,7 @@ class UploadModel {
}).any();
async doUpload(req: NextApiRequest, res: NextApiResponse) {
return new Promise<{ files: FileType[] }>((resolve, reject) => {
return new Promise<{ files: FileType[]; metadata: Record<string, any> }>((resolve, reject) => {
// @ts-ignore
this.uploader(req, res, (error) => {
if (error) {
@@ -46,11 +46,22 @@ class UploadModel {
}
resolve({
// @ts-ignore
files: req.files?.map((file) => ({
...file,
originalname: decodeURIComponent(file.originalname)
}))
files:
// @ts-ignore
req.files?.map((file) => ({
...file,
originalname: decodeURIComponent(file.originalname)
})) || [],
metadata: (() => {
if (!req.body?.metadata) return {};
try {
return JSON.parse(req.body.metadata);
} catch (error) {
console.log(error);
return {};
}
})()
});
});
});
@@ -64,7 +75,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await connectToDatabase();
const { userId } = await authUser({ req, authToken: true });
const { files = [] } = await upload.doUpload(req, res);
const { files, metadata } = await upload.doUpload(req, res);
const gridFs = new GridFSStorage('dataset', userId);
@@ -74,8 +85,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
path: file.path,
filename: file.originalname,
metadata: {
...metadata,
contentType: file.mimetype,
encoding: file.encoding,
userId
}
})

View File

@@ -5,6 +5,7 @@ import { authUser } from '@/service/utils/auth';
import { PgClient } from '@/service/pg';
import { Types } from 'mongoose';
import { PgTrainingTableName } from '@/constants/plugin';
import { GridFSStorage } from '@/service/lib/gridfs';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
@@ -21,24 +22,20 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await connectToDatabase();
// delete all pg data
await PgClient.delete(PgTrainingTableName, {
where: [['user_id', userId], 'AND', ['kb_id', id]]
});
// delete training data
await TrainingData.deleteMany({
userId,
kbId: id
});
// delete related app
await App.updateMany(
{
userId
},
{ $pull: { 'chat.relatedKbs': new Types.ObjectId(id) } }
);
// delete all pg data
await PgClient.delete(PgTrainingTableName, {
where: [['user_id', userId], 'AND', ['kb_id', id]]
});
// delete related files
const gridFs = new GridFSStorage('dataset', userId);
await gridFs.deleteFilesByKbId(id);
// delete kb data
await KB.findOneAndDelete({