File size: 3,216 Bytes
a5871f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

// src/app/api/files/route.ts
import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';

// Define types directly in this file as they are specific to this API route.
export interface File {
  id: string;
  type: 'file';
  name: string;
  path: string;
  fileType: 'pdf' | 'docx' | 'pptx' | 'other';
  contentSnippet: string;
}

export interface Folder {
  id:string;
  type: 'folder';
  name: string;
  path: string;
  children: (Folder | File)[];
}

export type FileSystemNode = Folder | File;


// Helper function to recursively scan directories
function getFileStructure(dirPath: string, relativeTo: string): FileSystemNode[] {
  // Ensure the base directory exists
  if (!fs.existsSync(dirPath)) {
    fs.mkdirSync(dirPath, { recursive: true });
  }

  const children: FileSystemNode[] = fs.readdirSync(dirPath, { withFileTypes: true }).map((dirent) => {
    const childAbsolutePath = path.join(dirPath, dirent.name);
    // This relative path will be like 'folder/file.pdf'
    const fileRelativePath = path.relative(relativeTo, childAbsolutePath).replace(/\\/g, '/');
    const id = fileRelativePath.replace(/[\/\.]/g, '-') || 'root';

    if (dirent.isDirectory()) {
        const subChildren = getFileStructure(childAbsolutePath, relativeTo);
        return {
            id,
            type: 'folder',
            name: dirent.name,
            // The browser path should start with a leading slash
            path: `/${fileRelativePath}`,
            children: subChildren
        } as Folder;
    } else {
        const extension = path.extname(dirent.name).toLowerCase();
        let fileType: File['fileType'] = 'other';
        let contentSnippet = 'A document file.';

        if (extension === '.pdf') {
            fileType = 'pdf';
            contentSnippet = 'A PDF document.';
        } else if (extension === '.docx') {
            fileType = 'docx';
            contentSnippet = 'A Word document.';
        } else if (extension === '.pptx') {
            fileType = 'pptx';
            contentSnippet = 'A PowerPoint presentation.';
        }

        // We only want to return specific file types for this app.
        // You could remove this if you want to show all files.
        if (fileType !== 'other') {
            return {
                id,
                type: 'file',
                name: dirent.name,
                // The path for the browser needs to be a root-relative URL
                path: `/${fileRelativePath}`,
                fileType: fileType,
                contentSnippet: contentSnippet,
            } as File;
        }
      
      return null;
    }
  }).filter((node): node is FileSystemNode => node !== null);

  return children;
}

export async function GET() {
  try {
    const publicDir = path.join(process.cwd(), 'public');
    const children = getFileStructure(publicDir, publicDir);
    
    // The root is now the list of files/folders in the public directory
    return NextResponse.json(children);
  } catch (error) {
    console.error('Failed to read file system:', error);
    // If 'public' directory doesn't exist or another error occurs, return an empty array.
    return NextResponse.json([]);
  }
}