Skip to content

Instantly share code, notes, and snippets.

@antoinerousseau
Last active June 14, 2022 03:08
Show Gist options
  • Save antoinerousseau/b6c9fa91c12f460c076baa26a7df6948 to your computer and use it in GitHub Desktop.
Save antoinerousseau/b6c9fa91c12f460c076baa26a7df6948 to your computer and use it in GitHub Desktop.
nginx config for custom file listing
<script>
var list = [];
document.querySelectorAll("a").forEach(function(tag) {
if (tag.innerText === "../" || tag.rel) {
return;
}
var url = tag.href.replace(location.href, "");
var file = decodeURIComponent(url);
tag.innerText = file;
var rawInfos = tag.nextSibling.textContent;
var date = new Date(rawInfos.match(/\d{2}\-\w{3}\-\d{4} \d{2}:\d{2}/)[0]);
var dateTag = document.createElement("span");
dateTag.innerText = date.toLocaleDateString() + " ";
var element = document.createElement("li");
element.appendChild(dateTag);
element.appendChild(tag);
var isFile = rawInfos.match(/\d{2}:\d{2} +(\d+)([KMG]?)/);
if (isFile) {
element.className = "file";
var size = isFile[1];
var unit = isFile[2];
var infos = document.createElement("span");
infos.innerText = " (" + size + "\u00A0" + unit + "B)";
element.appendChild(infos);
} else {
element.className = "dir";
}
list.push({
date: date.toISOString(),
element: element,
});
});
list.sort(function(left, right) {
return left.date > right.date ? -1 : 1;
});
var content = document.createElement("ul");
list.forEach(function(item) {
content.appendChild(item.element);
});
document.querySelectorAll("hr").forEach(function(tag) {
document.body.removeChild(tag);
});
document.body.removeChild(document.querySelector("pre"));
document.body.appendChild(content);
</script>
server {
server_name files.yoursite.com;
root /home/you/files;
location / {
# auth_basic "Restricted content";
# auth_basic_user_file /etc/nginx/htpasswd;
autoindex on;
charset utf-8;
autoindex_exact_size off;
sub_filter '<html>' '';
sub_filter '<head><title>Index of $uri</title></head>' '';
sub_filter '<body bgcolor="white">' '';
sub_filter_once on;
# you might need to enable ngx_http_addition_module for these:
add_before_body /.html/top.html;
add_after_body /.html/bottom.html;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>My files</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
<style>
body {
font-family: monospace;
padding-right: 100px;
word-break: break-word;
}
p, ul {
font-size: 1.2em;
}
ul {
list-style: none;
padding-left: 1px;
}
li {
margin: 10px 0;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment