Computer Hope
Other => Other => Topic started by: Matthew5 on February 25, 2019, 12:50:02 AM
-
I am reading a list of files in a directory on a linux based system, and want to present to the user of the website both the file name and a description of what that file is. For example if the extension is .ppt I'd like to give the description "Microsoft PowerPoint" or similar. I can't imagine I'm the first to ask this question, and found lots of answers for other programming environments using google searches, but nothing specific to node.js.
Note: I am NOT just looking for the mime-type! I see lots of ways to get the mime-type from an extension. I'd like the human-readable description of the type, based on the extension of the file.
My gut tells me someone has done this in node and I am just being unsuccessful in finding it with google. Thanks for any help.
-
The fs module provides the readdir() and readdirSync() methods.
https://stackoverflow.com/questions/2727167/how-do-you-get-a-list-of-the-names-of-all-files-present-in-a-directory-in-node-j?rq=1
You can extract the extension from the filename, and use a JavaScript switch statement to create custom output depending on the file extension.
if you are looking for a module that already does this, I'm not aware of one.
-
here's a POC
in a directory with these contents
drwxr-xr-x 2 hope hope 4096 Feb 25 07:26 dir1
-rw-r--r-- 1 hope hope 0 Feb 25 07:26 file-noext
-rw-r--r-- 1 hope hope 1941 Feb 25 07:47 listfiles.js
-rw-r--r-- 1 hope hope 112 Feb 25 07:50 note.txt
running this node app listfiles.js
"use strict";
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
// for custom portnum specify in command line e.g. node listfiles.js 5678
const port = process.argv[2] || 8080;
http.createServer(function (req, res) {
console.log(`${req.method} ${req.url}`);
// parse URL
const parsedUrl = url.parse(req.url);
// extract URL path
// Avoid https://en.wikipedia.org/wiki/Directory_traversal_attack
// e.g curl --path-as-is http://localhost:9000/../fileInDanger.txt
// by limiting the path to current directory only
const sanitizePath = path.normalize(parsedUrl.pathname).replace(/^(\.\.[\/\\])+/, '');
let pathname = path.join(__dirname, sanitizePath);
fs.exists(pathname, function (exist) {
if(!exist) {
// if the path is not found, return 404
res.statusCode = 404;
res.end(`File ${pathname} not found`);
return;
}
fs.readdir(pathname, (err, items) => {
var i = 0;
// regex from https://stackoverflow.com/questions/680929/how-to-extract-extension-from-filename-string-in-javascript
var re = /(?:\.([^.]+))?$/; var desc, ext, content = "";
for (i = 0; i < items.length; i++) {
ext = re.exec(items[i].toString())[1];
if (typeof ext == 'undefined') { ext = "none" };
switch(ext) {
case "js":
desc = "JavaScript"; break;
case "txt":
desc = "text file"; break;
case "none":
desc = "none or undefined"; break;
default:
console.log("fell through");
}
content += ( "Item " + i.toString() + ": " + items[i].toString() + ", extension = " + ext + ", desc = " + desc + "<br>" );
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
});
});
}).listen(parseInt(port));
console.log(`Server listening on port ${port}`);
produces this output in a web browser
Item 0: dir1, extension = none, desc = none or undefined
Item 1: file-noext, extension = none, desc = none or undefined
Item 2: listfiles.js, extension = js, desc = JavaScript
Item 3: note.txt, extension = txt, desc = text file