From 1e21f76c8f6f0928579b2750868d659d718f0c5e Mon Sep 17 00:00:00 2001 From: bicochan Date: Mon, 14 Jul 2025 11:31:56 +0900 Subject: [PATCH] =?UTF-8?q?[apps/cs]=20feat:=20useTus=20=ED=9B=85=EC=97=90?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=20=ED=99=95=EC=9E=A5=EC=9E=90=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80=20(CS=5FUT0?= =?UTF-8?q?1030102)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/cs/src/pages/image/ui/image-form.tsx | 39 ++++++++++++----------- apps/cs/src/shared/lib/use-tus.ts | 28 +++++++++++++++- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/apps/cs/src/pages/image/ui/image-form.tsx b/apps/cs/src/pages/image/ui/image-form.tsx index 649d195b8..1de7820c5 100644 --- a/apps/cs/src/pages/image/ui/image-form.tsx +++ b/apps/cs/src/pages/image/ui/image-form.tsx @@ -42,8 +42,9 @@ function ImageForm({ form, imgSn, ...props }: ImageFormProps) { file: uploadFile, response: uploadResponse, progress, + isValid, setFile, - } = useTus(); + } = useTus({ accept: ".jpeg,.jpg,.png" }); const setAttachmentFileCode = useAttachmentStore( (state) => state.setAttachmentFileCode, ); @@ -208,24 +209,26 @@ function ImageForm({ form, imgSn, ...props }: ImageFormProps) { customPreview={ <>
diff --git a/apps/cs/src/shared/lib/use-tus.ts b/apps/cs/src/shared/lib/use-tus.ts index 30976c23b..9d4bc7c00 100644 --- a/apps/cs/src/shared/lib/use-tus.ts +++ b/apps/cs/src/shared/lib/use-tus.ts @@ -2,9 +2,15 @@ import { useEffect, useState } from "react"; import { type HttpResponse, Upload } from "tus-js-client"; import { AUTH_USER_ID, CLAIM_NAME, ENDPOINT } from "../config"; import { useAttachmentStore } from "./use-attachment-store"; +import { useAlertStore } from "./use-alert-store"; import { getCookie } from "./cookie"; -export const useTus = () => { +type TusProps = { + accept?: string; +}; + +export const useTus = ({ accept }: TusProps = {}) => { + const alert = useAlertStore((state) => state.setMessage); const { attachmentFileCode, setAttachmentFileCode } = useAttachmentStore( (state) => state, ); @@ -13,6 +19,7 @@ export const useTus = () => { const [upload, setUpload] = useState(null); const [response, setResponse] = useState(null); const [progress, setProgress] = useState(0); + const [isValid, setIsValid] = useState(false); const metadata = { filename: file?.name ?? "", @@ -20,6 +27,23 @@ export const useTus = () => { atflCd: attachmentFileCode, }; + const validFileType = (file: File) => { + const allowedExtensions = accept?.split(",") ?? []; + const fileName = file.name; + const fileExtension = fileName + .substring(fileName.lastIndexOf(".")) + .toLowerCase(); + + if (!allowedExtensions.includes(fileExtension)) { + alert("파일 형식이 올바르지 않습니다."); + setIsValid(false); + return false; + } + + setIsValid(true); + return true; + }; + const createUpload = () => { return new Upload(file!, { endpoint: `${ENDPOINT.ATTACH_SERVICE.TUS_UPLOAD}`, @@ -48,6 +72,7 @@ export const useTus = () => { useEffect(() => { if (!file) return; + if (!validFileType(file)) return; const upload = createUpload(); upload.start(); @@ -59,6 +84,7 @@ export const useTus = () => { upload, response, progress, + isValid, setFile, }; };