Skip to content

Commit

Permalink
Merge pull request #871 from NUTFes/feat/kubosaka/870-change-receipt-…
Browse files Browse the repository at this point in the history
…file-name

レシート登録時にファイル名を動的生成
  • Loading branch information
Kubosaka authored Aug 11, 2024
2 parents 34bad9d + 3692c33 commit 6a53e08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const UplaodFileModal: FC<ModalProps> = (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;
Expand All @@ -65,9 +70,24 @@ const UplaodFileModal: FC<ModalProps> = (props) => {

setIsLoading(true);
const formData = new FormData();

//拡張子取得
const uploadFileName = imageFile?.name || '';
const Extension = uploadFileName.split('.').pop();

// 日付取得
const date = new Date();
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();

// ファイル名作成
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', {
Expand All @@ -94,7 +114,7 @@ const UplaodFileModal: FC<ModalProps> = (props) => {
const sendReceipt: Receipt = {
purchaseReportID: Number(id),
bucketName: process.env.NEXT_PUBLIC_BUCKET_NAME || '',
fileName: imageFile.name,
fileName: fileName,
fileType: imageFile.type,
remark: '',
};
Expand Down Expand Up @@ -176,7 +196,7 @@ const UplaodFileModal: FC<ModalProps> = (props) => {
</div>
</div>
<div className='my-2 flex w-full flex-wrap justify-center'>
<PrimaryButton type='button' onClick={() => submit()} disabled={isLoading && !imageFile}>
<PrimaryButton type='button' onClick={() => submit()} disabled={isLoading || !imageFile}>
登録
</PrimaryButton>
</div>
Expand Down
4 changes: 3 additions & 1 deletion view/next-project/src/pages/api/receipts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 6a53e08

Please sign in to comment.