init
This commit is contained in:
76
server/db/db.js
Normal file
76
server/db/db.js
Normal file
@@ -0,0 +1,76 @@
|
||||
import chalk from 'chalk'
|
||||
import path from 'path';
|
||||
import fs from 'fs/promises';
|
||||
import { pathToFileURL } from 'url';
|
||||
|
||||
export default class Database {
|
||||
#nodes;
|
||||
#edges;
|
||||
#labels = {}
|
||||
|
||||
constructor() {
|
||||
this.getData()
|
||||
}
|
||||
|
||||
async getData() {
|
||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
||||
let dbJson;
|
||||
try {
|
||||
dbJson = JSON.parse(dbData);
|
||||
} catch {
|
||||
dbJson = []
|
||||
}
|
||||
this.#nodes = dbJson["nodes"];
|
||||
this.#edges = dbJson["edges"];
|
||||
|
||||
console.log(chalk.yellow("DB established."))
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
|
||||
// superKey = "nodes" || "edges"
|
||||
async writeData(superKey, key, value) {
|
||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
||||
let dbJson;
|
||||
try {
|
||||
dbJson = JSON.parse(dbData);
|
||||
} catch {
|
||||
dbJson = []
|
||||
}
|
||||
|
||||
dbJson[superKey][key] = value;
|
||||
|
||||
await fs.writeFile(path.join(process.cwd(), 'db/db.json'), JSON.stringify(dbJson, null, 2), 'utf8')
|
||||
}
|
||||
|
||||
async getLabelModels() {
|
||||
const labelHandlers = {};
|
||||
const labelDir = path.join(process.cwd(), 'src/model/labels');
|
||||
const files = await fs.readdir(labelDir);
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.endsWith('.js')) continue;
|
||||
|
||||
const label = path.basename(file, '.js');
|
||||
const modulePath = path.join(labelDir, file);
|
||||
const module = await import(pathToFileURL(modulePath).href);
|
||||
labelHandlers[label] = module.default;
|
||||
|
||||
if (!this.#labels[label]) {
|
||||
this.#labels[label] = [];
|
||||
}
|
||||
}
|
||||
return labelHandlers
|
||||
}
|
||||
|
||||
generateUserID() {
|
||||
let id = this.#labels["User"].length + 1;
|
||||
while (this.get.user(`user-${id}`)) {
|
||||
id++;
|
||||
}
|
||||
return `user-${id}`; // O(1) most of the time
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
return { nodes: this.#nodes }
|
||||
}
|
||||
}
|
||||
67
server/db/getDownloads.js
Normal file
67
server/db/getDownloads.js
Normal file
@@ -0,0 +1,67 @@
|
||||
// getDownloadsFiles.js
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
|
||||
const DOWNLOADS = path.join(os.homedir(), 'Downloads');
|
||||
|
||||
/**
|
||||
* Recursively get all files in Downloads with name, path, and inode.
|
||||
* @returns {Promise<Array<{name: string, path: string, inode: number|null}>>}
|
||||
*/
|
||||
async function getAllDownloadsFiles() {
|
||||
const files = [];
|
||||
|
||||
async function scan(dir) {
|
||||
let entries;
|
||||
try {
|
||||
entries = await fs.readdir(dir, { withFileTypes: true });
|
||||
} catch (err) {
|
||||
console.warn(`Cannot read directory: ${dir}`, err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
|
||||
try {
|
||||
// Get stats (inode = ino on Unix, null on Windows)
|
||||
// const stats = await fs.stat(fullPath);
|
||||
// const inode = stats.ino ?? null;
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
// Recurse into subdirectories
|
||||
await scan(fullPath);
|
||||
} else if (entry.isFile()) {
|
||||
const parsed = path.parse(entry.name); // splits name/ext
|
||||
const realExtension = path.extname(fullPath); // from full path (more reliable)
|
||||
|
||||
// Use realExtension if available, fallback to parsed.ext
|
||||
const extension = realExtension || parsed.ext;
|
||||
|
||||
// Build clean name + extension
|
||||
const nameWithExt = parsed.name + extension;
|
||||
|
||||
files.push({
|
||||
name: entry.name, // original (may hide ext)
|
||||
// nameWithExt: nameWithExt, // ALWAYS has correct ext
|
||||
// displayName: nameWithExt, // use this in UI
|
||||
// extension: extension.slice(1).toLowerCase() || null, // "jpg", null if none
|
||||
// path: fullPath,
|
||||
// inode: stats.ino ?? null,
|
||||
// size: stats.size,
|
||||
// mtime: stats.mtime.toISOString()
|
||||
});
|
||||
}
|
||||
// Skip symlinks, sockets, etc. (or handle if needed)
|
||||
} catch (err) {
|
||||
console.warn(`Cannot access: ${fullPath}`, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await scan(DOWNLOADS);
|
||||
return files;
|
||||
}
|
||||
|
||||
export default getAllDownloadsFiles;
|
||||
29
server/index.html
Normal file
29
server/index.html
Normal file
@@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Downloads</title>
|
||||
<link rel="icon" href="Quill.png">
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.draggable {
|
||||
-webkit-app-region: drag;
|
||||
height: 20px;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100vw;
|
||||
z-index: 999;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="draggable"></div>
|
||||
<webview src="http://localhost:3020" style="width:100vw; height:100vh;"></webview>
|
||||
</body>
|
||||
</html>
|
||||
115
server/index.js
Normal file
115
server/index.js
Normal file
@@ -0,0 +1,115 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs/promises'
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
import express from 'express';
|
||||
import cors from 'cors'
|
||||
import http from 'http'
|
||||
import chalk from 'chalk'
|
||||
import moment from 'moment'
|
||||
|
||||
import { initWebSocket } from './ws.js'
|
||||
import Database from "./db/db.js"
|
||||
import getAllDownloadsFiles from "./db/getDownloads.js"
|
||||
|
||||
class Server {
|
||||
db;
|
||||
UIPath = path.join(__dirname, '../ui')
|
||||
|
||||
registerRoutes(router) {
|
||||
router.get('/', this.index)
|
||||
router.get('/*', this.get)
|
||||
return router
|
||||
}
|
||||
|
||||
index = async (req, res) => {
|
||||
let filePath = path.join(this.UIPath, "index.html");
|
||||
let html = await fs.readFile(filePath, 'utf-8');
|
||||
|
||||
let downloads = await getAllDownloadsFiles()
|
||||
const jsonString = JSON.stringify(downloads, null, 2); // pretty-print optional
|
||||
const scriptTag = `<script type="application/json" id="jsonData">\n${jsonString}\n</script>`;
|
||||
|
||||
const headEndRegex = /<\/head\s*>/i;
|
||||
|
||||
const match = html.match(headEndRegex);
|
||||
const insertPos = match.index;
|
||||
let result = html.slice(0, insertPos) + scriptTag + '\n' + html.slice(insertPos);
|
||||
res.type('html').send(result);
|
||||
}
|
||||
|
||||
get = async (req, res) => {
|
||||
let url = req.url
|
||||
|
||||
let filePath = path.join(this.UIPath, url);
|
||||
|
||||
res.sendFile(filePath, (err) => {
|
||||
if (err) {
|
||||
console.error(`Error serving ${filePath}:`, err);
|
||||
res.status(err.status || 500).send('File not found or error serving file.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
logRequest(req, res, next) {
|
||||
const formattedDate = moment().format('M.D');
|
||||
const formattedTime = moment().format('h:mma');
|
||||
if(req.url.includes("/api/")) {
|
||||
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
} else {
|
||||
if(req.url === "/")
|
||||
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
logResponse(req, res, next) {
|
||||
const originalSend = res.send;
|
||||
res.send = function (body) {
|
||||
if(res.statusCode >= 400) {
|
||||
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
|
||||
} else {
|
||||
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
|
||||
}
|
||||
originalSend.call(this, body);
|
||||
};
|
||||
next();
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.db = new Database()
|
||||
const app = express();
|
||||
app.use(cors({ origin: '*' }));
|
||||
app.use(express.json());
|
||||
|
||||
app.use(this.logRequest);
|
||||
app.use(this.logResponse);
|
||||
|
||||
let router = express.Router();
|
||||
this.registerRoutes(router)
|
||||
app.use('/', router);
|
||||
|
||||
const server = http.createServer(app);
|
||||
initWebSocket(server);
|
||||
const PORT = 3020;
|
||||
server.listen(PORT, () => {
|
||||
console.log("\n")
|
||||
console.log(chalk.yellow("**************Downloads****************"))
|
||||
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||
console.log(chalk.yellow("***************************************"))
|
||||
console.log("\n")
|
||||
});
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log(chalk.red('Closing server...'));
|
||||
console.log(chalk.green('Database connection closed.'));
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
}
|
||||
|
||||
const server = new Server()
|
||||
31
server/ws.js
Normal file
31
server/ws.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { WebSocket, WebSocketServer } from 'ws';
|
||||
|
||||
let wss;
|
||||
|
||||
export function initWebSocket(server) {
|
||||
wss = new WebSocketServer({ server });
|
||||
|
||||
wss.on('connection', (ws, req) => {
|
||||
console.log('✅ New WebSocket client connected');
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('Client disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
console.log('WebSocket server initialized');
|
||||
}
|
||||
|
||||
// Broadcast a message to all connected clients
|
||||
export function broadcast(reqType, data) {
|
||||
if (!wss) return;
|
||||
|
||||
const payload = typeof data === 'string' ? data : JSON.stringify(data);
|
||||
const message = `${reqType}|${payload}`;
|
||||
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user