renaming, spawning forms if it does not exist, simplifying
This commit is contained in:
67
flame/getDownloads.js
Normal file
67
flame/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;
|
||||
30
flame/index.html
Normal file
30
flame/index.html
Normal file
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Parchment</title>
|
||||
<link rel="icon" href="Quill.png">
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
background-color: #6A2C1C;
|
||||
}
|
||||
|
||||
.draggable {
|
||||
-webkit-app-region: drag;
|
||||
height: 40px;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100vw;
|
||||
z-index: 999;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="draggable"></div>
|
||||
<webview src="http://localhost:80" style="width:100vw; height:100vh;"></webview>
|
||||
</body>
|
||||
</html>
|
||||
120
flame/index.js
Normal file
120
flame/index.js
Normal file
@@ -0,0 +1,120 @@
|
||||
import path from 'path';
|
||||
import express from 'express';
|
||||
import cors from 'cors'
|
||||
import http from 'http'
|
||||
import chalk from 'chalk'
|
||||
import * as z from 'zod';
|
||||
import forms from 'forms'
|
||||
|
||||
import { initWebSocket } from './ws.js'
|
||||
import util from './util.js'
|
||||
|
||||
export default class Server {
|
||||
store;
|
||||
|
||||
registerRoutes(router) {
|
||||
router.get('/*', this.get)
|
||||
return router
|
||||
}
|
||||
|
||||
index = async (req, res) => {
|
||||
let filePath = path.join(util.CAVE_PATH, "index.html");
|
||||
res.sendFile(filePath)
|
||||
}
|
||||
|
||||
get = async (req, res) => {
|
||||
let url = req.url
|
||||
let filePath;
|
||||
if(url.startsWith("/_")) {
|
||||
filePath = path.join(util.CAVE_PATH, url);
|
||||
} else if(url.includes("75820185")) {
|
||||
filePath = path.join(util.CAVE_PATH, url.split("75820185")[1]);
|
||||
} else {
|
||||
filePath = path.join(util.CAVE_PATH, "index.html");
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
}
|
||||
|
||||
async connectToForms(spawnForms) {
|
||||
try {
|
||||
await this.store.connect()
|
||||
}
|
||||
catch(e) {
|
||||
try {
|
||||
await spawnForms()
|
||||
await this.store.connect()
|
||||
}
|
||||
catch(e) {
|
||||
try {
|
||||
let tryReconnect = async (attempts = 5, interval = 100) => {
|
||||
for (let i = 0; i < attempts; i++) {
|
||||
try {
|
||||
return await this.store.connect();
|
||||
} catch (e) {
|
||||
if (i === attempts - 1) {throw e;}
|
||||
await new Promise(resolve => setTimeout(resolve, interval));
|
||||
}
|
||||
}
|
||||
}
|
||||
await tryReconnect(5, 100)
|
||||
}
|
||||
catch(e) {
|
||||
console.error("Could not spawn forms: ", e)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.store.register("log", z.toJSONSchema(z.object({
|
||||
time: z.string(),
|
||||
host: z.string(),
|
||||
ip: z.string(),
|
||||
path: z.string(),
|
||||
method: z.string()
|
||||
})))
|
||||
this.store.add("log", {
|
||||
time: "0100",
|
||||
host: "parchment.page",
|
||||
ip: "0.0.0.1",
|
||||
path: "/asd",
|
||||
method: "GET"
|
||||
})
|
||||
}
|
||||
|
||||
constructor(spawnForms) {
|
||||
const app = express();
|
||||
app.use(cors({ origin: '*' }));
|
||||
app.use(express.json());
|
||||
|
||||
app.use(util.logRequest);
|
||||
app.use(util.logResponse);
|
||||
|
||||
let router = express.Router();
|
||||
this.registerRoutes(router)
|
||||
app.use('/', router);
|
||||
|
||||
this.store = new forms.client()
|
||||
this.connectToForms(spawnForms)
|
||||
|
||||
const server = http.createServer(app);
|
||||
initWebSocket(server);
|
||||
|
||||
const PORT = 80;
|
||||
server.listen(PORT, () => {
|
||||
console.log("\n")
|
||||
console.log(chalk.yellow("**************Parchment****************"))
|
||||
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);
|
||||
}
|
||||
}
|
||||
40
flame/util.js
Normal file
40
flame/util.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import moment from 'moment'
|
||||
import chalk from 'chalk'
|
||||
import paths from 'path'
|
||||
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = paths.dirname(__filename);
|
||||
|
||||
export default class util {
|
||||
|
||||
static APP_PATH = paths.join(__dirname, "..")
|
||||
static CAVE_PATH = paths.join(__dirname, "../cave")
|
||||
static FLAME_PATH = __dirname
|
||||
static FORMS_PATH = paths.join(__dirname, "../forms")
|
||||
|
||||
static 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();
|
||||
}
|
||||
|
||||
static 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();
|
||||
}
|
||||
}
|
||||
31
flame/ws.js
Normal file
31
flame/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