Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image): metadata support image mime type #2026

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/global/common/file/image/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type MongoImageSchemaType = {
type: `${MongoImageTypeEnum}`;

metadata?: {
mime?: string; // image mime type.
relatedId?: string; // This id is associated with a set of images
};
};
8 changes: 5 additions & 3 deletions packages/service/common/file/image/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function getMongoImgUrl(id: string) {
}

export const maxImgSize = 1024 * 1024 * 12;
const base64MimeRegex = /data:image\/([^\)]+);base64/;
export async function uploadMongoImg({
type,
base64Img,
Expand All @@ -22,15 +23,16 @@ export async function uploadMongoImg({
return Promise.reject('Image too large');
}

const base64Data = base64Img.split(',')[1];
const [base64Mime, base64Data] = base64Img.split(',')
const mime = `image/${base64Mime.match(base64MimeRegex)?.[1] ?? 'jpeg'}`
const binary = Buffer.from(base64Data, 'base64');

const { _id } = await MongoImage.create({
type,
teamId,
binary,
expiredTime,
metadata,
metadata: Object.assign({ mime }, metadata),
shareId
});

Expand All @@ -42,7 +44,7 @@ export async function readMongoImg({ id }: { id: string }) {
if (!data) {
return Promise.reject('Image not found');
}
return data?.binary;
return data;
}

export async function delImgByRelatedId({
Expand Down
4 changes: 2 additions & 2 deletions projects/app/src/pages/api/system/img/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await connectToDatabase();
const { id } = req.query as { id: string };

const binary = await readMongoImg({ id });
const { binary, metadata } = await readMongoImg({ id });

res.setHeader('Content-Type', guessBase64ImageType(binary.toString('base64')));
res.setHeader('Content-Type', metadata?.mime ?? guessBase64ImageType(binary.toString('base64')));
res.send(binary);
} catch (error) {
jsonRes(res, {
Expand Down
Loading