const minisrv_service_file = true;
try {
let relativePath = request_headers.request_url;
if (relativePath.indexOf('?') > -1) relativePath = relativePath.split('?')[0];
while (relativePath.endsWith('/')) relativePath = relativePath.slice(0, relativePath.length - 1);
const dir = service_name + relativePath;
const num_per_page = 25;
const dirs = ['
| Directory | Parent Directory | - | - |
'];
const files = [];
let vault_found = false;
// Iterate through each service vault to find the first occurrence
for (let i = 0; i < service_vaults.length; i++) {
const vaultPath = path.join(service_vaults[i], dir || '');
if (!fs.existsSync(vaultPath)) continue;
try {
vault_found = true;
const entries = fs.readdirSync(vaultPath);
entries.forEach(entry => {
if (entry === path.basename(__filename)) return;
// Check if entry exists in all service vaults
let found = false;
let checkPath = "";
for (let j = 0; j < service_vaults.length; j++) {
checkPath = path.join(service_vaults[j], dir || '', entry);
if (fs.existsSync(checkPath)) {
found = true;
break;
}
}
// Skip if not found in all
if (!found) return;
const fullPath = checkPath;
const stats = fs.statSync(fullPath);
const isDir = stats.isDirectory();
const mimeType = (isDir) ? "Directory" : wtvmime.getContentType(fullPath)[1];
let readableSize = '-';
// Get file size with unit
if (!isDir) {
const fileSize = stats.size;
const units = ['Bytes', 'KB', 'MB', 'GB'];
const unitSize = Math.floor(Math.log(fileSize) / Math.log(1024));
readableSize = (fileSize / Math.pow(1024, unitSize)).toFixed(2) + units[unitSize];
}
// Get last modified time
const mtime = stats.mtime.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
// Check if modified recently
const currentTime = Date.now();
const tenSecondsAgo = currentTime - 10000;
const isModifiedRecently = stats.mtime.getTime() > tenSecondsAgo;
// Create clickable link
const link = isDir ?
`${entry}` :
`${entry}`;
// Generate icon and timestamp display
const icon = mimeType;
const timestampDisplay = isModifiedRecently ?
`${mtime}` :
`${mtime}`;
if (isDir) dirs.push(`| ${icon} | ${(isModifiedRecently) ? entry : link} | ${readableSize} | ${timestampDisplay} |
`);
else files.push(`| ${icon} | ${(isModifiedRecently) ? entry : link} | ${readableSize} | ${timestampDisplay} |
`);
});
} catch (err) {
console.error('Error:', err);
continue;
}
}
if (!vault_found) {
const errpage = wtvshared.doErrorPage(404);
headers = errpage[0];
data = errpage[1];
} else {
// Pagination logic
const totalFiles = files.length;
const max_pages = Math.ceil(totalFiles / num_per_page);
let current_page = 1; // Default to first page
if (totalFiles > 0) {
current_page = parseInt(request_headers.query.page || 1);
if (current_page < 1) current_page = 1;
if (current_page > max_pages) current_page = max_pages;
}
const start_index = (current_page - 1) * num_per_page;
const end_index = current_page * num_per_page;
const merged_files = dirs.concat(files);
const paginatedFiles = merged_files.slice(start_index, end_index);
const paginationHtml = `
`;
data = `
Directory Index
Directory Index of ${relativePath}
| Type | Name | Size | Last Modified |
${paginatedFiles.join('')}
${paginationHtml}
`;
headers = `200 OK\nContent-Type: text/html`;
}
} catch (err) {
console.error('Error:', err);
const errpage = wtvshared.doErrorPage(404);
headers = errpage[0];
data = errpage[1];
}