mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00

Co-authored-by: Archer <545436317@qq.com> Co-authored-by: heheer <71265218+newfish-cmyk@users.noreply.github.com>
54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import { UploadImgProps } from '@fastgpt/global/common/file/api';
|
|
import { imageBaseUrl } from '@fastgpt/global/common/file/image/constants';
|
|
import { MongoImage } from './schema';
|
|
|
|
export function getMongoImgUrl(id: string) {
|
|
return `${imageBaseUrl}${id}`;
|
|
}
|
|
|
|
export const maxImgSize = 1024 * 1024 * 12;
|
|
export async function uploadMongoImg({
|
|
type,
|
|
base64Img,
|
|
teamId,
|
|
expiredTime,
|
|
metadata,
|
|
|
|
shareId
|
|
}: UploadImgProps & {
|
|
teamId: string;
|
|
}) {
|
|
if (base64Img.length > maxImgSize) {
|
|
return Promise.reject('Image too large');
|
|
}
|
|
|
|
const base64Data = base64Img.split(',')[1];
|
|
const binary = Buffer.from(base64Data, 'base64');
|
|
|
|
const { _id } = await MongoImage.create({
|
|
type,
|
|
teamId,
|
|
binary,
|
|
expiredTime: expiredTime,
|
|
metadata,
|
|
|
|
shareId
|
|
});
|
|
|
|
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;
|
|
}
|
|
|
|
export async function delImgByRelatedId(relateIds: string[]) {
|
|
return MongoImage.deleteMany({
|
|
'metadata.relatedId': { $in: relateIds.map((id) => String(id)) }
|
|
});
|
|
}
|