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
|
'use server';
import { revalidatePath } from 'next/cache';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { headers } from 'next/headers';
import db from '@/db/db';
import { fileDownloadLogs, userDownloadStats, rfqAttachments } from '@/db/schema';
import { eq, and, gte, sql } from 'drizzle-orm';
import {
createFileDownloadLogSchema,
type CreateFileDownloadLogInput,
type FileDownloadLog
} from './validation';
import { z } from 'zod';
// 요청 정보 타입
interface RequestInfo {
ip?: string;
userAgent?: string;
referer?: string;
sessionId?: string;
}
// 헤더에서 요청 정보 추출
async function getRequestInfo(): Promise<RequestInfo> {
const headersList = await headers();
return {
ip: headersList.get('x-forwarded-for')?.split(',')[0] ||
headersList.get('x-real-ip') ||
headersList.get('cf-connecting-ip') || // Cloudflare
undefined,
userAgent: headersList.get('user-agent') || undefined,
referer: headersList.get('referer') || undefined,
sessionId: headersList.get('x-session-id') || undefined, // 커스텀 세션 ID
};
}
// 파일 다운로드 로그 생성 (메인 서버 액션)
export async function createFileDownloadLog(
input: CreateFileDownloadLogInput
): Promise<{ success: boolean; error?: string; logId?: number }> {
try {
const validatedInput = createFileDownloadLogSchema.parse(input);
const session = await getServerSession(authOptions);
if (!session?.user) {
return { success: false, error: 'Unauthorized' };
}
const requestInfo = await getRequestInfo();
const fileInfo = validatedInput.fileInfo;
// 다운로드 로그 생성
const [newLog] = await db
.insert(fileDownloadLogs)
.values({
fileId: validatedInput.fileId,
userId: session.user.id,
userEmail: session.user.email || undefined,
userName: session.user.name || undefined,
userIP: requestInfo.ip,
userAgent: requestInfo.userAgent,
fileName: fileInfo?.fileName,
filePath: fileInfo?.filePath,
fileSize: fileInfo?.fileSize,
success: validatedInput.success,
errorMessage: validatedInput.errorMessage,
sessionId: requestInfo.sessionId,
requestId: validatedInput.requestId,
referer: requestInfo.referer,
downloadDurationMs: validatedInput.downloadDurationMs,
downloadedAt: new Date(),
})
.returning({ id: fileDownloadLogs.id });
// 성공한 다운로드인 경우 통계 업데이트
if (validatedInput.success && fileInfo?.fileSize) {
await updateUserDownloadStats(
session.user.id,
fileInfo.fileSize,
validatedInput.fileId
);
}
return { success: true, logId: newLog.id };
} catch (error) {
console.error('파일 다운로드 로그 생성 실패:', error);
if (error instanceof z.ZodError) {
return {
success: false,
error: `입력값 검증 실패: ${error.errors.map(e => e.message).join(', ')}`
};
}
return { success: false, error: '로그 생성 중 오류가 발생했습니다.' };
}
}
// 사용자 다운로드 통계 업데이트
async function updateUserDownloadStats(
userId: string,
fileSize: number,
fileId: number
): Promise<void> {
try {
const today = new Date();
today.setHours(0, 0, 0, 0);
// 오늘 날짜의 통계가 있는지 확인
const [existingStats] = await db
.select()
.from(userDownloadStats)
.where(
and(
eq(userDownloadStats.userId, userId),
gte(userDownloadStats.date, today)
)
)
.limit(1);
if (existingStats) {
// 기존 통계 업데이트
await db
.update(userDownloadStats)
.set({
totalDownloads: sql`${userDownloadStats.totalDownloads} + 1`,
totalBytes: sql`${userDownloadStats.totalBytes} + ${fileSize}`,
lastDownloadAt: new Date(),
// uniqueFiles는 별도 로직으로 계산 (복잡하므로 생략)
})
.where(eq(userDownloadStats.id, existingStats.id));
} else {
// 새 통계 생성
await db.insert(userDownloadStats).values({
userId,
date: today,
totalDownloads: 1,
totalBytes: fileSize,
uniqueFiles: 1,
lastDownloadAt: new Date(),
});
}
} catch (error) {
console.error('다운로드 통계 업데이트 실패:', error);
// 통계 실패가 메인 기능에 영향을 주지 않도록 에러를 던지지 않음
}
}
// 사용자 다운로드 이력 조회
export async function getUserDownloadHistory(
page: number = 1,
limit: number = 20
): Promise<{
logs: FileDownloadLog[],
total: number,
hasMore: boolean
}> {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
throw new Error('Unauthorized');
}
const offset = (page - 1) * limit;
// 총 개수 조회
const [{ count }] = await db
.select({ count: sql<number>`count(*)` })
.from(fileDownloadLogs)
.where(eq(fileDownloadLogs.userId, session.user.id));
// 로그 조회
const logs = await db
.select()
.from(fileDownloadLogs)
.where(eq(fileDownloadLogs.userId, session.user.id))
.orderBy(sql`${fileDownloadLogs.downloadedAt} DESC`)
.limit(limit)
.offset(offset);
return {
logs,
total: count,
hasMore: offset + logs.length < count,
};
} catch (error) {
console.error('다운로드 이력 조회 실패:', error);
throw new Error('다운로드 이력을 가져올 수 없습니다.');
}
}
// 사용자 다운로드 통계 조회
export async function getUserDownloadStats(days: number = 30): Promise<{
totalDownloads: number;
totalBytes: number;
uniqueFiles: number;
dailyStats: Array<{
date: string;
downloads: number;
bytes: number;
}>;
}> {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
throw new Error('Unauthorized');
}
const startDate = new Date();
startDate.setDate(startDate.getDate() - days);
// 기간별 통계 조회
const dailyStats = await db
.select({
date: userDownloadStats.date,
downloads: userDownloadStats.totalDownloads,
bytes: userDownloadStats.totalBytes,
})
.from(userDownloadStats)
.where(
and(
eq(userDownloadStats.userId, session.user.id),
gte(userDownloadStats.date, startDate)
)
)
.orderBy(userDownloadStats.date);
// 총합 계산
const totals = dailyStats.reduce(
(acc, stat) => ({
totalDownloads: acc.totalDownloads + stat.downloads,
totalBytes: acc.totalBytes + stat.bytes,
}),
{ totalDownloads: 0, totalBytes: 0 }
);
// 고유 파일 수 계산 (기간 내)
const [{ uniqueFiles }] = await db
.select({
uniqueFiles: sql<number>`count(distinct ${fileDownloadLogs.fileId})`
})
.from(fileDownloadLogs)
.where(
and(
eq(fileDownloadLogs.userId, session.user.id),
eq(fileDownloadLogs.success, true),
gte(fileDownloadLogs.downloadedAt, startDate)
)
);
return {
...totals,
uniqueFiles: uniqueFiles || 0,
dailyStats: dailyStats.map(stat => ({
date: stat.date.toISOString().split('T')[0],
downloads: stat.downloads,
bytes: stat.bytes,
})),
};
} catch (error) {
console.error('다운로드 통계 조회 실패:', error);
throw new Error('다운로드 통계를 가져올 수 없습니다.');
}
}
// 관리자용: 전체 다운로드 로그 조회 (권한 확인 필요)
export async function getAllDownloadLogs(
page: number = 1,
limit: number = 50,
filters?: {
userId?: string;
success?: boolean;
startDate?: Date;
endDate?: Date;
}
): Promise<{
logs: FileDownloadLog[],
total: number,
hasMore: boolean
}> {
try {
const session = await getServerSession(authOptions);
if (!session?.user || !session?.user?.roles.includes('admin') ) {
throw new Error('관리자 권한이 필요합니다.');
}
const offset = (page - 1) * limit;
// 필터 조건 구성
const conditions = [];
if (filters?.userId) {
conditions.push(eq(fileDownloadLogs.userId, filters.userId));
}
if (filters?.success !== undefined) {
conditions.push(eq(fileDownloadLogs.success, filters.success));
}
if (filters?.startDate) {
conditions.push(gte(fileDownloadLogs.downloadedAt, filters.startDate));
}
if (filters?.endDate) {
conditions.push(sql`${fileDownloadLogs.downloadedAt} <= ${filters.endDate}`);
}
const whereClause = conditions.length > 0 ? and(...conditions) : undefined;
// 총 개수 조회
const [{ count }] = await db
.select({ count: sql<number>`count(*)` })
.from(fileDownloadLogs)
.where(whereClause);
// 로그 조회
const logs = await db
.select()
.from(fileDownloadLogs)
.where(whereClause)
.orderBy(sql`${fileDownloadLogs.downloadedAt} DESC`)
.limit(limit)
.offset(offset);
return {
logs,
total: count,
hasMore: offset + logs.length < count,
};
} catch (error) {
console.error('전체 다운로드 로그 조회 실패:', error);
throw new Error('다운로드 로그를 가져올 수 없습니다.');
}
}
// 편의 함수: 간단한 로그 생성
export async function logDownloadAttempt(
fileId: number,
success: boolean,
errorMessage?: string,
requestId?: string
): Promise<void> {
await createFileDownloadLog({
fileId,
success,
errorMessage,
requestId,
});
}
|