File size: 4,042 Bytes
88b6846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextResponse } from 'next/server';
import { promises as fs } from 'fs';
import path from 'path';
import { getMetadataPath, ensureDir, sanitizePath } from '@/lib/dataPath';

export async function POST(request: Request) {
    try {
        const body = await request.json();
        const { speaker_id, dataset_name, index } = body;

        if (!speaker_id || !dataset_name || index === undefined) {
            return NextResponse.json({ error: 'Missing parameters: speaker_id, dataset_name, and index are required' }, { status: 400 });
        }

        // Validate index is a number
        if (typeof index !== 'number' || index < 0) {
            return NextResponse.json({ error: 'Invalid index: must be a non-negative number' }, { status: 400 });
        }

        // Sanitize inputs
        const safeSpeakerId = sanitizePath(speaker_id);
        const safeDatasetName = sanitizePath(dataset_name);

        const metadataDir = getMetadataPath();
        await ensureDir(metadataDir);
        const metadataPath = path.join(metadataDir, 'dataset_info.json');

        let datasetInfo: Record<string, unknown> = { speakers: {} };
        try {
            const content = await fs.readFile(metadataPath, 'utf-8');
            datasetInfo = JSON.parse(content);
        } catch {
            // File might not exist
        }

        // Ensure nested structure exists
        const speakers = (datasetInfo.speakers || {}) as Record<string, { datasets?: Record<string, { bookmarks?: number[] }> }>;
        if (!speakers[safeSpeakerId]) {
            speakers[safeSpeakerId] = { datasets: {} };
        }
        if (!speakers[safeSpeakerId].datasets) {
            speakers[safeSpeakerId].datasets = {};
        }
        if (!speakers[safeSpeakerId].datasets![safeDatasetName]) {
            speakers[safeSpeakerId].datasets![safeDatasetName] = { bookmarks: [] };
        }

        const ds = speakers[safeSpeakerId].datasets![safeDatasetName];
        if (!ds.bookmarks) ds.bookmarks = [];

        // Toggle bookmark
        if (ds.bookmarks.includes(index)) {
            ds.bookmarks = ds.bookmarks.filter((i: number) => i !== index);
        } else {
            ds.bookmarks.push(index);
            // Sort bookmarks for consistency
            ds.bookmarks.sort((a, b) => a - b);
        }

        datasetInfo.speakers = speakers;
        await fs.writeFile(metadataPath, JSON.stringify(datasetInfo, null, 2));

        return NextResponse.json({ success: true, bookmarks: ds.bookmarks });

    } catch (error) {
        console.error('Error toggling bookmark:', error);
        return NextResponse.json({
            error: 'Internal Server Error',
            details: error instanceof Error ? error.message : 'Unknown error'
        }, { status: 500 });
    }
}

export async function GET(request: Request) {
    try {
        const { searchParams } = new URL(request.url);
        const speaker_id = searchParams.get('speaker_id');
        const dataset_name = searchParams.get('dataset_name');

        if (!speaker_id || !dataset_name) {
            return NextResponse.json({ bookmarks: [] });
        }

        // Sanitize inputs
        const safeSpeakerId = sanitizePath(speaker_id);
        const safeDatasetName = sanitizePath(dataset_name);

        const metadataPath = path.join(getMetadataPath(), 'dataset_info.json');

        try {
            const content = await fs.readFile(metadataPath, 'utf-8');
            const datasetInfo = JSON.parse(content);
            const speakers = datasetInfo.speakers as Record<string, { datasets?: Record<string, { bookmarks?: number[] }> }>;
            const bookmarks = speakers[safeSpeakerId]?.datasets?.[safeDatasetName]?.bookmarks || [];
            return NextResponse.json({ bookmarks });
        } catch {
            return NextResponse.json({ bookmarks: [] });
        }
    } catch (error) {
        console.error('Error getting bookmarks:', error);
        return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
    }
}