From 34e991f017b7421fbcab331dec096c548fab8fb5 Mon Sep 17 00:00:00 2001 From: waveringana Date: Sun, 11 Dec 2022 06:31:19 +0000 Subject: [PATCH] Add documentation to functions --- app/routes/adduser.ts | 2 +- app/routes/index.ts | 3 ++- app/routes/middleware.ts | 8 +++++--- app/types/db.ts | 8 ++++++++ app/types/lib.ts | 4 ++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/routes/adduser.ts b/app/routes/adduser.ts index 044c38a..64cee7c 100644 --- a/app/routes/adduser.ts +++ b/app/routes/adduser.ts @@ -4,7 +4,7 @@ import express from "express"; import {db, createUser} from "../types/db"; const router: Router = express.Router(); - +/**Middleware to check if a user is actually signed in */ const adminCheck: Middleware = (req: Request, res: Response, next: NextFunction) => { if (!req.user) return res.status(403).send("You are not authorized to perform this action"); diff --git a/app/routes/index.ts b/app/routes/index.ts index 3cd1273..6308897 100644 --- a/app/routes/index.ts +++ b/app/routes/index.ts @@ -18,9 +18,10 @@ import {fileStorage, fileFilter} from "../types/multer"; import {checkAuth, checkSharexAuth, createEmbedData, handleUpload} from "./middleware"; 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) => { 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}'`; db.all(query, (err:Error, rows: []) => { diff --git a/app/routes/middleware.ts b/app/routes/middleware.ts index e3138d3..9be3789 100644 --- a/app/routes/middleware.ts +++ b/app/routes/middleware.ts @@ -20,6 +20,7 @@ export const checkAuth: Middleware = (req, res, next) => { next(); } +/**Checks shareX auth key */ export const checkSharexAuth: Middleware = (req, res, next) => { let auth = process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY"; let key = null; @@ -40,6 +41,7 @@ export const checkSharexAuth: Middleware = (req, res, next) => { next(); } +/**Creates oembed json file for embed metadata */ export const createEmbedData: Middleware = (req, res, next) => { const files = req.files as Express.Multer.File[] for (let file in files) { @@ -62,7 +64,7 @@ export const createEmbedData: Middleware = (req, res, next) => { } next(); } - +/** Converts video to gif and vice versa using ffmpeg */ export const convert: Middleware = (req, res, next) => { const files = req.files as Express.Multer.File[] 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) => { if (!req.file && !req.files) { console.log("No files were uploaded"); @@ -120,7 +122,7 @@ export const handleUpload: Middleware = (req, res, next) => { next(); } - +/**Inserts into media database */ function insertToDB (filename: string, expireDate: Date, username: string, next: NextFunction) { let params: MediaParams = [ filename, diff --git a/app/types/db.ts b/app/types/db.ts index c477574..b29d674 100644 --- a/app/types/db.ts +++ b/app/types/db.ts @@ -7,6 +7,7 @@ mkdirp.sync("./var/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) { 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) { return new Promise((resolve, reject) => { 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) { return new Promise((resolve, reject) => { 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) { return new Promise((resolve, reject) => { 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 { id? : number | string, username?: string expire? :Date } +/**A row for the media database */ export interface MediaRow { id? : number | string, path: string, @@ -64,12 +70,14 @@ export interface MediaRow { 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, username: string, diff --git a/app/types/lib.ts b/app/types/lib.ts index 9723274..84fbe42 100644 --- a/app/types/lib.ts +++ b/app/types/lib.ts @@ -6,12 +6,12 @@ declare global { } } } - +/**Splits a file name into its name and then its extension */ export function extension(str: string){ let file = str.split("/").pop(); return [file.substr(0,file.lastIndexOf(".")),file.substr(file.lastIndexOf("."),file.length).toLowerCase()]; } - +/**Type for user data */ export interface User { username: string; id?: string;