Skip to content

Instantly share code, notes, and snippets.

@recalde
Created November 14, 2024 02:49
Show Gist options
  • Save recalde/ac8713309804e27832133172493b44db to your computer and use it in GitHub Desktop.
Save recalde/ac8713309804e27832133172493b44db to your computer and use it in GitHub Desktop.
Directory Browser
#!/bin/bash
# Create the icons directory if it doesn't exist
mkdir -p wwwroot/icons
# Download a folder icon
curl -o wwwroot/icons/folder.png https://img.icons8.com/ultraviolet/40/000000/folder-invoices.png
# Download a file icon
curl -o wwwroot/icons/file.png https://img.icons8.com/ultraviolet/40/000000/document.png
# Download an up (parent directory) icon
curl -o wwwroot/icons/up.png https://img.icons8.com/ultraviolet/40/000000/up-squared.png
echo "Icons downloaded successfully!"
@page "{*path}"
@model DirectoryBrowserModel
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Directory Browser</title>
<link rel="stylesheet" href="~/css/retro-style.css">
</head>
<body>
<h1>Directory Browser</h1>
<!-- Filter Textbox -->
<input type="text" id="filter" placeholder="Filter files and folders..." onkeyup="filterList()">
<!-- Sort Buttons -->
<button onclick="sortList('type')">Sort by Type</button>
<button onclick="sortList('name')">Sort by Name</button>
<table id="fileTable">
<thead>
<tr><th>Type</th><th>Name</th></tr>
</thead>
<tbody>
@if (Model.ParentPath != null)
{
<tr>
<td><img src="~/icons/up.png" alt="Parent Folder"></td>
<td><a href="@Model.ParentPath">[Parent Directory]</a></td>
</tr>
}
@foreach (var folder in Model.Folders)
{
<tr data-type="folder">
<td><img src="~/icons/folder.png" alt="Folder"></td>
<td><a href="@Url.Page("DirectoryBrowser", new { path = folder.RelativePath })">@folder.Name</a></td>
</tr>
}
@foreach (var file in Model.Files)
{
<tr data-type="file">
<td><img src="~/icons/file.png" alt="File"></td>
<td>@file.Name</td>
</tr>
}
</tbody>
</table>
<script>
function filterList() {
const filter = document.getElementById('filter').value.toLowerCase();
const rows = document.querySelectorAll('#fileTable tbody tr');
rows.forEach(row => {
const name = row.querySelector('td:nth-child(2)').innerText.toLowerCase();
if (name.includes(filter)) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
}
function sortList(type) {
const rows = Array.from(document.querySelectorAll('#fileTable tbody tr')).filter(row => row.querySelector('td:nth-child(2)'));
const tbody = document.querySelector('#fileTable tbody');
rows.sort((a, b) => {
const valA = type === 'type' ? a.dataset.type : a.querySelector('td:nth-child(2)').innerText.toLowerCase();
const valB = type === 'type' ? b.dataset.type : b.querySelector('td:nth-child(2)').innerText.toLowerCase();
return valA.localeCompare(valB);
});
// Append sorted rows to the tbody
rows.forEach(row => tbody.appendChild(row));
}
</script>
</body>
</html>
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.IO;
public class DirectoryBrowserModel : PageModel
{
public string? ParentPath { get; private set; }
public List<FileEntry> Folders { get; private set; } = new();
public List<FileEntry> Files { get; private set; } = new();
[BindProperty(SupportsGet = true)]
public string? Path { get; set; }
public void OnGet()
{
var fullPath = string.IsNullOrEmpty(Path) ? Directory.GetCurrentDirectory() : System.IO.Path.Combine(Directory.GetCurrentDirectory(), Path);
var directoryInfo = new DirectoryInfo(fullPath);
// Set parent directory link
ParentPath = directoryInfo.Parent != null ? System.IO.Path.Combine("/", directoryInfo.Parent.FullName) : null;
// Add folders
Folders.AddRange(directoryInfo.GetDirectories().Select(d => new FileEntry
{
Name = d.Name,
RelativePath = System.IO.Path.Combine(Path ?? string.Empty, d.Name)
}));
// Add files
Files.AddRange(directoryInfo.GetFiles().Select(f => new FileEntry
{
Name = f.Name,
RelativePath = System.IO.Path.Combine(Path ?? string.Empty, f.Name)
}));
}
public class FileEntry
{
public string Name { get; set; } = string.Empty;
public string RelativePath { get; set; } = string.Empty;
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
var app = builder.Build();
app.UseStaticFiles();
app.MapRazorPages();
app.Run();
input[type="text"] {
padding: 5px;
margin-bottom: 10px;
width: 100%;
box-sizing: border-box;
}
button {
padding: 5px 10px;
margin-right: 5px;
background-color: #333;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #555;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment