preliminary work to add settings page
This commit is contained in:
parent
4a79d63096
commit
3b7ada6e5d
8 changed files with 180 additions and 45 deletions
15
app/app.ts
15
app/app.ts
|
@ -1,4 +1,4 @@
|
|||
const version = 1.9;
|
||||
const version = 1.12;
|
||||
|
||||
import "dotenv";
|
||||
|
||||
|
@ -16,6 +16,7 @@ import path from "path";
|
|||
import authRouter from "./routes/auth";
|
||||
import indexRouter from "./routes/index";
|
||||
import adduserRouter from "./routes/adduser";
|
||||
import settingsRouter from "./routes/settings";
|
||||
|
||||
import {db, expire, createDatabase, updateDatabase, MediaRow} from "./lib/db";
|
||||
|
||||
|
@ -69,7 +70,7 @@ function onError(error: any) {
|
|||
|
||||
// Check if there is an existing DB or not, then check if it needs to be updated to new schema
|
||||
db.get("SELECT * FROM sqlite_master WHERE name ='users' and type='table'", async (err, row) => {
|
||||
if (!row) createDatabase(2);
|
||||
if (!row) createDatabase(3);
|
||||
else checkVersion();
|
||||
});
|
||||
|
||||
|
@ -77,12 +78,11 @@ function checkVersion () {
|
|||
db.get("PRAGMA user_version", (err: Error, row: any) => {
|
||||
if (row && row.user_version) {
|
||||
const version = row.user_version;
|
||||
if (version != 2) console.log("DATABASE IS OUTDATED");
|
||||
//no future releases yet, and else statement handles version 1
|
||||
//updateDatabase(version, 2);
|
||||
if (version != 3) console.log("DATABASE IS OUTDATED");
|
||||
updateDatabase(version, 3);
|
||||
} else {
|
||||
// Because ver 1 does not have user_version set, we can safely assume that it is ver 1
|
||||
updateDatabase(1, 2);
|
||||
updateDatabase(1, 3);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -124,6 +124,7 @@ app.use(passport.authenticate("session"));
|
|||
app.use("/", indexRouter);
|
||||
app.use("/", authRouter);
|
||||
app.use("/", adduserRouter);
|
||||
app.use("/", settingsRouter);
|
||||
|
||||
app.use("/uploads", express.static("uploads"));
|
||||
|
||||
|
@ -141,6 +142,8 @@ async function prune () {
|
|||
fs.unlink(`uploads/${row.path}`, (err) => {
|
||||
if (err && err.errno == -4058) {
|
||||
console.log("File already deleted");
|
||||
} else {
|
||||
if (err) console.log(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -31,6 +31,14 @@ export function createDatabase(version: number) {
|
|||
)"
|
||||
);
|
||||
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
downsclaing BOOLEAN, \
|
||||
namerandomization BOOLEAN \
|
||||
)"
|
||||
);
|
||||
|
||||
db.run(`PRAGMA user_version = ${version}`);
|
||||
}
|
||||
|
||||
|
@ -38,9 +46,10 @@ export function createDatabase(version: number) {
|
|||
export function updateDatabase(oldVersion: number, newVersion: number) {
|
||||
if (oldVersion == 1) {
|
||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||
db.run("PRAGMA user_version = 2", (err) => {
|
||||
db.run("PRAGMA user_version = 3", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
|
||||
db.run("ALTER TABLE media ADD COLUMN username TEXT", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
|
@ -49,6 +58,20 @@ export function updateDatabase(oldVersion: number, newVersion: number) {
|
|||
if (err) return;
|
||||
});
|
||||
}
|
||||
if (oldVersion == 2) {
|
||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||
db.run("PRAGMA user_version = 3", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
downsclaing BOOLEAN, \
|
||||
namerandomization BOOLEAN \
|
||||
)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**Inserts into the media table */
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
max-width: 400px;
|
||||
margin: 50px auto;
|
||||
padding: 25px;
|
||||
background: #11111;
|
||||
background: #111111;
|
||||
border: 1px solid #e6e6e6;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
|
44
app/routes/settings.ts
Normal file
44
app/routes/settings.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import type {
|
||||
RequestHandler as Middleware,
|
||||
Request,
|
||||
Response,
|
||||
NextFunction,
|
||||
} from "express";
|
||||
|
||||
import express from "express";
|
||||
|
||||
import { db, UserRow } from "../lib/db";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const fetchUsers = (): Promise<[UserRow]> => {
|
||||
const query = "SELECT * FROM users";
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
db.all(query, (err: Error, rows: [UserRow]) => {
|
||||
if (err) reject(err);
|
||||
resolve(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const fetchSettings: Middleware = async (req, res, next) => {
|
||||
res.locals.users = req.user.username == "admin" ? await fetchUsers() : null;
|
||||
next();
|
||||
};
|
||||
|
||||
router.get(
|
||||
"/settings",
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
if (!req.user) return res.render("home");
|
||||
console.log(req.user);
|
||||
next();
|
||||
},
|
||||
fetchSettings,
|
||||
(req: Request, res: Response) => {
|
||||
res.locals.filter = null;
|
||||
req.user.username == "admin" ? res.render("settings", { user: req.user, userList: res.locals.users }) : res.redirect("/");
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
|
@ -1,37 +1,40 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Embedder</title>
|
||||
<link rel="stylesheet" href="/css/base.css">
|
||||
<link rel="stylesheet" href="/css/index.css">
|
||||
<link rel="stylesheet" href="/css/login.css">
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
|
||||
<link rel="manifest" href="/site.webmanifest">
|
||||
</head>
|
||||
<body>
|
||||
<section class="prompt">
|
||||
<h3>Embedder</h3>
|
||||
<h1>Sign in</h1>
|
||||
<form action="/login/password" method="post">
|
||||
<section>
|
||||
<label for="username">Username</label>
|
||||
<input id="username" name="username" type="text" autocomplete="username" required autofocus>
|
||||
</section>
|
||||
<section>
|
||||
<label for="current-password">Password</label>
|
||||
<input id="current-password" name="password" type="password" autocomplete="current-password" required>
|
||||
</section>
|
||||
<button type="submit">Sign in</button>
|
||||
</form>
|
||||
<hr>
|
||||
</section>
|
||||
<footer class="info">
|
||||
<p><a href="https://l.nekomimi.pet/project">Created by Wavering Ana</a></p>
|
||||
<p><a href="https://github.com/WaveringAna/Embedder">Github</a></p>
|
||||
</footer>
|
||||
</body>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Embedder</title>
|
||||
<link rel="stylesheet" href="/css/base.css">
|
||||
<link rel="stylesheet" href="/css/index.css">
|
||||
<link rel="stylesheet" href="/css/login.css">
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
|
||||
<link rel="manifest" href="/site.webmanifest">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<section class="prompt">
|
||||
<h3>Embedder</h3>
|
||||
<h1>Sign in</h1>
|
||||
<form action="/login/password" method="post">
|
||||
<section>
|
||||
<label for="username">Username</label>
|
||||
<input id="username" name="username" type="text" autocomplete="username" required autofocus>
|
||||
</section>
|
||||
<section>
|
||||
<label for="current-password">Password</label>
|
||||
<input id="current-password" name="password" type="password" autocomplete="current-password" required>
|
||||
</section>
|
||||
<button type="submit">Sign in</button>
|
||||
</form>
|
||||
<hr>
|
||||
</section>
|
||||
<footer class="info">
|
||||
<p><a href="https://l.nekomimi.pet/project">Created by Wavering Ana</a></p>
|
||||
<p><a href="https://github.com/WaveringAna/Embedder">Github</a></p>
|
||||
</footer>
|
||||
</body>
|
||||
|
||||
</html>
|
61
app/views/settings.ejs
Normal file
61
app/views/settings.ejs
Normal file
|
@ -0,0 +1,61 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Embedder</title>
|
||||
<link rel="stylesheet" href="/css/base.css">
|
||||
<link rel="stylesheet" href="/css/index.css">
|
||||
<link rel="stylesheet" href="/css/login.css">
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
|
||||
<link rel="manifest" href="/site.webmanifest">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<style>
|
||||
section .settings {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin: 0 auto;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
</style>
|
||||
<section class="prompt">
|
||||
<h3><a href="/">Embedder</a></h3>
|
||||
<h1>Settings</h1>
|
||||
<form action="/settings" method="post">
|
||||
<section class="settings">
|
||||
<label for="fileNaming">How do you want to name files?</label>
|
||||
<select id="fileNaming" name="fileNaming">
|
||||
<option value="original">Original Filename</option>
|
||||
<option value="random">Random</option>
|
||||
</select>
|
||||
</section>
|
||||
<% if (user.name == "admin" || user.username == "admin") { %>
|
||||
<section class="settings">
|
||||
<label for="downscaling">Allow video downscaling for better video embedding?</label>
|
||||
<select id="downscaling" name="downscaling">
|
||||
<option value="true">Yes</option>
|
||||
<option value="false">No</option>
|
||||
</select>
|
||||
</section>
|
||||
<section class="settings">
|
||||
<label for="userList">List of guest accounts: </label>
|
||||
<span><%= JSON.stringify(userList.map(obj => obj.username)) %></span>
|
||||
</section>
|
||||
<% } %>
|
||||
<button type="submit">Apply Settings</button>
|
||||
</form>
|
||||
<hr>
|
||||
</section>
|
||||
<footer class="info">
|
||||
<p><a href="https://l.nekomimi.pet/project">Created by Wavering Ana</a></p>
|
||||
<p><a href="https://github.com/WaveringAna/Embedder">Github</a></p>
|
||||
</footer>
|
||||
</body>
|
||||
|
||||
</html>
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -6,7 +6,7 @@
|
|||
"packages": {
|
||||
"": {
|
||||
"name": "embedder",
|
||||
"version": "1.10.1",
|
||||
"version": "1.11",
|
||||
"hasInstallScript": true,
|
||||
"license": "Unlicense",
|
||||
"dependencies": {
|
||||
|
|
|
@ -26,7 +26,8 @@
|
|||
"copy-files": "copyfiles -a -u 1 app/public/* app/views/* app/public/**/* app/views/**/* dist/",
|
||||
"tsc": "tsc",
|
||||
"postinstall": "npm run tsc && npm run copy-files",
|
||||
"build": "npm run clean && npm run copy-files && npm run tsc"
|
||||
"build": "npm run clean && npm run copy-files && npm run tsc",
|
||||
"dev": "ts-node-dev --respawn --watch ./app/views/*.ejs --transpile-only ./app/app.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ffmpeg-installer/ffmpeg": "^1.1.0",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue