Add documentation to functions

This commit is contained in:
waveringana 2022-12-11 06:31:19 +00:00
parent 0a0d9b9802
commit 34e991f017
5 changed files with 18 additions and 7 deletions

View file

@ -4,7 +4,7 @@ import express from "express";
import {db, createUser} from "../types/db"; import {db, createUser} from "../types/db";
const router: Router = express.Router(); const router: Router = express.Router();
/**Middleware to check if a user is actually signed in */
const adminCheck: Middleware = (req: Request, res: Response, next: NextFunction) => { const adminCheck: Middleware = (req: Request, res: Response, next: NextFunction) => {
if (!req.user) if (!req.user)
return res.status(403).send("You are not authorized to perform this action"); return res.status(403).send("You are not authorized to perform this action");

View file

@ -18,9 +18,10 @@ import {fileStorage, fileFilter} from "../types/multer";
import {checkAuth, checkSharexAuth, createEmbedData, handleUpload} from "./middleware"; import {checkAuth, checkSharexAuth, createEmbedData, handleUpload} from "./middleware";
let upload = multer({ storage: fileStorage /**, fileFilter: fileFilter**/ }); //maybe make this a env variable? let 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) => { const fetchMedia: Middleware = (req, res, next) => {
let admin: boolean = req.user.username == "admin" ? true : false let admin: boolean = req.user.username == "admin" ? true : false
/**Check if the user is an admin, if so, show all posts from all users */
let query: string = admin == true ? "SELECT * FROM media" : `SELECT * FROM media WHERE username = '${req.user.username}'`; let query: string = admin == true ? "SELECT * FROM media" : `SELECT * FROM media WHERE username = '${req.user.username}'`;
db.all(query, (err:Error, rows: []) => { db.all(query, (err:Error, rows: []) => {

View file

@ -20,6 +20,7 @@ export const checkAuth: Middleware = (req, res, next) => {
next(); next();
} }
/**Checks shareX auth key */
export const checkSharexAuth: Middleware = (req, res, next) => { export const checkSharexAuth: Middleware = (req, res, next) => {
let auth = process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY"; let auth = process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY";
let key = null; let key = null;
@ -40,6 +41,7 @@ export const checkSharexAuth: Middleware = (req, res, next) => {
next(); next();
} }
/**Creates oembed json file for embed metadata */
export const createEmbedData: Middleware = (req, res, next) => { export const createEmbedData: Middleware = (req, res, next) => {
const files = req.files as Express.Multer.File[] const files = req.files as Express.Multer.File[]
for (let file in files) { for (let file in files) {
@ -62,7 +64,7 @@ export const createEmbedData: Middleware = (req, res, next) => {
} }
next(); next();
} }
/** Converts video to gif and vice versa using ffmpeg */
export const convert: Middleware = (req, res, next) => { export const convert: Middleware = (req, res, next) => {
const files = req.files as Express.Multer.File[] const files = req.files as Express.Multer.File[]
for (let file in files) { for (let file in files) {
@ -100,7 +102,7 @@ export const convert: Middleware = (req, res, next) => {
} }
} }
} }
/**Middleware for handling uploaded files. Inserts it into the database */
export const handleUpload: Middleware = (req, res, next) => { export const handleUpload: Middleware = (req, res, next) => {
if (!req.file && !req.files) { if (!req.file && !req.files) {
console.log("No files were uploaded"); console.log("No files were uploaded");
@ -120,7 +122,7 @@ export const handleUpload: Middleware = (req, res, next) => {
next(); next();
} }
/**Inserts into media database */
function insertToDB (filename: string, expireDate: Date, username: string, next: NextFunction) { function insertToDB (filename: string, expireDate: Date, username: string, next: NextFunction) {
let params: MediaParams = [ let params: MediaParams = [
filename, filename,

View file

@ -7,6 +7,7 @@ mkdirp.sync("./var/db");
export const db = new sqlite3.Database("./var/db/media.db"); export const db = new sqlite3.Database("./var/db/media.db");
/**Inserts a new user to the database */
export function createUser(username: string, password: string) { export function createUser(username: string, password: string) {
let salt = crypto.randomBytes(16); let salt = crypto.randomBytes(16);
@ -17,6 +18,7 @@ export function createUser(username: string, password: string) {
]); ]);
} }
/**Selects a path for a file given ID */
export function getPath(id: number | string) { export function getPath(id: number | string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let query: string = `SELECT path FROM media WHERE id = ?`; let query: string = `SELECT path FROM media WHERE id = ?`;
@ -28,6 +30,7 @@ export function getPath(id: number | string) {
}) })
} }
/**Deletes from database given an ID */
export function deleteId(database: string, id: number | string) { export function deleteId(database: string, id: number | string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let query: string = `DELETE FROM ${database} WHERE id = ?` let query: string = `DELETE FROM ${database} WHERE id = ?`
@ -39,6 +42,7 @@ export function deleteId(database: string, id: number | string) {
}) })
} }
/**Expires a database row given a Date in unix time */
export function expire(database: string, column: string, expiration:number) { export function expire(database: string, column: string, expiration:number) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let query: string = `SELECT * FROM ${database} WHERE ${column} < ?`; let query: string = `SELECT * FROM ${database} WHERE ${column} < ?`;
@ -51,12 +55,14 @@ export function expire(database: string, column: string, expiration:number) {
}) })
} }
/**A generic database row */
export interface GenericRow { export interface GenericRow {
id? : number | string, id? : number | string,
username?: string username?: string
expire? :Date expire? :Date
} }
/**A row for the media database */
export interface MediaRow { export interface MediaRow {
id? : number | string, id? : number | string,
path: string, path: string,
@ -64,12 +70,14 @@ export interface MediaRow {
username?: string username?: string
} }
/**Params type for doing work with media database */
export type MediaParams = [ export type MediaParams = [
path: string, path: string,
expire: Date, expire: Date,
username?: string username?: string
] ]
/**A row for the user database */
export interface UserRow { export interface UserRow {
id? : Number, id? : Number,
username: string, username: string,

View file

@ -6,12 +6,12 @@ declare global {
} }
} }
} }
/**Splits a file name into its name and then its extension */
export function extension(str: string){ export function extension(str: string){
let file = str.split("/").pop(); let file = str.split("/").pop();
return [file.substr(0,file.lastIndexOf(".")),file.substr(file.lastIndexOf("."),file.length).toLowerCase()]; return [file.substr(0,file.lastIndexOf(".")),file.substr(file.lastIndexOf("."),file.length).toLowerCase()];
} }
/**Type for user data */
export interface User { export interface User {
username: string; username: string;
id?: string; id?: string;