Add documentation to functions
This commit is contained in:
parent
0a0d9b9802
commit
34e991f017
5 changed files with 18 additions and 7 deletions
|
@ -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");
|
||||
|
|
|
@ -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: []) => {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue