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
|
// lib/file-storage.ts - File과 ArrayBuffer를 위한 분리된 함수들
import { promises as fs } from "fs";
import path from "path";
import crypto from "crypto";
interface FileStorageConfig {
baseDir: string;
publicUrl: string;
isProduction: boolean;
}
// 파일명 해시 생성 유틸리티
export function generateHashedFileName(originalName: string): string {
const fileExtension = path.extname(originalName);
const fileNameWithoutExt = path.basename(originalName, fileExtension);
const timestamp = Date.now();
const randomHash = crypto.createHash('md5')
.update(`${fileNameWithoutExt}-${timestamp}-${Math.random()}`)
.digest('hex')
.substring(0, 8);
return `${timestamp}-${randomHash}${fileExtension}`;
}
// ✅ File 저장용 인터페이스
interface SaveFileOptions {
file: File;
directory: string;
originalName?: string;
}
// ✅ Buffer/ArrayBuffer 저장용 인터페이스
interface SaveBufferOptions {
buffer: Buffer | ArrayBuffer;
fileName: string;
directory: string;
originalName?: string;
}
interface SaveFileResult {
success: boolean;
filePath?: string;
publicPath?: string;
fileName?: string;
error?: string;
}
const nasPath = process.env.NAS_PATH || "/evcp_nas"
// 환경별 설정
function getStorageConfig(): FileStorageConfig {
const isProduction = process.env.NODE_ENV === "production";
if (isProduction) {
return {
baseDir: nasPath,
publicUrl: "/api/files",
isProduction: true,
};
} else {
return {
baseDir: path.join(process.cwd(), "public"),
publicUrl: "",
isProduction: false,
};
}
}
// ✅ 1. File 객체 저장 함수 (기존 방식)
export async function saveFile({
file,
directory,
originalName
}: SaveFileOptions): Promise<SaveFileResult> {
try {
const config = getStorageConfig();
const finalFileName = originalName || file.name;
const hashedFileName = generateHashedFileName(finalFileName);
// 저장 경로 설정
const saveDir = path.join(config.baseDir, directory);
const filePath = path.join(saveDir, hashedFileName);
// 웹 접근 경로
let publicPath: string;
if (config.isProduction) {
publicPath = `${config.publicUrl}/${directory}/${hashedFileName}`;
} else {
publicPath = `/${directory}/${hashedFileName}`;
}
console.log(`📄 File 객체 저장: ${finalFileName}`);
console.log(`📁 저장 위치: ${filePath}`);
console.log(`🌐 웹 접근 경로: ${publicPath}`);
// 디렉토리 생성
await fs.mkdir(saveDir, { recursive: true });
// File 객체에서 데이터 추출
const arrayBuffer = await file.arrayBuffer();
const dataBuffer = Buffer.from(arrayBuffer);
// 파일 저장
await fs.writeFile(filePath, dataBuffer);
console.log(`✅ File 저장 완료: ${hashedFileName} (${dataBuffer.length} bytes)`);
return {
success: true,
filePath,
publicPath,
fileName: hashedFileName,
};
} catch (error) {
console.error("File 저장 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "File 저장 중 오류가 발생했습니다.",
};
}
}
// ✅ 2. Buffer/ArrayBuffer 저장 함수 (DRM 복호화용)
export async function saveBuffer({
buffer,
fileName,
directory,
originalName
}: SaveBufferOptions): Promise<SaveFileResult> {
try {
const config = getStorageConfig();
const finalFileName = originalName || fileName;
const hashedFileName = generateHashedFileName(finalFileName);
// 저장 경로 설정
const saveDir = path.join(config.baseDir, directory);
const filePath = path.join(saveDir, hashedFileName);
// 웹 접근 경로
let publicPath: string;
if (config.isProduction) {
publicPath = `${config.publicUrl}/${directory}/${hashedFileName}`;
} else {
publicPath = `/${directory}/${hashedFileName}`;
}
console.log(`🔓 Buffer/ArrayBuffer 저장: ${finalFileName}`);
console.log(`📁 저장 위치: ${filePath}`);
console.log(`🌐 웹 접근 경로: ${publicPath}`);
// 디렉토리 생성
await fs.mkdir(saveDir, { recursive: true });
// Buffer 준비
const dataBuffer = buffer instanceof ArrayBuffer ? Buffer.from(buffer) : buffer;
// 파일 저장
await fs.writeFile(filePath, dataBuffer);
console.log(`✅ Buffer 저장 완료: ${hashedFileName} (${dataBuffer.length} bytes)`);
return {
success: true,
filePath,
publicPath,
fileName: hashedFileName,
};
} catch (error) {
console.error("Buffer 저장 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "Buffer 저장 중 오류가 발생했습니다.",
};
}
}
// ✅ 업데이트 함수들
export async function updateFile(
options: SaveFileOptions,
oldFilePath?: string
): Promise<SaveFileResult> {
try {
const result = await saveFile(options);
if (result.success && oldFilePath) {
await deleteFile(oldFilePath);
}
return result;
} catch (error) {
console.error("File 업데이트 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "File 업데이트 중 오류가 발생했습니다.",
};
}
}
export async function updateBuffer(
options: SaveBufferOptions,
oldFilePath?: string
): Promise<SaveFileResult> {
try {
const result = await saveBuffer(options);
if (result.success && oldFilePath) {
await deleteFile(oldFilePath);
}
return result;
} catch (error) {
console.error("Buffer 업데이트 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "Buffer 업데이트 중 오류가 발생했습니다.",
};
}
}
// 파일 삭제 함수
export async function deleteFile(publicPath: string): Promise<boolean> {
try {
const config = getStorageConfig();
let absolutePath: string;
if (config.isProduction) {
const relativePath = publicPath.replace('/api/files/', '');
absolutePath = path.join(nasPath, relativePath);
} else {
absolutePath = path.join(process.cwd(), 'public', publicPath);
}
console.log(`🗑️ 파일 삭제: ${absolutePath}`);
await fs.access(absolutePath);
await fs.unlink(absolutePath);
return true;
} catch (error) {
console.log("파일 삭제 실패 또는 파일이 없음:", error);
return false;
}
}
// ✅ 편의 함수들 (하위 호환성)
export const save = {
file: saveFile,
buffer: saveBuffer,
};
// ✅ DRM 워크플로우 통합 함수
export async function saveDRMFile(
originalFile: File,
decryptFunction: (file: File) => Promise<ArrayBuffer>,
directory: string
): Promise<SaveFileResult> {
try {
console.log(`🔐 DRM 파일 처리 시작: ${originalFile.name}`);
// 1. DRM 복호화
const decryptedData = await decryptFunction(originalFile);
// 2. 복호화된 데이터 저장
const result = await saveBuffer({
buffer: decryptedData,
fileName: originalFile.name,
directory
});
if (result.success) {
console.log(`✅ DRM 파일 처리 완료: ${originalFile.name}`);
}
return result;
} catch (error) {
console.error(`❌ DRM 파일 처리 실패: ${originalFile.name}`, error);
return {
success: false,
error: error instanceof Error ? error.message : "DRM 파일 처리 중 오류가 발생했습니다.",
};
}
}
|