Public Source Viewer

비나래아카이브 개발자 포털

실제 서비스 구조를 살펴볼 수 있는 공개용 코드 뷰어입니다. 인증, 세션, 외부 연동, 토큰, 관리자 식별 등 보안상 민감한 구현은 파일 단위 또는 줄 단위로 검열됩니다.

Redacted View
src/services/ai-note.service.ts
공개 가능
1 import fs from 'fs';
2 import path from 'path';
3 import { aiNotesDir, legacyAiNotesFile } from '../config/constants';
4 import { AiNote } from '../types/models';
5
6 function getUserNotesFile(username: string): string {
7 const safeName = Buffer.from(username, 'utf8').toString('base64url');
8 return path.join(aiNotesDir, `${safeName}.json`);
9 }
10
11 function ensureStore(username: string): string {
12 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
13 fs.mkdirSync(aiNotesDir, { recursive: true });
14 }
15
16 const notesFile = getUserNotesFile(username);
17 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
18 let migratedNotes: AiNote[] = [];
19 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
20 try {
21 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
22 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
23 migratedNotes = Array.isArray(legacyStore[username]) ? legacyStore[username] : [];
24 } catch (error) {
25 console.error('기존 AI 노트 파일 읽기 실패:', error);
26 }
27 }
28 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
29 }
30
31 return notesFile;
32 }
33
34 function readStore(username: string): AiNote[] {
35 const notesFile = ensureStore(username);
36 try {
37 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
38 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
39 return Array.isArray(notes) ? notes.map(normalizeNote) : [];
40 } catch (error) {
41 console.error('AI 노트 파일 읽기 실패, 초기화합니다.', error);
42 return [];
43 }
44 }
45
46 function normalizeNote(note: AiNote): AiNote {
47 return {
48 ...note,
49 title: note.title || '',
50 tags: Array.isArray(note.tags) ? note.tags : [],
51 pinned: note.pinned === true,
52 updatedAt: note.updatedAt || note.createdAt
53 };
54 }
55
56 function writeStore(username: string, notes: AiNote[]): void {
57 const notesFile = ensureStore(username);
58 [SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
59 }
60
61 export function getUserAiNotes(username: string): AiNote[] {
62 return readStore(username);
63 }
64
65 export function saveUserAiNote(username: string, note: AiNote): AiNote[] {
66 const notes = readStore(username);
67 const nextNotes = [note, ...notes];
68 writeStore(username, nextNotes);
69 return nextNotes;
70 }
71
72 export function updateUserAiNote(username: string, noteId: string, patch: Partial<AiNote>): AiNote[] {
73 const notes = readStore(username);
74 const nextNotes = notes.map((note) => note.id === noteId
75 ? normalizeNote({ ...note, ...patch, updatedAt: new Date().toISOString() })
76 : note);
77 writeStore(username, nextNotes);
78 return nextNotes;
79 }
80
81 export function deleteUserAiNote(username: string, noteId: string): AiNote[] {
82 const notes = readStore(username);
83 const nextNotes = notes.filter((note) => note.id !== noteId);
84 writeStore(username, nextNotes);
85 return nextNotes;
86 }
87