parent
89663c696f
commit
8c3f2db3b2
10 changed files with 161 additions and 253 deletions
76
app/app.ts
76
app/app.ts
|
@ -12,13 +12,12 @@ const SQLiteStore = sqlite3(session);
|
|||
import fs from "fs";
|
||||
import http from "http";
|
||||
import path from "path";
|
||||
import { unlink } from 'fs/promises';
|
||||
|
||||
import authRouter from "./routes/auth";
|
||||
import indexRouter from "./routes/index";
|
||||
import adduserRouter from "./routes/adduser";
|
||||
|
||||
import {db, expire, createDatabase, updateDatabase, MediaRow, UserRow} from "./types/db";
|
||||
import {db, expire, createDatabase, updateDatabase, MediaRow} from "./types/db";
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
@ -69,31 +68,25 @@ function onError(error: any) {
|
|||
|
||||
|
||||
// Check if there is an existing DB or not, then check if it needs to be updated to new schema
|
||||
const row = db.prepare("SELECT * FROM sqlite_master WHERE name ='users' and type='table'").get();
|
||||
db.get("SELECT * FROM sqlite_master WHERE name ='users' and type='table'", async (err, row) => {
|
||||
if (!row) createDatabase(2);
|
||||
else checkVersion();
|
||||
});
|
||||
|
||||
if (!row) {
|
||||
createDatabase(2);
|
||||
} else {
|
||||
checkVersion();
|
||||
}
|
||||
|
||||
|
||||
function checkVersion() {
|
||||
// Using the synchronous API of better-sqlite3
|
||||
const row = db.prepare("PRAGMA user_version").get() as UserRow;
|
||||
|
||||
if (row && row.user_version) {
|
||||
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);
|
||||
} else {
|
||||
} else {
|
||||
// Because ver 1 does not have user_version set, we can safely assume that it is ver 1
|
||||
updateDatabase(1, 2);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function onListening() {
|
||||
const addr = server.address();
|
||||
const bind = typeof addr === "string"
|
||||
|
@ -134,38 +127,25 @@ app.use("/", adduserRouter);
|
|||
|
||||
app.use("/uploads", express.static("uploads"));
|
||||
|
||||
async function prune() {
|
||||
try {
|
||||
// Fetching all media entries
|
||||
const rows = db.prepare("SELECT * FROM media").all() as MediaRow[];
|
||||
console.log("Uploaded files: " + rows.length);
|
||||
console.log(rows);
|
||||
async function prune () {
|
||||
db.all("SELECT * FROM media", (err: Error, rows: []) => {
|
||||
console.log("Uploaded files: " + rows.length);
|
||||
console.log(rows);
|
||||
});
|
||||
|
||||
// Vacuuming the database
|
||||
console.log("Vacuuming database...");
|
||||
db.prepare("VACUUM").run();
|
||||
console.log("Vacuuming database...");
|
||||
db.run("VACUUM");
|
||||
|
||||
// Deleting expired media files
|
||||
const expiredRows= db.prepare("SELECT path FROM media WHERE expire < ?").all(Date.now()) as MediaRow[];
|
||||
db.each("SELECT path FROM media WHERE expire < ?", [Date.now()], (err: Error, row: MediaRow) => {
|
||||
console.log(`Expired row: ${row}`);
|
||||
fs.unlink(`uploads/${row.path}`, (err) => {
|
||||
if (err && err.errno == -4058) {
|
||||
console.log("File already deleted");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
for (const row of expiredRows) {
|
||||
console.log(`Expired row: ${row}`);
|
||||
try {
|
||||
await unlink(`uploads/${row.path}`);
|
||||
} catch (err: any) {
|
||||
if (err && err.code === 'ENOENT') {
|
||||
console.log("File already deleted");
|
||||
} else {
|
||||
console.error(`Failed to delete file: ${row.path}`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await expire("media", "expire", Date.now());
|
||||
|
||||
} catch (err) {
|
||||
console.error("Error in prune function:", err);
|
||||
}
|
||||
await expire("media", "expire", Date.now());
|
||||
}
|
||||
|
||||
setInterval(prune, 1000 * 60); //prune every minute
|
||||
setInterval(prune, 1000 * 60); //prune every minute
|
|
@ -9,33 +9,30 @@ import {db, UserRow} from "../types/db";
|
|||
const router = express.Router();
|
||||
|
||||
passport.use(new LocalStrategy(function verify(username, password, cb) {
|
||||
try {
|
||||
// Fetch user from database using better-sqlite3's synchronous API
|
||||
const row = db.prepare("SELECT * FROM users WHERE username = ?").get(username) as UserRow;
|
||||
|
||||
db.get("SELECT * FROM users WHERE username = ?", [username], function(err: Error, row: UserRow) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!row) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password."
|
||||
});
|
||||
}
|
||||
|
||||
// Synchronously hash the provided password with the stored salt
|
||||
const hashedPassword = crypto.pbkdf2Sync(password, row.salt, 310000, 32, "sha256");
|
||||
|
||||
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password."
|
||||
});
|
||||
}
|
||||
|
||||
return cb(null, row);
|
||||
|
||||
} catch (err) {
|
||||
return cb(err);
|
||||
}
|
||||
crypto.pbkdf2(password, row.salt, 310000, 32, "sha256", function(err, hashedPassword) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password."
|
||||
});
|
||||
}
|
||||
return cb(null, row);
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
passport.serializeUser(function(user:User, cb) {
|
||||
process.nextTick(function() {
|
||||
cb(null, {
|
||||
|
|
|
@ -17,32 +17,13 @@ import {fileStorage} from "../types/multer";
|
|||
import {checkAuth, checkSharexAuth, convertTo720p, createEmbedData, handleUpload} from "../types/middleware";
|
||||
|
||||
const upload = multer({ storage: fileStorage /**, fileFilter: fileFilter**/ }); //maybe make this a env variable?
|
||||
|
||||
/**Middleware to grab media from media database */
|
||||
const fetchMedia: Middleware = (req, res, next) => {
|
||||
try {
|
||||
const admin: boolean = req.user.username == "admin" ? true : false;
|
||||
/**Check if the user is an admin, if so, show all posts from all users */
|
||||
const query: string = admin ? "SELECT * FROM media" : `SELECT * FROM media WHERE username = ?`;
|
||||
const rows = (admin ? db.prepare(query).all() : db.prepare(query).all(req.user.username)) as MediaRow[];
|
||||
const files = rows.map((row: MediaRow) => {
|
||||
return {
|
||||
id: row.id,
|
||||
path: row.path,
|
||||
expire: row.expire,
|
||||
sername: row.username,
|
||||
url: "/" + row.id
|
||||
};
|
||||
});
|
||||
const admin: boolean = req.user.username == "admin" ? true : false;
|
||||
/**Check if the user is an admin, if so, show all posts from all users */
|
||||
const query: string = admin == true ? "SELECT * FROM media" : `SELECT * FROM media WHERE username = '${req.user.username}'`;
|
||||
|
||||
res.locals.files = files.reverse(); //reverse so newest files appear first
|
||||
res.locals.Count = files.length;
|
||||
next();
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
|
||||
/**db.all(query, (err:Error, rows: []) => {
|
||||
db.all(query, (err:Error, rows: []) => {
|
||||
if (err) return next(err);
|
||||
const files = rows.map((row: MediaRow)=> {
|
||||
return {
|
||||
|
@ -56,7 +37,7 @@ const fetchMedia: Middleware = (req, res, next) => {
|
|||
res.locals.files = files.reverse(); //reverse so newest files appear first
|
||||
res.locals.Count = files.length;
|
||||
next();
|
||||
});**/
|
||||
});
|
||||
};
|
||||
|
||||
const router = express.Router();
|
||||
|
|
219
app/types/db.ts
219
app/types/db.ts
|
@ -1,57 +1,13 @@
|
|||
//import sqlite3 from "sqlite3";
|
||||
import sqlite3 from "sqlite3";
|
||||
import mkdirp from "mkdirp";
|
||||
import crypto from "crypto";
|
||||
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
mkdirp.sync("./uploads");
|
||||
mkdirp.sync("./var/db");
|
||||
|
||||
export const db = new Database("./var/db/media.db");
|
||||
export const db = new sqlite3.Database("./var/db/media.db");
|
||||
|
||||
/**
|
||||
* Represents a generic row structure in a database table.
|
||||
*/
|
||||
export interface GenericRow {
|
||||
id? : number | string,
|
||||
username?: string
|
||||
expire? :Date
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a row structure in the media table.
|
||||
*/
|
||||
export interface MediaRow {
|
||||
id? : number | string,
|
||||
path: string,
|
||||
expire: Date,
|
||||
username?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* A tuple representing parameters for media-related operations.
|
||||
*/
|
||||
export type MediaParams = [
|
||||
path: string,
|
||||
expire: string | number | null,
|
||||
username?: string
|
||||
]
|
||||
|
||||
/**
|
||||
* Represents a row structure in the user table.
|
||||
*/
|
||||
export interface UserRow {
|
||||
id? : number | string,
|
||||
username: string,
|
||||
hashed_password: any,
|
||||
salt: any,
|
||||
user_version? : number
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the database tables and sets the user version pragma.
|
||||
* @param {number} version - The desired user version for the database.
|
||||
*/
|
||||
/**Create the database schema for the embedders app*/
|
||||
export function createDatabase(version: number){
|
||||
console.log("Creating database");
|
||||
|
||||
|
@ -61,9 +17,7 @@ export function createDatabase(version: number){
|
|||
hashed_password BLOB, \
|
||||
expire INTEGER, \
|
||||
salt BLOB \
|
||||
)")
|
||||
|
||||
createUser("admin", process.env.EBPASS || "changeme");
|
||||
)", () => createUser("admin", process.env.EBPASS || "changeme"));
|
||||
|
||||
db.run("CREATE TABLE IF NOT EXISTS media ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
|
@ -75,75 +29,58 @@ export function createDatabase(version: number){
|
|||
db.run(`PRAGMA user_version = ${version}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the database schema from an old version to a new one.
|
||||
* @param {number} oldVersion - The current version of the database.
|
||||
* @param {number} newVersion - The desired version to upgrade to.
|
||||
*/
|
||||
/**Updates old Database schema to new */
|
||||
export function updateDatabase(oldVersion: number, newVersion: number){
|
||||
if (oldVersion == 1) {
|
||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||
db.run("PRAGMA user_version = 2");
|
||||
db.run("ALTER TABLE media ADD COLUMN username TEXT");
|
||||
db.run("PRAGMA user_version = 2", (err) => {
|
||||
if(err) return;
|
||||
});
|
||||
db.run("ALTER TABLE media ADD COLUMN username TEXT", (err) => {
|
||||
if(err) return;
|
||||
});
|
||||
|
||||
db.run("ALTER TABLE users ADD COLUMN expire TEXT");
|
||||
db.run("ALTER TABLE users ADD COLUMN expire TEXT", (err) => {
|
||||
if(err) return;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a new media record into the media table.
|
||||
* @param {string} filename - The name of the file.
|
||||
* @param {string | number | null} expireDate - The expiration date for the media.
|
||||
* @param {string} username - The name of the user.
|
||||
*/
|
||||
export function insertToDB (filename: string, expireDate: string | number | null, username: string) {
|
||||
try {
|
||||
const params: MediaParams = [
|
||||
filename,
|
||||
expireDate,
|
||||
username
|
||||
];
|
||||
|
||||
const query = db.prepare("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)");
|
||||
query.run(...params);
|
||||
|
||||
console.log(`Inserted ${filename} to database`)
|
||||
/**Inserts into the media table */
|
||||
export function insertToDB (filename: string, expireDate: Date, username: string) {
|
||||
const params: MediaParams = [
|
||||
filename,
|
||||
expireDate,
|
||||
username
|
||||
];
|
||||
|
||||
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", params, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return err;
|
||||
}
|
||||
console.log(`Uploaded ${filename} to database`);
|
||||
if (expireDate == null)
|
||||
console.log("It will not expire");
|
||||
else if (expireDate != null || expireDate != undefined)
|
||||
console.log(`It will expire on ${expireDate}`);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for images in the database based on a name or keyword.
|
||||
* @param {string} imagename - The name or keyword to search for.
|
||||
* @param {boolean} partial - Whether to perform a partial search.
|
||||
*/
|
||||
/**Searches the database and returns images with partial or exact keysearches */
|
||||
export function searchImages(imagename: string, partial: boolean) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`searching for ${imagename}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the name of an image in the database.
|
||||
* @param {string} oldimagename - The current name of the image.
|
||||
* @param {string} newname - The new name for the image.
|
||||
*/
|
||||
export function updateImageName(oldimagename: string, newname:string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`updating ${oldimagename} to ${newname}`);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Adds a new user to the users table in the database.
|
||||
* @param {string} username - The name of the user.
|
||||
* @param {string} password - The user's password.
|
||||
*/
|
||||
|
||||
/**Inserts a new user to the database */
|
||||
export function createUser(username: string, password: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`Creating user ${username}`);
|
||||
|
@ -159,50 +96,70 @@ export function createUser(username: string, password: string) {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the path for a file from the media table based on the given ID.
|
||||
*
|
||||
* @param {number|string} id - The ID of the record whose path needs to be fetched.
|
||||
* @returns {Promise<any[]>} A promise that resolves with the selected paths.
|
||||
* The resolved value is an array of records (usually containing a single item).
|
||||
* @throws Will reject the promise with an error if the query fails.
|
||||
*/
|
||||
export function getPath(id: number | string): Promise<any[]> {
|
||||
/**Selects the path for a file given ID */
|
||||
export function getPath(id: number | string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const query = db.prepare("SELECT path FROM media WHERE id = ?");
|
||||
resolve(query.all(id));
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
const query = "SELECT path FROM media WHERE id = ?";
|
||||
|
||||
db.get(query, [id], (err: Error, path: object) => {
|
||||
if (err) {reject(err);}
|
||||
resolve(path);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a row from a specified table based on its ID.
|
||||
*
|
||||
* @param {string} database - The name of the database table.
|
||||
* @param {number | string} id - The ID of the row to delete.
|
||||
*/
|
||||
export function deleteId(database: string, id: number | string): void {
|
||||
const deleteQuery = `DELETE FROM ${database} WHERE id = ?`;
|
||||
db.prepare(deleteQuery).run(id);
|
||||
/**Deletes from database given an ID */
|
||||
export function deleteId(database: string, id: number | string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = `DELETE FROM ${database} WHERE id = ?`;
|
||||
|
||||
db.run(query, [id], (err: Error) => {
|
||||
if (err) {reject(err); return;}
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes rows from a table that are older than a given expiration date.
|
||||
*
|
||||
* @param {string} database - The name of the database table.
|
||||
* @param {string} column - The column name used for date comparison.
|
||||
* @param {number} expiration - The expiration date in UNIX time.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
/**Expires a database row given a Date in unix time */
|
||||
export function expire(database: string, column: string, expiration:number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const rows: GenericRow[] = db.query(`SELECT * FROM ${database} WHERE ${column} < ?`).all(expiration);
|
||||
const query = `SELECT * FROM ${database} WHERE ${column} < ?`;
|
||||
|
||||
for (const row of rows) {
|
||||
deleteId(database, row.id);
|
||||
}
|
||||
db.each(query, [expiration], async (err: Error, row: GenericRow) => {
|
||||
if (err) reject(err);
|
||||
await deleteId(database, row.id);
|
||||
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**A generic database row */
|
||||
export interface GenericRow {
|
||||
id? : number | string,
|
||||
username?: string
|
||||
expire? :Date
|
||||
}
|
||||
|
||||
/**A row for the media database */
|
||||
export interface MediaRow {
|
||||
id? : number | string,
|
||||
path: string,
|
||||
expire: Date,
|
||||
username?: string
|
||||
}
|
||||
|
||||
/**Params type for doing work with media database */
|
||||
export type MediaParams = [
|
||||
path: string,
|
||||
expire: Date,
|
||||
username?: string
|
||||
]
|
||||
|
||||
/**A row for the user database */
|
||||
export interface UserRow {
|
||||
id? : number | string,
|
||||
username: string,
|
||||
hashed_password: any,
|
||||
salt: any
|
||||
}
|
|
@ -151,13 +151,10 @@ export const handleUpload: Middleware = (req, res, next) => {
|
|||
console.log("No files were uploaded");
|
||||
return res.status(400).send("No files were uploaded.");
|
||||
}
|
||||
|
||||
console.log("running handleUpload")
|
||||
|
||||
const files = (req.files) ? req.files as Express.Multer.File[] : req.file; //Check if a single file was uploaded or multiple
|
||||
console.log(files);
|
||||
const username = (req.user) ? req.user.username : "sharex"; //if no username was provided, we can presume that it is sharex
|
||||
const expireDate: number | null = (req.body.expire) ? Date.now() + (req.body.expire * 24 * 60 * 60 * 1000) : null;
|
||||
const expireDate: Date = (req.body.expire) ? new Date(Date.now() + (req.body.expire * 24 * 60 * 60 * 1000)) : null;
|
||||
|
||||
if (files instanceof Array) {
|
||||
for (const file in files) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {Request} from "express";
|
||||
import multer, {FileFilterCallback, MulterError} from "multer";
|
||||
import multer, {FileFilterCallback} from "multer";
|
||||
|
||||
import {db, MediaRow} from "./db";
|
||||
import {extension} from "./lib";
|
||||
|
@ -22,26 +22,27 @@ export const fileStorage = multer.diskStorage({
|
|||
): void => {
|
||||
const nameAndExtension = extension(file.originalname);
|
||||
console.log(`Uploading ${file}`);
|
||||
try {
|
||||
console.log("querying")
|
||||
const query = db.query(`SELECT * FROM media WHERE path = ?`);
|
||||
const exists = query.all(nameAndExtension[0] + nameAndExtension[1]);
|
||||
console.log(exists)
|
||||
|
||||
if (exists.length !== 0) {
|
||||
const suffix = (new Date().getTime() / 1000).toString();
|
||||
const newName = (request.body.title || nameAndExtension[0]) + "-" + suffix + nameAndExtension[1];
|
||||
callback(null, newName);
|
||||
console.log("ran callback with suffix")
|
||||
} else {
|
||||
const newName = (request.body.title || nameAndExtension[0]) + nameAndExtension[1];
|
||||
callback(null, newName);
|
||||
console.log("ran callback")
|
||||
db.all("SELECT * FROM media WHERE path = ?", [nameAndExtension[0] + nameAndExtension[1]], (err: Error, exists: []) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(err, null);
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.log(err);
|
||||
callback(err, null);
|
||||
}
|
||||
if (exists.length != 0) {
|
||||
const suffix = new Date().getTime() / 1000;
|
||||
|
||||
if (request.body.title == "" || request.body.title == null || request.body.title == undefined) {
|
||||
callback(null, nameAndExtension[0] + "-" + suffix + nameAndExtension[1]);
|
||||
} else {
|
||||
callback(null, request.body.title + "-" + suffix + nameAndExtension[1]);
|
||||
}
|
||||
} else {
|
||||
if (request.body.title == "" || request.body.title == null || request.body.title == undefined) {
|
||||
callback(null, nameAndExtension[0] + nameAndExtension[1]);
|
||||
} else {
|
||||
callback(null, request.body.title + nameAndExtension[1]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
1
index.ts
1
index.ts
|
@ -1 +0,0 @@
|
|||
console.log("Hello via Bun!");
|
|
@ -61,14 +61,11 @@
|
|||
"@types/probe-image-size": "^7.2.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
||||
"@typescript-eslint/parser": "^5.46.1",
|
||||
"bun-types": "^1.0.2",
|
||||
"copyfiles": "^2.4.1",
|
||||
"eslint": "^8.28.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"ts-node": "^10.9.1",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "^4.9.3"
|
||||
},
|
||||
"module": "index.ts",
|
||||
"type": "module"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"types": ["bun-types"],
|
||||
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||
|
||||
/* Projects */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue