parent
89663c696f
commit
8c3f2db3b2
10 changed files with 161 additions and 253 deletions
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]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue