summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Powers <markppowers0@gmail.com>2020-08-09 11:49:36 -0500
committerMark Powers <markppowers0@gmail.com>2020-08-09 11:49:36 -0500
commitce1814387c9793e9553a525a44d218c50d7e255d (patch)
tree9ba7be3019f5802a13b6e25d90a9a6cb3a806676
parent96378c5db8da2d1e694fbe219595317ae3682f33 (diff)
Refactor to use templates, add configHEADmaster
-rw-r--r--index.js118
-rw-r--r--main.css5
-rw-r--r--package.json1
-rw-r--r--templates.js55
4 files changed, 117 insertions, 62 deletions
diff --git a/index.js b/index.js
index 4a52648..b8b34ab 100644
--- a/index.js
+++ b/index.js
@@ -3,18 +3,22 @@
const exec = require('child_process').exec;
const express = require('express');
const fs = require('fs')
+const templates = require('./templates.js');
+const bodyParser = require("body-parser");
const maxFiles = 50;
const port = 8000
-const rootPath = ""
-const type = "written"
+const rootPath = "/home/mark/Music"
+const type = "audio"
const filters = {
- "audio": ["flac", "ogg", "mp3", "aac", "midi", "mid", "opus", "wav"],
+ "audio": ["flac", "ogg", "mp3", "aac", "midi", "mid", "opus", "wav", "m4a", "m3u"],
"written": ["txt", "pdf", "html", "epub", "doc", "docx"]
}
const server = express();
+server.use(bodyParser.urlencoded({ extended: false }));
+
server.use((req, res, next) => {
console.debug(new Date(), req.ip, req.method, req.originalUrl);
if(!["::1", "::ffff:127.0.0.1"].includes(req.ip )){
@@ -34,65 +38,45 @@ function shuffle(a) {
return a;
}
+function loadTheFilesAndExtensions(path){
+ files = fs.readdirSync(path)
+ files.forEach((file) => {
+ let stat = fs.statSync(`${path}/${file}`)
+ if(stat.isDirectory()){
+ loadTheFilesAndExtensions(`${path}/${file}`)
+ } else {
+ let idx = file.lastIndexOf(".")
+ if(idx == -1){
+ return
+ }
+ let ext = file.substring(idx+1)
+ if(!extentions.includes(ext)){
+ extentions.push(ext)
+ }
+ if(type != "all" && filters[type].includes(ext)){
+ theFiles.push({
+ path: path,
+ name: file,
+ created: String(stat.birthtime).substring(0, 24),
+ accessed: String(stat.atime).substring(0, 24),
+ })
+ }
+
+ }
+ })
+}
+
var theFiles
+var extentions
server.get('/', async (req, res, next) => {
+ extentions = []
theFiles = []
- listFiles = function(path){
- files = fs.readdirSync(path)
- files.forEach((file) => {
- let stat = fs.statSync(`${path}/${file}`)
- if(stat.isDirectory()){
- listFiles(`${path}/${file}`)
- } else {
- let idx = file.lastIndexOf(".")
- if(idx == -1){
- return
- }
- let ext = file.substring(idx+1)
- console.log(ext)
- if(filters[type].includes(ext)){
- theFiles.push({
- path: path,
- name: file,
- created: String(stat.birthtime).substring(0, 24),
- accessed: String(stat.atime).substring(0, 24),
- })
- }
-
- }
- })
- }
- listFiles(rootPath)
+ loadTheFilesAndExtensions(rootPath)
shuffle(theFiles)
+
theFiles = theFiles.slice(0, maxFiles)
var html = []
- html.push(`<!doctype html>
- <html lang="en">
- <head>
- <title>Files</title>
- <link rel="stylesheet" type="text/css" href="/main.css">
- <script>
- function fetchOpen(index){
- fetch("/open/"+index)
- .then(response => console.log("ok"))
- .catch((error) => {
- console.error('Error:', error);
- });
- }
- function fetchOpenDir(index){
- event.stopPropagation();
- console.log("test")
- fetch("/openDir/"+index)
- .then(response => console.log("ok"))
- .catch((error) => {
- console.error('Error:', error);
- });
- }
- </script>
- </head>
- <body>
- <h1>Check out these files!</h1>
- <ul>`)
+ html.push(templates["/"]["pre"])
theFiles.forEach((file, index) => {
html.push(`<li onclick="fetchOpen(${index})">`)
html.push(`<span class="name" >${file.name}</span>`)
@@ -101,17 +85,31 @@ server.get('/', async (req, res, next) => {
html.push(`<span class="date" >Accessed: ${file.accessed}</span>`)
html.push(`</li>`)
})
- html.push(`</ul>
- </body>
- </html>`)
+ html.push(templates["/"]["post"])
+ res.status(200).send(html.join(""))
+})
+
+server.get("/config", async (req, res, next) => {
+ var html = []
+ html.push(templates["/config"]["pre"])
+ html.push(`<div><span>Path</span><input id="inputPath" type="text" value="${rootPath}"></div>`)
+ html.push(`<div><span>Max Files</span><input id="inputMax" type="number" value="${maxFiles}"></div>`)
+ html.push(`<div><span>File Filter</span><input id="inputType" type="text" value="${type}"></div>`)
+ html.push(`<div><span class="btn" onclick="postConfig()">Save</span></div>`)
+ html.push(templates["/config"]["post"])
res.status(200).send(html.join(""))
+})
+
+server.post("/config", async (req, res, next) => {
+ console.log(req.headers)
+ console.log(req.body)
+ res.status(200).send("")
})
server.get("/open/:index", (req, res, next)=>{
let index = Number(req.params.index)
let f = theFiles[index]
exec(`xdg-open "${f.path}/${f.name}"`)
-
res.status(200).send("");
})
diff --git a/main.css b/main.css
index 50ee09c..917dd83 100644
--- a/main.css
+++ b/main.css
@@ -30,15 +30,16 @@ span.path {
display: block;
color: #aaa;
}
-span span.btn {
+span.btn {
background-color: #aaa;
color: #fff;
padding: 3px;
margin-left: 3em;
}
-span span.btn:hover {
+span.btn:hover {
background-color: #888;
color: #fff;
padding: 3px;
margin-left: 3em;
+ cursor: pointer
} \ No newline at end of file
diff --git a/package.json b/package.json
index 8c694bb..78ccd10 100644
--- a/package.json
+++ b/package.json
@@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"dependencies": {
+ "body-parser": "^1.19.0",
"express": "^4.17.1"
},
"devDependencies": {},
diff --git a/templates.js b/templates.js
new file mode 100644
index 0000000..10526ee
--- /dev/null
+++ b/templates.js
@@ -0,0 +1,55 @@
+module.exports = {
+ "/": {
+ "pre": `<!doctype html>
+ <html lang="en">
+ <head>
+ <title>Files</title>
+ <link rel="stylesheet" type="text/css" href="/main.css">
+ <script>
+ function fetchOpen(index){
+ fetch("/open/"+index)
+ .then(response => console.log("ok"))
+ .catch((error) => {
+ console.error('Error:', error);
+ });
+ }
+ function fetchOpenDir(index){
+ event.stopPropagation();
+ console.log("test")
+ fetch("/openDir/"+index)
+ .then(response => console.log("ok"))
+ .catch((error) => {
+ console.error('Error:', error);
+ });
+ }
+ </script>
+ </head>
+ <body>
+ <h1>Check out these files!</h1>
+ <ul>`,
+ "post": `</ul>
+ </body>
+ </html>`
+ },
+ "/config": {
+ "pre": `<!doctype html>
+ <html lang="en">
+ <head>
+ <title>Config</title>
+ <link rel="stylesheet" type="text/css" href="/main.css">
+ <script>
+ function postConfig(){
+ newDir = document.getElementById("inputPath")
+ newMax = document.getElementById("inputMax")
+ newType = document.getElementById("inputType")
+ newObj = { rootPath: newDir, maxFiles: newMax, type: newType}
+ fetch("/config", {method: "POST", body: JSON.stringify(newObj)})
+ }
+ </script>
+ </head>
+ <body>
+ <h1>Config</h1>`,
+ "post": `</body>
+ </html>`
+ }
+}