mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
v4.5.2 (#439)
This commit is contained in:
1
packages/service/common/file/image/constant.ts
Normal file
1
packages/service/common/file/image/constant.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const imageBaseUrl = '/api/system/img/';
|
25
packages/service/common/file/image/controller.ts
Normal file
25
packages/service/common/file/image/controller.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { imageBaseUrl } from './constant';
|
||||
import { MongoImage } from './schema';
|
||||
|
||||
export function getMongoImgUrl(id: string) {
|
||||
return `${imageBaseUrl}${id}`;
|
||||
}
|
||||
|
||||
export async function uploadMongoImg({ base64Img, userId }: { base64Img: string; userId: string }) {
|
||||
const base64Data = base64Img.split(',')[1];
|
||||
|
||||
const { _id } = await MongoImage.create({
|
||||
userId,
|
||||
binary: Buffer.from(base64Data, 'base64')
|
||||
});
|
||||
|
||||
return getMongoImgUrl(String(_id));
|
||||
}
|
||||
|
||||
export async function readMongoImg({ id }: { id: string }) {
|
||||
const data = await MongoImage.findById(id);
|
||||
if (!data) {
|
||||
return Promise.reject('Image not found');
|
||||
}
|
||||
return data?.binary;
|
||||
}
|
16
packages/service/common/file/image/schema.ts
Normal file
16
packages/service/common/file/image/schema.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { connectionMongo, type Model } from '../../mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
|
||||
const ImageSchema = new Schema({
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
required: true
|
||||
},
|
||||
binary: {
|
||||
type: Buffer
|
||||
}
|
||||
});
|
||||
|
||||
export const MongoImage: Model<{ userId: string; binary: Buffer }> =
|
||||
models['image'] || model('image', ImageSchema);
|
Reference in New Issue
Block a user