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

レシート登録時にファイル名を動的生成 #871

Merged
merged 2 commits into from
Aug 11, 2024
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
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
Loading