diff options
Diffstat (limited to 'lib/vendor-document-list/dolce-upload-service.ts')
| -rw-r--r-- | lib/vendor-document-list/dolce-upload-service.ts | 107 |
1 files changed, 106 insertions, 1 deletions
diff --git a/lib/vendor-document-list/dolce-upload-service.ts b/lib/vendor-document-list/dolce-upload-service.ts index d98a4c70..032b028c 100644 --- a/lib/vendor-document-list/dolce-upload-service.ts +++ b/lib/vendor-document-list/dolce-upload-service.ts @@ -17,6 +17,19 @@ export interface DOLCEUploadResult { } } +interface ResultData { + FileId: string; + UploadId: string; + FileSeq: number; + FileName: string; + FileRelativePath: string; + FileSize: number; + FileCreateDT: string; // ISO string format + FileWriteDT: string; // ISO string format + OwnerUserId: string; +} + + interface FileReaderConfig { baseDir: string; isProduction: boolean; @@ -339,8 +352,10 @@ private async uploadFiles( uploadId: string // 이미 생성된 UploadId를 매개변수로 받음 ): Promise<Array<{ uploadId: string, fileId: string, filePath: string }>> { const uploadResults = [] + const resultDataArray: ResultData[] = [] - for (const attachment of attachments) { + for (let i = 0; i < attachments.length; i++) { + const attachment = attachments[i] try { // FileId만 새로 생성 (UploadId는 이미 생성된 것 사용) const fileId = uuidv4() @@ -386,6 +401,23 @@ private async uploadFiles( filePath: dolceFilePath }) + // ResultData 객체 생성 (PWPUploadResultService 호출용) + const fileStats = await this.getFileStats(attachment.filePath) // 파일 통계 정보 조회 + + const resultData: ResultData = { + FileId: fileId, + UploadId: uploadId, + FileSeq: i + 1, // 1부터 시작하는 시퀀스 + FileName: attachment.fileName, + FileRelativePath: dolceFilePath, + FileSize: fileStats.size, + FileCreateDT: fileStats.birthtime.toISOString(), + FileWriteDT: fileStats.mtime.toISOString(), + OwnerUserId: userId + } + + resultDataArray.push(resultData) + console.log(`✅ File uploaded successfully: ${attachment.fileName} -> ${dolceFilePath}`) console.log(`✅ DB updated for attachment ID: ${attachment.id}`) @@ -395,9 +427,82 @@ private async uploadFiles( } } + // 모든 파일 업로드가 완료된 후 PWPUploadResultService 호출 + if (resultDataArray.length > 0) { + try { + await this.finalizeUploadResult(resultDataArray) + console.log(`✅ Upload result finalized for UploadId: ${uploadId}`) + } catch (error) { + console.error(`❌ Failed to finalize upload result for UploadId: ${uploadId}`, error) + // 파일 업로드는 성공했지만 결과 저장 실패 - 로그만 남기고 계속 진행 + } + } + return uploadResults } + +private async finalizeUploadResult(resultDataArray: ResultData[]): Promise<void> { + const url = `${this.UPLOAD_SERVICE_URL}/PWPUploadResultService.ashx` + + try { + const jsonData = JSON.stringify(resultDataArray) + const dataBuffer = Buffer.from(jsonData, 'utf-8') + + console.log(`Calling PWPUploadResultService with ${resultDataArray.length} files`) + console.log('ResultData:', JSON.stringify(resultDataArray, null, 2)) + + const response = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: dataBuffer + }) + + if (!response.ok) { + const errorText = await response.text() + throw new Error(`PWPUploadResultService failed: HTTP ${response.status} - ${errorText}`) + } + + const result = await response.text() + + if (result !== 'Success') { + throw new Error(`PWPUploadResultService returned unexpected result: ${result}`) + } + + console.log('✅ PWPUploadResultService call successful') + + } catch (error) { + console.error('❌ PWPUploadResultService call failed:', error) + throw error + } +} + +// 파일 통계 정보 조회 헬퍼 메서드 (파일시스템에서 파일 정보를 가져옴) +private async getFileStats(filePath: string): Promise<{ size: number, birthtime: Date, mtime: Date }> { + try { + // Node.js 환경이라면 fs.stat 사용 + const fs = require('fs').promises + const stats = await fs.stat(filePath) + + return { + size: stats.size, + birthtime: stats.birthtime, + mtime: stats.mtime + } + } catch (error) { + console.warn(`Could not get file stats for ${filePath}, using defaults`) + // 파일 정보를 가져올 수 없는 경우 기본값 사용 + const now = new Date() + return { + size: 0, + birthtime: now, + mtime: now + } + } +} + /** * 문서 정보 업로드 (DetailDwgReceiptMgmtEdit) */ |
