import path from "path"; import fs from "fs"; import { APP_INTERCEPT, FileEntry, MY_COMPUTER_INTERCEPT } from "./fileserver"; import { shouldHideFile } from "./hide-files"; import { encode, getEncoding } from "./encoding"; import { log } from "console"; import { app } from "electron"; export function generateDirectoryListing(currentPath: string, files: string[]): string { const parentPath = path.dirname(currentPath || '/'); const title = currentPath === '/' ? 'My Host Computer' : `Directory: ${encode(currentPath)}`; // Get file info and sort (directories first, then alphabetically) const items = files .map(name => { const fullPath = path.join(currentPath, name); let stats: fs.Stats; try { stats = fs.statSync(fullPath); } catch (error) { log(`FileServer: Failed to get stats for ${fullPath}: ${error}`); stats = new fs.Stats(); } return { name, fullPath, stats } as FileEntry; }) .filter(entry => entry.stats && !shouldHideFile(entry)) .sort((a, b) => { if (a.stats.isDirectory() !== b.stats.isDirectory()) { return a.stats.isDirectory() ? -1 : 1; } return a.name.localeCompare(b.name); }) .map(getFileLiHtml) .join('') // Generate very simple HTML that works in IE 5.5 return `
${getEncoding()}${getParentFolderLinkHtml(parentPath)} | ${getDesktopLinkHtml()} | ${getDownloadsLinkHtml()}