preliminary work for guest account support
This commit is contained in:
parent
b6779a7d7c
commit
bbc655b3f9
11 changed files with 132 additions and 50 deletions
30
app/routes/adduser.ts
Normal file
30
app/routes/adduser.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import type {RequestHandler as Middleware, Router, Request, Response, NextFunction} from 'express';
|
||||
import express from "express";
|
||||
|
||||
import {db, createUser} from "../db";
|
||||
|
||||
const router: Router = express.Router();
|
||||
|
||||
const adminCheck: Middleware = (req: Request, res: Response, next: NextFunction) => {
|
||||
//@ts-ignore
|
||||
if (!req.user)
|
||||
return res.status(403).send("You are not authorized to perform this action");
|
||||
else {
|
||||
//@ts-ignore
|
||||
if (req.user.username != "admin")
|
||||
return res.status(403).send("You are not authorized to perform this action");
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
router.get("/adduser", adminCheck, (req: Request, res: Response, next: NextFunction) => {
|
||||
res.locals.filter = null;
|
||||
res.render("adduser", { user: req.user });
|
||||
});
|
||||
|
||||
router.post("/adduser", adminCheck, (req: Request, res: Response, next: NextFunction) => {
|
||||
createUser(req.body.username, req.body.password);
|
||||
res.redirect('/');
|
||||
});
|
||||
|
||||
export default router;
|
|
@ -1,13 +1,11 @@
|
|||
import type {MediaRow, UserRow} from '../types';
|
||||
import type {RequestHandler as Middleware} from 'express';
|
||||
import type {UserRow} from '../types';
|
||||
|
||||
import crypto from "crypto";
|
||||
import express from "express";
|
||||
import passport from "passport";
|
||||
import {Strategy as LocalStrategy} from "passport-local";
|
||||
|
||||
import { Strategy as LocalStrategy } from "passport-local";
|
||||
|
||||
import db from "../db";
|
||||
import {db} from "../db";
|
||||
|
||||
let router = express.Router();
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import type {RequestHandler as Middleware, Router, Request, Response} from 'express';
|
||||
import types from 'multer';
|
||||
import type {RequestHandler as Middleware, Router, Request, Response, NextFunction} from 'express';
|
||||
|
||||
import multer from "multer";
|
||||
import express from "express";
|
||||
|
@ -14,8 +13,8 @@ ffmpeg.setFfprobePath(ffprobepath.path);
|
|||
|
||||
import fs from "fs";
|
||||
|
||||
import db from "../db";
|
||||
import {checkAuth, convert, handleUpload} from "./middleware";
|
||||
import {db, createUser} from "../db";
|
||||
import {checkAuth, checkSharexAuth, createEmbedData, handleUpload} from "./middleware";
|
||||
import { MediaRow } from '../types';
|
||||
|
||||
function extension(str: String){
|
||||
|
@ -89,13 +88,12 @@ const fetchMedia: Middleware = (req, res, next) => {
|
|||
|
||||
let router = express.Router();
|
||||
|
||||
router.get("/", (req, res, next) => {
|
||||
// @ts-ignore, user is part of req header
|
||||
if (!req.user) { return res.render("home"); }
|
||||
router.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
if (!req.user)
|
||||
return res.render("home")
|
||||
next();
|
||||
}, fetchMedia, (req, res) => {
|
||||
}, fetchMedia, (req: Request, res: Response) => {
|
||||
res.locals.filter = null;
|
||||
// @ts-ignore, user is part of req header
|
||||
res.render("index", { user: req.user });
|
||||
});
|
||||
|
||||
|
@ -103,10 +101,10 @@ router.get("/gifv/:file", async (req, res, next) => {
|
|||
let url = `${req.protocol}://${req.get("host")}/uploads/${req.params.file}`;
|
||||
let width; let height;
|
||||
|
||||
let nameAndExtension = extension("uploads/" + req.params.file);
|
||||
let nameAndExtension = extension(`uploads/${req.params.file}`);
|
||||
if (nameAndExtension[1] == ".mp4" || nameAndExtension[1] == ".mov" || nameAndExtension[1] == ".webm") {
|
||||
ffmpeg()
|
||||
.input("uploads/" + req.params.file)
|
||||
.input(`uploads/${req.params.file}`)
|
||||
.inputFormat(nameAndExtension[1].substring(1))
|
||||
.ffprobe((err: Error, data: ffmpeg.FfprobeData) => {
|
||||
if (err) return next(err);
|
||||
|
@ -116,7 +114,7 @@ router.get("/gifv/:file", async (req, res, next) => {
|
|||
});
|
||||
} else if (nameAndExtension[1] == ".gif") {
|
||||
ffmpeg()
|
||||
.input("uploads/" + req.params.file)
|
||||
.input(`uploads/${req.params.file}`)
|
||||
.inputFormat("gif")
|
||||
.ffprobe((err: Error, data: ffmpeg.FfprobeData) => {
|
||||
if (err) return next(err);
|
||||
|
@ -130,16 +128,16 @@ router.get("/gifv/:file", async (req, res, next) => {
|
|||
}
|
||||
});
|
||||
|
||||
router.post("/", [upload.array("fileupload"), convert, handleUpload], (req: Request, res: Response) => {
|
||||
return res.redirect("/");
|
||||
router.post("/", [checkAuth, upload.array("fileupload"), createEmbedData, handleUpload], (req: Request, res: Response) => {
|
||||
res.redirect("/")
|
||||
});
|
||||
|
||||
router.post("/sharex", [checkAuth, upload.array("fileupload"), convert, handleUpload], (req: Request, res: Response) => {
|
||||
router.post("/sharex", [checkSharexAuth, upload.array("fileupload"), createEmbedData, handleUpload], (req: Request, res: Response) => {
|
||||
// @ts-ignore
|
||||
return res.send(`${req.protocol}://${req.get("host")}/uploads/${req.files[0].filename}`);
|
||||
});
|
||||
|
||||
router.post("/:id(\\d+)/delete", (req, res, next) => {
|
||||
router.post("/:id(\\d+)/delete", [checkAuth], (req: Request, res: Response, next: NextFunction) => {
|
||||
db.all("SELECT path FROM media WHERE id = ?", [ req.params.id ], (err: Error, path: Array<any>) => {
|
||||
if (err) { return next(err); }
|
||||
fs.unlink(`uploads/${path[0].path}`, (err => {
|
||||
|
|
|
@ -10,15 +10,21 @@ ffmpeg.setFfprobePath(ffprobepath.path);
|
|||
import fs from "fs";
|
||||
import process from "process";
|
||||
|
||||
import db from "../db";
|
||||
import {db} from "../db";
|
||||
|
||||
function extension(str: String){
|
||||
let file = str.split("/").pop();
|
||||
return [file.substr(0,file.lastIndexOf(".")),file.substr(file.lastIndexOf("."),file.length).toLowerCase()];
|
||||
}
|
||||
|
||||
//Checks ShareX key
|
||||
export const checkAuth: Middleware = (req: Request, res: Response, next: Function) => {
|
||||
export const checkAuth: Middleware = (req, res, next) => {
|
||||
if (!req.user) {
|
||||
return res.status(401);
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
export const checkSharexAuth: Middleware = (req, res, next) => {
|
||||
let auth = process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY";
|
||||
let key = null;
|
||||
|
||||
|
@ -38,8 +44,8 @@ export const checkAuth: Middleware = (req: Request, res: Response, next: Functio
|
|||
next();
|
||||
}
|
||||
|
||||
//Converts mp4 to gif and vice versa with ffmpeg
|
||||
export const convert: Middleware = (req: Request, res: Response, next: Function) => {
|
||||
//createEmbedDatas mp4 to gif and vice versa with ffmpeg
|
||||
export const createEmbedData: Middleware = (req, res, next) => {
|
||||
for (let file in req.files) {
|
||||
// @ts-ignore
|
||||
let nameAndExtension = extension(req.files[file].originalname);
|
||||
|
@ -58,13 +64,20 @@ export const convert: Middleware = (req: Request, res: Response, next: Function)
|
|||
if (err) return next(err);
|
||||
console.log(`oembed file created ${nameAndExtension[0]}${nameAndExtension[1]}.json`);
|
||||
});
|
||||
|
||||
/**if (nameAndExtension[1] == ".mp4") {
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
export const convert: Middleware = (req, res, next) => {
|
||||
for (let file in req.files) {
|
||||
// @ts-ignore
|
||||
let nameAndExtension = extension(req.files[file].originalname);
|
||||
if (nameAndExtension[1] == ".mp4" || nameAndExtension[1] == ".webm" || nameAndExtension[1] == ".mkv" || nameAndExtension[1] == ".avi" || nameAndExtension[1] == ".mov") {
|
||||
console.log("Converting " + nameAndExtension[0] + nameAndExtension[1] + " to gif");
|
||||
console.log(nameAndExtension[0] + nameAndExtension[1]);
|
||||
ffmpeg()
|
||||
.input(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
||||
.inputFormat("mp4")
|
||||
.inputFormat(nameAndExtension[1].substring(1))
|
||||
.outputFormat("gif")
|
||||
.output(`uploads/${nameAndExtension[0]}.gif`)
|
||||
.on("end", function() {
|
||||
|
@ -90,13 +103,11 @@ export const convert: Middleware = (req: Request, res: Response, next: Function)
|
|||
console.log(`Uploaded to uploads/${nameAndExtension[0]}.mp4`);
|
||||
})
|
||||
.run();
|
||||
}**/
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
export const handleUpload: Middleware = (req: Request, res: Response, next: Function) => {
|
||||
|
||||
export const handleUpload: Middleware = (req, res, next) => {
|
||||
if (!req.files || Object.keys(req.files).length === 0) {
|
||||
console.log("No files were uploaded");
|
||||
return res.status(400).send("No files were uploaded.");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue