This commit is contained in:
2024-01-14 13:35:08 +00:00
commit 8bfb336450
13 changed files with 1797 additions and 0 deletions

41
server/index.html Normal file
View File

@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Directory Listing</title>
<style>
/* Add your custom styles here */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
}
a {
text-decoration: none;
color: #1a0dab;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Custom Directory Listing</h1>
<ul>
{files}
</ul>
</body>
</html>

1347
server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

18
server/package.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "24_01-transmediale-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"serve-index": "^1.9.1"
}
}

BIN
server/public/map.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

View File

@@ -0,0 +1 @@
hello

Binary file not shown.

105
server/server.js Normal file
View File

@@ -0,0 +1,105 @@
// const express = require('express');
// const serveIndex = require('serve-index');
// const bodyParser = require('body-parser');
// const path = require('path');
// const cors = require('cors');
// const app = express();
// const port = 3000;
// // Enable CORS for all routes
// app.use(cors());
// app.use(bodyParser.json());
// // Initial directory path
// let currentDirectory = path.join(__dirname, 'public');
// // Set root path for directory listing
// app.use('/', express.static(currentDirectory), serveIndex(currentDirectory, { icons: true }));
// // Start the server
// app.listen(port, () => {
// console.log(`Server is running on http://localhost:${port}`);
// });
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
const listingPath = path.join(__dirname, "public");
app.get("*", (req, res) => {
const decodedPath = decodeURIComponent(req.path);
const filePath = path.join(listingPath, decodedPath);
if (!fs.existsSync(filePath)) {
return res.status(404).end();
}
if (fs.statSync(filePath).isDirectory()) {
const filesInDir = fs.readdirSync(filePath);
const links = filesInDir.map(file => {
const encodedFile = encodeURIComponent(file);
const fileLink = path.join(req.path, encodedFile);
let linkAttributes = "";
let fileLocationEncoding = ""
console.log(fileLink)
let convertedString = fileLink.replace(/\\/g, '/');
console.log(convertedString)
// Check if the file is of a specific type that should open in a new window/tab
const fileExtension = path.extname(file).toLowerCase();
if (isFileTypeToOpenInNewTab(fileExtension)) {
fileLocationEncoding = 'http://localhost:3000/' + convertedString
linkAttributes = `onclick="window.open('${fileLocationEncoding}', 'fileWindow', 'width=600,height=400');"`;
return `<li ${linkAttributes}>${file}</li>`;
} else {
return `<li><a href="${fileLink}" ${linkAttributes}>${file}</a></li>`;
}
});
return res.send(`<ul>${links.join("")}</ul>`);
} else {
const fileExtension = path.extname(filePath).toLowerCase();
if (fileExtension === ".pdf") {
// For PDF files, send the file to be displayed in the browser.
const fileStream = fs.createReadStream(filePath);
fileStream.pipe(res);
} else {
// For other file types, set content type and send file.
const contentType = getContentType(fileExtension);
res.set("Content-Type", contentType);
return res.sendFile(filePath);
}
}
});
// Function to get content type based on file extension
function getContentType(fileExtension) {
switch (fileExtension) {
case ".txt":
return "text/plain";
case ".png":
return "image/png";
// Add more content types as needed
default:
return "application/octet-stream";
}
}
// Function to check if the file type should open in a new window/tab
function isFileTypeToOpenInNewTab(fileExtension) {
const supportedFileTypes = [".pdf", ".png", ".txt"]; // Add more file types as needed
return supportedFileTypes.includes(fileExtension);
}
const PORT = 3000;
app.listen(PORT, () => {
console.log("Server is running on http://localhost:" + PORT);
});