Reubencf commited on
Commit
d596ecb
·
1 Parent(s): 483fbb4

Fix file content loading for public text files

Browse files
app/api/public/route.ts CHANGED
@@ -75,6 +75,19 @@ export async function GET(request: NextRequest) {
75
  }
76
  }
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  files.push({
79
  name: item,
80
  type: 'file',
@@ -83,7 +96,8 @@ export async function GET(request: NextRequest) {
83
  path: relativePath,
84
  extension: getFileExtension(item),
85
  uploadedBy: metadata.uploadedBy || 'Anonymous',
86
- uploadedAt: metadata.uploadedAt || stats.birthtime.toISOString()
 
87
  })
88
  }
89
  }
 
75
  }
76
  }
77
 
78
+ // Read content for text-based files
79
+ let content = undefined
80
+ const ext = path.extname(item).toLowerCase()
81
+ const textExtensions = ['.json', '.tex', '.dart', '.txt', '.md', '.html', '.css', '.js', '.ts', '.jsx', '.tsx', '.py', '.java', '.c', '.cpp', '.h']
82
+
83
+ if (textExtensions.includes(ext)) {
84
+ try {
85
+ content = fs.readFileSync(fullPath, 'utf-8')
86
+ } catch (err) {
87
+ console.error(`Error reading ${item}:`, err)
88
+ }
89
+ }
90
+
91
  files.push({
92
  name: item,
93
  type: 'file',
 
96
  path: relativePath,
97
  extension: getFileExtension(item),
98
  uploadedBy: metadata.uploadedBy || 'Anonymous',
99
+ uploadedAt: metadata.uploadedAt || stats.birthtime.toISOString(),
100
+ ...(content !== undefined && { content })
101
  })
102
  }
103
  }
app/components/FileManager.tsx CHANGED
@@ -543,25 +543,30 @@ export function FileManager({ currentPath, onNavigate, onClose, onOpenFlutterApp
543
  try {
544
  let fileContent = ''
545
  if (sidebarSelection === 'secure' && passkey) {
546
- const response = await fetch(`/api/data?key=${encodeURIComponent(passkey)}&folder=`)
547
  const data = await response.json()
548
  const fileData = data.files?.find((f: any) => f.name === file.name)
549
  fileContent = fileData?.content || ''
550
  } else if (sidebarSelection === 'public') {
551
  // Load from public folder
552
- const response = await fetch(`/api/public?path=${encodeURIComponent(file.path)}`)
 
553
  if (response.ok) {
554
- fileContent = await response.text()
 
 
555
  }
556
  }
557
 
 
 
558
  // Open Text Editor with the file content
559
  if (onOpenTextFile) {
560
  onOpenTextFile({
561
  content: fileContent,
562
  fileName: file.name,
563
  filePath: currentPath,
564
- passkey: passkey
565
  })
566
  }
567
  } catch (error) {
 
543
  try {
544
  let fileContent = ''
545
  if (sidebarSelection === 'secure' && passkey) {
546
+ const response = await fetch(`/api/data?key=${encodeURIComponent(passkey)}&folder=${encodeURIComponent(currentPath)}`)
547
  const data = await response.json()
548
  const fileData = data.files?.find((f: any) => f.name === file.name)
549
  fileContent = fileData?.content || ''
550
  } else if (sidebarSelection === 'public') {
551
  // Load from public folder
552
+ const folderPath = currentPath.replace('public/', '').replace('public', '')
553
+ const response = await fetch(`/api/public?folder=${encodeURIComponent(folderPath)}`)
554
  if (response.ok) {
555
+ const data = await response.json()
556
+ const fileData = data.files?.find((f: any) => f.name === file.name)
557
+ fileContent = fileData?.content || ''
558
  }
559
  }
560
 
561
+ console.log('Opening file:', file.name, 'Content length:', fileContent.length)
562
+
563
  // Open Text Editor with the file content
564
  if (onOpenTextFile) {
565
  onOpenTextFile({
566
  content: fileContent,
567
  fileName: file.name,
568
  filePath: currentPath,
569
+ passkey: passkey || ''
570
  })
571
  }
572
  } catch (error) {