Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
090c8808
authored
Jul 14, 2024
by
Zong
Committed by
GitHub
Jul 14, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(image): metadata support image mime type (#2026)
parent
dd2a9bde
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
8 additions
and
5 deletions
+8
-5
packages/global/common/file/image/type.d.ts
+1
-0
packages/service/common/file/image/controller.ts
+5
-3
projects/app/src/pages/api/system/img/[id].ts
+2
-2
No files found.
packages/global/common/file/image/type.d.ts
View file @
090c8808
...
...
@@ -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
};
};
packages/service/common/file/image/controller.ts
View file @
090c8808
...
...
@@ -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
,
...
...
@@ -22,7 +23,8 @@ 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({
...
...
@@ -30,7 +32,7 @@ export async function uploadMongoImg({
teamId,
binary,
expiredTime,
metadata
,
metadata
: Object.assign({ mime }, metadata)
,
shareId
});
...
...
@@ -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({
...
...
projects/app/src/pages/api/system/img/[id].ts
View file @
090c8808
...
...
@@ -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
,
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment