Add Static Resource Hosting (#8)

* Add ability to host files from web API rather than using the static site files.hyperling.com.

* Add details.

* Prevent further files from being committed in the static resource folder.

* Beginnings of a photo share from the website to replace PhotoPrism.

* Fix log message for files route.

* Allow customization of ports via shell script.

* Update README goals and fix URLs.

* FInalize the PHOTOS page, complete with video controls!

* Moe enhancements, such as displaying README's and adding [VIDEO] tag.

* Clean file a little.

* Remove TODO.
This commit is contained in:
2024-01-24 11:02:23 +00:00
committed by GitHub
parent 01277d0c9c
commit 2fa8b555e7
6 changed files with 222 additions and 19 deletions

57
main.js
View File

@ -25,7 +25,18 @@ const pages_dir = "./pages/";
const file_types = ["php", "sh"];
let ports = [];
ports.push(8080);
// Check parameters for numeric port numbers.
process.argv.forEach(function (val, index, array) {
console.log("Parameter", index + ':', val, !isNaN(val));
if (!isNaN(val)) {
console.log("Adding Port", val)
ports.push(val);
}
});
// Default port if none were passed.
if (ports.length === 0) {
ports.push(8080);
}
//// Functions ////
@ -116,7 +127,7 @@ async function main() {
console.log("...Adding Routes...");
let router = express.Router();
/* AUTOMATIC METHOD BASED ON OBJECT/ARRAY OF WEB SCRIPTS
/* AUTOMATIC METHOD BASED ON OBJECT/ARRAY OF WEB SCRIPTS
// Creates routes with the URL of the key and location of the value.
*/
for (let key in pages) {
@ -170,6 +181,48 @@ async function main() {
res.send(sitemap_html);
});
// Return a resource from the files folder.
console.log(" * Creating router for files");
router.get('/files*', function (req, res) {
console.log("file response to", req.socket.remoteAddress, "asking for", req.url)
// Build variables.
const file = "." + req.path;
const extensions = req.path.split(".");
const extension = extensions[extensions.length-1];
// Check extension and guess a MIME type.
let mime;
switch (extension) {
case "apk":
mime = "application/vnd.android.package-archive";
break;
case "jpg" || "jpeg":
mime = "image/jpeg";
break;
case "png":
mime = "image/png";
break;
case "html":
mime = "text/html";
break;
default:
mime = "text/*";
break;
}
console.log("- Extension", extension, "led to MIME", mime);
// Return the file
res.contentType(mime);
let f = fs.createReadStream(file)
.on("error", function(e) {
res.contentType("text/plain");
res.send(404, "File Not Found");
})
.pipe(res)
;
});
// Originally a test, now a catch-all redirection to Home!
console.log(" * Creating router for redirection");
router.get('/*', function (req, res) {