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

update: limit image extension upload #2212

Merged
merged 1 commit into from
Sep 5, 2023
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
4 changes: 3 additions & 1 deletion src/components/FileInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ChangeEvent, ReactNode, useRef } from 'react'

import { IMAGE_ALLOW_EXTENSIONS } from 'hooks/social'

export default function FileInput({
onChange,
children,
Expand Down Expand Up @@ -41,7 +43,7 @@ export default function FileInput({
type="file"
onChange={handleFileChange}
style={{ visibility: 'hidden', height: 0, width: 0 }}
accept={image ? 'image/*' : undefined}
accept={image ? IMAGE_ALLOW_EXTENSIONS.map(ext => `image/${ext}`).join(', ') : undefined}
/>
</>
)
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/social.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { v4 as uuid } from 'uuid'

import { BUCKET_NAME } from 'constants/env'

const ALLOW_EXTENSIONS = ['jpg', 'jpeg', 'png', 'svg', 'webp', 'gif']
export const IMAGE_ALLOW_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif']

export const useUploadImageToCloud = () => {
const [uploadImage] = useUploadImageMutation()
Expand All @@ -14,7 +14,7 @@ export const useUploadImageToCloud = () => {
async (file: Blob | File) => {
try {
const ext = (file as File).name.split('.').pop() ?? ''
if (!ALLOW_EXTENSIONS.includes(ext)) throw new Error('File is not support')
if (!IMAGE_ALLOW_EXTENSIONS.includes(ext)) throw new Error('File is not support')

const fileName = `${uuid() + Date.now()}.${ext}`
const res = await uploadImage({
Expand Down
Loading