This commit is contained in:
Archer
2023-10-30 13:26:42 +08:00
committed by GitHub
parent 008d0af010
commit 60ee160131
216 changed files with 4429 additions and 2229 deletions

View File

@@ -0,0 +1 @@
export const imageBaseUrl = '/api/system/img/';

View 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;
}

View 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);