-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogic.js
181 lines (148 loc) · 5.14 KB
/
logic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const fs = require("fs");
const https = require("https");
const path = require('path');
const crypto = require('crypto');
const fetch = require('node-fetch');
const AdmZip = require('adm-zip');
const http = require('http');
const { URL } = require('url');
const conf = require("config");
const folder = './public/content/';
let fileList = [];
let fileHashes = {};
let lastModifiedHeader = null;
let io = null;
function setVars(ioVar) {
io = ioVar;
}
function handleSocketConnection(socket, config) {
socket.emit("filelist", fileList);
socket.emit("settings", {
contentIntervall: config.contentIntervall,
backgroundColor: config.backgroundColor
});
socket.on('requpdate', () => {
socket.emit("filelist", fileList);
socket.emit("settings", {
contentIntervall: config.contentIntervall
});
});
}
function requireHTTPS(req, res, next) {
if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
return res.redirect('https://' + req.get('host') + req.url);
}
next();
}
function UpdateContent() {
const files = fs.readdirSync(folder);
const validExtensions = ['.pdf', '.mp4', '.webm', '.jpg', '.jpeg', '.png', '.gif', '.svg', '.webp'];
const newFileHashes = {};
files.forEach(file => {
const filePath = path.join(folder, file);
const isFile = fs.lstatSync(filePath).isFile();
const isValidExtension = validExtensions.some(ext => file.endsWith(ext));
if (isFile && isValidExtension) {
newFileHashes[file] = computeFileHash(filePath);
}
});
const hasChanges =
Object.keys(newFileHashes).length !== Object.keys(fileHashes).length || // Different number of files
Object.keys(newFileHashes).some(fileName => newFileHashes[fileName] !== fileHashes[fileName]); // Different file content
fileList = Object.keys(newFileHashes); // Update the fileList
fileHashes = newFileHashes; // Update the stored hashes for the next comparison
if (hasChanges) {
console.log("" + new Date());
console.log("Content changed!");
console.log(fileList);
io.emit("update", {}); // Make sure to import the `io` object or pass it as an argument
}
}
function DownloadZipFile(config) {
downloadAndExtractZip(config.zipURL, folder)
.then(() => {})
.catch(err => console.error('Error:', err));
}
function computeFileHash(filePath) {
const fileContent = fs.readFileSync(filePath);
const hash = crypto.createHash('sha256');
hash.update(fileContent);
return hash.digest('hex');
}
function getPortFromUrl(urlString) {
const url = new URL(urlString);
// If the port is specified in the URL, use it
if (url.port) {
return parseInt(url.port, 10);
}
// If not, determine the default port based on the protocol
switch (url.protocol) {
case 'http:':
return 80;
case 'https:':
return 443;
default:
throw new Error('Unsupported protocol');
}
}
async function downloadAndExtractZip(url, outputPath) {
const parsedUrl = new URL(url);
const port = getPortFromUrl(url);
const options = {
method: 'HEAD',
host: parsedUrl.hostname,
port: port,
path: parsedUrl.pathname
};
const requestModule = parsedUrl.protocol === 'https:' ? https : http;
requestModule.request(options, async (res) => {
const newLastModified = res.headers['last-modified'];
if (newLastModified && newLastModified !== lastModifiedHeader) {
// ZIP file has potentially changed, download and extract it
const response = await fetch(url);
const buffer = await response.buffer();
// Check if directory exists
if (!fs.existsSync(`./public/download/`)) {
// If not, create the directory
fs.mkdirSync('./public/download/', { recursive: true });
}
const zipPath = `./public/download/temp.zip`;
fs.writeFileSync(zipPath, buffer);
if (!conf.config.keepFiles) deleteAllFilesFromFolder('./public/content');
const zip = new AdmZip(zipPath);
zip.extractAllTo(outputPath, true);
fs.unlinkSync(zipPath);
lastModifiedHeader = newLastModified; // Update the last modified header for the next check
}
}).end();
}
function deleteAllFilesFromFolder(directoryPath) {
// Read the directory contents
const files = fs.readdirSync(directoryPath);
for (const file of files) {
const filePath = path.join(directoryPath, file);
const fileStats = fs.statSync(filePath);
// Check if it's a file and delete
if (fileStats.isFile()) {
fs.unlinkSync(filePath);
console.log(`Deleted: ${filePath}`);
} else {
console.warn(`Skipped (not a file): ${filePath}`);
}
}
}
function deleteLastLines(linesToDelete) {
for (let i = 0; i < linesToDelete; i++) {
process.stdout.moveCursor(0, -1); // move up one line
process.stdout.clearLine(1); // clear line to the right of the cursor
}
}
module.exports = {
handleSocketConnection,
requireHTTPS,
UpdateContent,
DownloadZipFile,
deleteAllFilesFromFolder,
deleteLastLines,
setVars
};