1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
"use client";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Badge } from "@/components/ui/badge";
import { CheckCircle2, XCircle, AlertCircle, Upload } from "lucide-react";
interface FileValidationResult {
file: File;
valid: boolean;
parsed?: {
ownDocNo: string;
revNo: string;
stage: string;
fileName: string;
extension: string;
};
error?: string;
}
interface SwpUploadValidationDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
validationResults: FileValidationResult[];
onConfirmUpload: (validFiles: File[]) => void;
isUploading: boolean;
availableDocNos?: string[]; // 업로드 가능한 문서번호 목록
isVendorMode?: boolean; // 벤더 모드인지 여부 (문서번호 검증 필수)
}
/**
* 파일명 검증 함수 (클라이언트 사이드)
* 형식: [OWN_DOC_NO]_[REV_NO]_[STAGE].[확장자] 또는 [OWN_DOC_NO]_[REV_NO]_[STAGE]_[자유-파일명].[확장자]
* 자유 파일명은 선택사항이며, 포함될 경우 언더스코어를 포함할 수 있음
* @param fileName 검증할 파일명
* @param availableDocNos 업로드 가능한 문서번호 목록 (선택)
* @param isVendorMode 벤더 모드인지 여부 (true인 경우 문서번호 검증 필수)
* @param docNoToDocTypeMap 문서번호 → DOC_TYPE 매핑 (Stage 검증용)
* @param documentClassStages Document Class → 허용 Stage 목록 매핑
*/
export function validateFileName(
fileName: string,
availableDocNos?: string[],
isVendorMode?: boolean,
docNoToDocTypeMap?: Record<string, string>,
documentClassStages?: Record<string, string[]>
): {
valid: boolean;
parsed?: {
ownDocNo: string;
revNo: string;
stage: string;
fileName: string;
extension: string;
};
error?: string;
} {
try {
// 확장자 분리
const lastDotIndex = fileName.lastIndexOf(".");
if (lastDotIndex === -1) {
return {
valid: false,
error: "파일 확장자가 없습니다",
};
}
const extension = fileName.substring(lastDotIndex + 1);
const nameWithoutExt = fileName.substring(0, lastDotIndex);
// 언더스코어로 분리
const parts = nameWithoutExt.split("_");
// 최소 3개 파트 필요: ownDocNo, revNo, stage (fileName은 선택사항)
if (parts.length < 3) {
return {
valid: false,
error: `언더스코어(_)가 최소 2개 있어야 합니다 (현재: ${parts.length - 1}개). 형식: [OWN_DOC_NO]_[REV_NO]_[STAGE].[확장자]`,
};
}
// 앞에서부터 3개는 고정: ownDocNo, revNo, stage
const ownDocNo = parts[0];
const revNo = parts[1];
const stage = parts[2];
// 나머지는 자유 파일명 (선택사항, 언더스코어 포함 가능)
const customFileName = parts.length > 3 ? parts.slice(3).join("_") : "";
// 필수 항목이 비어있지 않은지 확인
if (!ownDocNo || ownDocNo.trim() === "") {
return {
valid: false,
error: "문서번호(OWN_DOC_NO)가 비어있습니다",
};
}
if (!revNo || revNo.trim() === "") {
return {
valid: false,
error: "리비전 번호(REV_NO)가 비어있습니다",
};
}
if (!stage || stage.trim() === "") {
return {
valid: false,
error: "스테이지(STAGE)가 비어있습니다",
};
}
// 문서번호 검증 (벤더 모드에서는 필수)
if (isVendorMode) {
const trimmedDocNo = ownDocNo.trim();
// 벤더 모드에서 문서 목록이 비어있으면 에러
if (!availableDocNos || availableDocNos.length === 0) {
return {
valid: false,
error: "할당된 문서가 없거나 문서 목록 로드에 실패했습니다. 페이지를 새로고침하거나 관리자에게 문의하세요.",
};
}
// 문서번호가 목록에 없으면 에러
if (!availableDocNos.includes(trimmedDocNo)) {
return {
valid: false,
error: `문서번호 '${trimmedDocNo}'는 업로드 권한이 없습니다. 할당된 문서번호를 확인해주세요.`,
};
}
}
// Stage 검증 (DOC_TYPE별 허용 Stage 확인)
const trimmedDocNo = ownDocNo.trim();
const trimmedStage = stage.trim();
if (docNoToDocTypeMap && documentClassStages) {
const docType = docNoToDocTypeMap[trimmedDocNo];
if (docType) {
const allowedStages = documentClassStages[docType];
if (allowedStages && allowedStages.length > 0) {
// 허용된 Stage 목록이 있는 경우에만 검증
if (!allowedStages.includes(trimmedStage)) {
return {
valid: false,
error: `문서 '${trimmedDocNo}'의 Document Class '${docType}'에서 Stage '${trimmedStage}'는 허용되지 않습니다. 허용된 Stage: ${allowedStages.join(", ")}`,
};
}
}
// allowedStages가 비어있으면 Stage 검증을 스킵 (설정되지 않은 경우)
}
// docType이 없으면 Stage 검증을 스킵 (문서 정보가 없는 경우)
}
return {
valid: true,
parsed: {
ownDocNo: trimmedDocNo,
revNo: revNo.trim(),
stage: trimmedStage,
fileName: customFileName.trim(),
extension,
},
};
} catch (error) {
return {
valid: false,
error: error instanceof Error ? error.message : "알 수 없는 오류",
};
}
}
/**
* 업로드 전 파일 검증 다이얼로그
*/
export function SwpUploadValidationDialog({
open,
onOpenChange,
validationResults,
onConfirmUpload,
isUploading,
availableDocNos = [],
isVendorMode = false,
}: SwpUploadValidationDialogProps) {
const validFiles = validationResults.filter((r) => r.valid);
const invalidFiles = validationResults.filter((r) => !r.valid);
const handleUpload = () => {
if (validFiles.length > 0) {
onConfirmUpload(validFiles.map((r) => r.file));
}
};
const handleCancel = () => {
if (!isUploading) {
onOpenChange(false);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-3xl max-h-[80vh]">
<DialogHeader>
<DialogTitle>파일 업로드 검증</DialogTitle>
<DialogDescription>
선택한 파일의 파일명 형식을 검증합니다
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
{/* 요약 통계 */}
<div className="grid grid-cols-3 gap-4">
<div className="rounded-lg border p-3">
<div className="text-sm text-muted-foreground">전체 파일</div>
<div className="text-2xl font-bold">{validationResults.length}</div>
</div>
<div className="rounded-lg border p-3 bg-green-50 dark:bg-green-950/30">
<div className="text-sm text-green-600 dark:text-green-400">검증 성공</div>
<div className="text-2xl font-bold text-green-600 dark:text-green-400">
{validFiles.length}
</div>
</div>
<div className="rounded-lg border p-3 bg-red-50 dark:bg-red-950/30">
<div className="text-sm text-red-600 dark:text-red-400">검증 실패</div>
<div className="text-2xl font-bold text-red-600 dark:text-red-400">
{invalidFiles.length}
</div>
</div>
</div>
{/* 경고 메시지 */}
{invalidFiles.length > 0 && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertDescription>
{invalidFiles.length}개 파일의 파일명 형식이 올바르지 않습니다.
검증에 성공한 {validFiles.length}개 파일만 업로드됩니다.
</AlertDescription>
</Alert>
)}
{validFiles.length === 0 && (
<Alert variant="destructive">
<XCircle className="h-4 w-4" />
<AlertDescription>
업로드 가능한 파일이 없습니다. 파일명 형식을 확인해주세요.
</AlertDescription>
</Alert>
)}
{/* 파일 목록 */}
<div className="max-h-[400px] overflow-auto rounded-md border p-4">
<div className="space-y-3">
{/* 검증 성공 파일 */}
{validFiles.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold text-green-600 dark:text-green-400 flex items-center gap-2">
<CheckCircle2 className="h-4 w-4" />
검증 성공 ({validFiles.length}개)
</h4>
{validFiles.map((result, index) => (
<div
key={index}
className="rounded-lg border border-green-200 dark:border-green-800 bg-green-50 dark:bg-green-950/30 p-3"
>
<div className="flex items-start justify-between gap-2">
<div className="flex-1 min-w-0">
<div className="font-mono text-sm break-all">
{result.file.name}
</div>
{result.parsed && (
<div className="flex flex-wrap gap-1 mt-2">
<Badge variant="outline" className="text-xs">
문서: {result.parsed.ownDocNo}
</Badge>
<Badge variant="outline" className="text-xs">
Rev: {result.parsed.revNo}
</Badge>
<Badge variant="outline" className="text-xs">
Stage: {result.parsed.stage}
</Badge>
{result.parsed.fileName && (
<Badge variant="outline" className="text-xs">
파일명: {result.parsed.fileName}
</Badge>
)}
<Badge variant="outline" className="text-xs">
확장자: .{result.parsed.extension}
</Badge>
</div>
)}
</div>
<CheckCircle2 className="h-5 w-5 text-green-600 dark:text-green-400 shrink-0" />
</div>
</div>
))}
</div>
)}
{/* 검증 실패 파일 */}
{invalidFiles.length > 0 && (
<div className="space-y-2 mt-4">
<h4 className="text-sm font-semibold text-red-600 dark:text-red-400 flex items-center gap-2">
<XCircle className="h-4 w-4" />
검증 실패 ({invalidFiles.length}개)
</h4>
{invalidFiles.map((result, index) => (
<div
key={index}
className="rounded-lg border border-red-200 dark:border-red-800 bg-red-50 dark:bg-red-950/30 p-3"
>
<div className="flex items-start justify-between gap-2">
<div className="flex-1 min-w-0">
<div className="font-mono text-sm break-all">
{result.file.name}
</div>
{result.error && (
<div className="text-xs text-red-600 dark:text-red-400 mt-1">
✗ {result.error}
</div>
)}
</div>
<XCircle className="h-5 w-5 text-red-600 dark:text-red-400 shrink-0" />
</div>
</div>
))}
</div>
)}
</div>
</div>
{/* 형식 안내 */}
<div className="rounded-lg bg-blue-50 dark:bg-blue-950/30 border border-blue-200 dark:border-blue-800 p-3">
<div className="text-sm font-medium text-blue-900 dark:text-blue-100 mb-1">
📋 올바른 파일명 형식
</div>
<code className="text-xs text-blue-700 dark:text-blue-300">
[OWN_DOC_NO]_[REV_NO]_[STAGE].[확장자]
</code>
<div className="text-xs text-blue-600 dark:text-blue-400 mt-1">
예: VD-DOC-001_01_IFA.pdf
</div>
<div className="text-xs text-blue-600 dark:text-blue-400 mt-1">
※ 선택사항: [OWN_DOC_NO]_[REV_NO]_[STAGE]_[파일명].[확장자] (파일명 추가 가능)
</div>
<div className="text-xs text-blue-600 dark:text-blue-400 mt-1">
※ 파일명에는 언더스코어(_)가 포함될 수 있습니다.
</div>
{isVendorMode && (
<>
<div className="text-xs text-blue-600 dark:text-blue-400 mt-2 pt-2 border-t border-blue-200 dark:border-blue-800">
{availableDocNos.length > 0 ? (
<>ℹ️ 업로드 가능한 문서: {availableDocNos.length}개</>
) : (
<>⚠️ 할당된 문서가 없습니다</>
)}
</div>
<div className="text-xs text-blue-600 dark:text-blue-400 mt-1">
⚠️ 각 문서의 Document Class에 정의된 Stage만 사용할 수 있습니다.
</div>
</>
)}
</div>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={handleCancel}
disabled={isUploading}
>
취소
</Button>
<Button
onClick={handleUpload}
disabled={validFiles.length === 0 || isUploading}
>
{isUploading ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2" />
업로드 중...
</>
) : (
<>
<Upload className="h-4 w-4 mr-2" />
업로드 ({validFiles.length}개)
</>
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
|