From 88bbe17075b17c70559fbe5a02ac8d048bff8641 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 11 Aug 2024 13:38:52 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=AC=E3=82=B7=E3=83=BC=E3=83=88?= =?UTF-8?q?=E7=99=BB=E9=8C=B2=E6=99=82=E3=81=AB=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E5=90=8D=E3=82=92=E5=8B=95=E7=9A=84=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../purchasereports/UploadFileModal.tsx | 27 ++++++++++++++++--- view/next-project/src/pages/api/receipts.tsx | 4 ++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/view/next-project/src/components/purchasereports/UploadFileModal.tsx b/view/next-project/src/components/purchasereports/UploadFileModal.tsx index 0432ef6c6..edb36d7f2 100644 --- a/view/next-project/src/components/purchasereports/UploadFileModal.tsx +++ b/view/next-project/src/components/purchasereports/UploadFileModal.tsx @@ -54,6 +54,11 @@ const UplaodFileModal: FC = (props) => { }); }; + const generateRandomString = (charCount = 7): string => { + const str = Math.random().toString(36).substring(2).slice(-charCount); + return str.length < charCount ? str + 'a'.repeat(charCount - str.length) : str; + }; + const submit = async () => { if (!imageFile) { return; @@ -65,9 +70,23 @@ const UplaodFileModal: FC = (props) => { setIsLoading(true); const formData = new FormData(); + + //拡張子取得 + const uploadFileName = imageFile?.name || ''; + const Extension = uploadFileName.split('.').pop(); + + // 日付取得 + const date = new Date(); + const month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(); + const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); + const formattedDate = `${date.getFullYear()}${month}${day}`; + const randomStr = generateRandomString(); + + // ファイル名作成 + const fileName = `receipt_${formattedDate}_${randomStr}.${Extension}`; + formData.append('file', imageFile); - const fileName = imageFile?.name || ''; - formData.append('fileName', `receipts/${fileName}`); + formData.append('fileName', fileName); formData.append('year', year); const response = await fetch('/api/receipts', { @@ -94,7 +113,7 @@ const UplaodFileModal: FC = (props) => { const sendReceipt: Receipt = { purchaseReportID: Number(id), bucketName: process.env.NEXT_PUBLIC_BUCKET_NAME || '', - fileName: imageFile.name, + fileName: fileName, fileType: imageFile.type, remark: '', }; @@ -176,7 +195,7 @@ const UplaodFileModal: FC = (props) => {
- submit()} disabled={isLoading && !imageFile}> + submit()} disabled={isLoading || !imageFile}> 登録
diff --git a/view/next-project/src/pages/api/receipts.tsx b/view/next-project/src/pages/api/receipts.tsx index cf33f71da..20458a62b 100644 --- a/view/next-project/src/pages/api/receipts.tsx +++ b/view/next-project/src/pages/api/receipts.tsx @@ -37,7 +37,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const bucketName = 'finansu'; const year = fields.year && fields.year[0]; - const fileName = `${year}/receipts/${files.file[0].originalFilename}`; + const fileName = fields.fileName + ? `${year}/receipts/${fields.fileName}` + : `${year}/receipts/${files.file[0].originalFilename}`; const file = files.file[0]; const mimetype = file.mimetype; const metaData = { From 3692c33699dc8123502015d9d46ef0a58223d290 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 11 Aug 2024 16:54:10 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[fix]=E4=BB=8A=E6=9C=88=E3=82=92=E6=AD=A3?= =?UTF-8?q?=E3=81=97=E3=81=8F=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/purchasereports/UploadFileModal.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/view/next-project/src/components/purchasereports/UploadFileModal.tsx b/view/next-project/src/components/purchasereports/UploadFileModal.tsx index edb36d7f2..f1a4a24f7 100644 --- a/view/next-project/src/components/purchasereports/UploadFileModal.tsx +++ b/view/next-project/src/components/purchasereports/UploadFileModal.tsx @@ -77,7 +77,8 @@ const UplaodFileModal: FC = (props) => { // 日付取得 const date = new Date(); - const month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(); + const thisMonth = date.getMonth() + 1; + const month = thisMonth < 10 ? '0' + thisMonth : thisMonth; const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); const formattedDate = `${date.getFullYear()}${month}${day}`; const randomStr = generateRandomString();