generated from lobehub/chat-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: support image upload feature (#9)
* ✨ feat: try with upload * ✨ feat: try with upload * 📝 docs: update docs * ✨ feat: try with upload * 🐛 fix: fix upload error * ✨ feat: support upload with image * 🎨 chore: clean code
- Loading branch information
Showing
12 changed files
with
265 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { getServerConfig } from '@/config/server'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
const baseURL = 'https://api.imgur.com/3'; | ||
|
||
export const POST = async (req: Request) => { | ||
const clientId = getServerConfig().IMGUR_CLIENT_ID; | ||
|
||
const res = await fetch(`${baseURL}/upload`, { | ||
body: await req.blob(), | ||
headers: { | ||
Authorization: `Client-ID ${clientId}`, | ||
}, | ||
method: 'POST', | ||
}).catch((error) => { | ||
return new Response(JSON.stringify(error.cause), { status: 400 }); | ||
}); | ||
|
||
if (!res.ok) { | ||
return res; | ||
} | ||
|
||
const data: UploadResponse = await res.json(); | ||
|
||
let url: string | undefined; | ||
if (data.success) { | ||
url = data.data.link; | ||
} | ||
|
||
if (!url) return new Response(JSON.stringify({ error: 'upload failed' }), { status: 500 }); | ||
|
||
return new Response(JSON.stringify({ url })); | ||
}; | ||
|
||
interface UploadResponse { | ||
data: UploadData; | ||
status: number; | ||
success: boolean; | ||
} | ||
|
||
interface UploadData { | ||
account_id: any; | ||
account_url: any; | ||
ad_type: any; | ||
ad_url: any; | ||
animated: boolean; | ||
bandwidth: number; | ||
datetime: number; | ||
deletehash: string; | ||
description: any; | ||
favorite: boolean; | ||
has_sound: boolean; | ||
height: number; | ||
hls: string; | ||
id: string; | ||
in_gallery: boolean; | ||
in_most_viral: boolean; | ||
is_ad: boolean; | ||
link: string; | ||
mp4: string; | ||
name: string; | ||
nsfw: any; | ||
section: any; | ||
size: number; | ||
tags: any[]; | ||
title: any; | ||
type: string; | ||
views: number; | ||
vote: any; | ||
width: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { ActionIcon, Image } from '@lobehub/ui'; | ||
import { Upload } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
import { FileImageIcon, Trash } from 'lucide-react'; | ||
import { memo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { useMidjourneyStore } from '@/store/midjourney'; | ||
|
||
const useStyles = createStyles(({ css, token, stylish, cx }) => ({ | ||
container: css` | ||
padding: 4px; | ||
background: ${token.colorFillTertiary}; | ||
border: 1px solid ${token.colorBorderSecondary}; | ||
border-radius: ${token.borderRadiusLG}px; | ||
`, | ||
deleteButton: css` | ||
color: #fff; | ||
background: ${token.colorBgMask}; | ||
&:hover { | ||
background: ${token.colorError}; | ||
} | ||
`, | ||
image: css` | ||
width: 56px; | ||
height: 56px; | ||
`, | ||
imageWrapper: css` | ||
align-self: center; | ||
width: 56px; | ||
min-width: auto; | ||
height: 56px; | ||
min-height: auto; | ||
margin-block: 0; | ||
`, | ||
prompt: cx( | ||
stylish.noScrollbar, | ||
css` | ||
align-self: flex-start; | ||
padding: 6px; | ||
font-family: ${token.fontFamilyCode}; | ||
font-size: 13px; | ||
line-height: 1.4 !important; | ||
`, | ||
), | ||
})); | ||
|
||
interface ReferenceImageProps { | ||
imageUploading: boolean; | ||
setImageUploading: (loading: boolean) => void; | ||
} | ||
const ReferenceImage = memo<ReferenceImageProps>(({ imageUploading, setImageUploading }) => { | ||
const { styles } = useStyles(); | ||
const [uploadImageUrl, uploadImage] = useMidjourneyStore((s) => [ | ||
s.referenceImageUrl, | ||
s.uploadImage, | ||
]); | ||
const { t } = useTranslation('common'); | ||
|
||
return uploadImageUrl ? ( | ||
<Image | ||
actions={ | ||
<ActionIcon | ||
className={styles.deleteButton} | ||
glass | ||
icon={Trash} | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
useMidjourneyStore.setState({ referenceImageUrl: undefined }); | ||
}} | ||
size={'small'} | ||
/> | ||
} | ||
classNames={{ image: styles.image, wrapper: styles.imageWrapper }} | ||
src={uploadImageUrl} | ||
wrapperClassName={styles.imageWrapper} | ||
/> | ||
) : ( | ||
<Upload | ||
accept="image/*" | ||
beforeUpload={async (file) => { | ||
setImageUploading(true); | ||
|
||
try { | ||
await uploadImage(file); | ||
} catch {} | ||
|
||
setImageUploading(false); | ||
return false; | ||
}} | ||
multiple={true} | ||
showUploadList={false} | ||
> | ||
<ActionIcon icon={FileImageIcon} loading={imageUploading} title={t('input.uploadImage')} /> | ||
</Upload> | ||
); | ||
}); | ||
|
||
export default ReferenceImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class FileService { | ||
async uploadFile(file: File) { | ||
const formData = new FormData(); | ||
formData.append('image', file); | ||
|
||
const res = await fetch('/api/files/image', { | ||
body: formData, | ||
method: 'POST', | ||
}); | ||
|
||
const { url } = await res.json(); | ||
|
||
return url as string; | ||
} | ||
} | ||
|
||
export const fileService = new FileService(); |
Oops, something went wrong.