linting
This commit is contained in:
parent
34e991f017
commit
b217248293
13 changed files with 1229 additions and 406 deletions
39
.eslintrc.js
Normal file
39
.eslintrc.js
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
module.exports = {
|
||||||
|
"env": {
|
||||||
|
"browser": true,
|
||||||
|
"es2021": true,
|
||||||
|
"node": true
|
||||||
|
},
|
||||||
|
"extends": [
|
||||||
|
"eslint:recommended",
|
||||||
|
"plugin:@typescript-eslint/recommended"
|
||||||
|
],
|
||||||
|
"overrides": [
|
||||||
|
],
|
||||||
|
"parser": "@typescript-eslint/parser",
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": "latest",
|
||||||
|
"sourceType": "module"
|
||||||
|
},
|
||||||
|
"plugins": [
|
||||||
|
"@typescript-eslint"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"indent": [
|
||||||
|
"error",
|
||||||
|
2
|
||||||
|
],
|
||||||
|
"linebreak-style": [
|
||||||
|
"error",
|
||||||
|
"unix"
|
||||||
|
],
|
||||||
|
"quotes": [
|
||||||
|
"error",
|
||||||
|
"double"
|
||||||
|
],
|
||||||
|
"semi": [
|
||||||
|
"error",
|
||||||
|
"always"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,31 +0,0 @@
|
||||||
{
|
|
||||||
"env": {
|
|
||||||
"node": true,
|
|
||||||
"commonjs": true,
|
|
||||||
"es2021": true
|
|
||||||
},
|
|
||||||
"extends": "eslint:recommended",
|
|
||||||
"overrides": [
|
|
||||||
],
|
|
||||||
"parserOptions": {
|
|
||||||
"ecmaVersion": "latest"
|
|
||||||
},
|
|
||||||
"rules": {
|
|
||||||
"indent": [
|
|
||||||
"error",
|
|
||||||
"tab"
|
|
||||||
],
|
|
||||||
"linebreak-style": [
|
|
||||||
"error",
|
|
||||||
"unix"
|
|
||||||
],
|
|
||||||
"quotes": [
|
|
||||||
"error",
|
|
||||||
"double"
|
|
||||||
],
|
|
||||||
"semi": [
|
|
||||||
"error",
|
|
||||||
"always"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
48
app/app.ts
48
app/app.ts
|
@ -1,4 +1,4 @@
|
||||||
require("dotenv").config();
|
import "dotenv";
|
||||||
|
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import passport from "passport";
|
import passport from "passport";
|
||||||
|
@ -17,20 +17,18 @@ import adduserRouter from "./routes/adduser";
|
||||||
|
|
||||||
import {db, expire, createUser, MediaRow} from "./types/db";
|
import {db, expire, createUser, MediaRow} from "./types/db";
|
||||||
|
|
||||||
let app = express();
|
const app = express();
|
||||||
let server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
let port = normalizePort(process.env.EBPORT || "3000");
|
const port = normalizePort(process.env.EBPORT || "3000");
|
||||||
|
|
||||||
function normalizePort(val: string) {
|
function normalizePort(val: string) {
|
||||||
var port = parseInt(val, 10);
|
const port = parseInt(val, 10);
|
||||||
|
|
||||||
if (isNaN(port)) {
|
if (isNaN(port)) {
|
||||||
// named pipe
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (port >= 0) {
|
if (port >= 0) {
|
||||||
// port number
|
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +45,7 @@ function onError(error: any) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
var bind = typeof port === "string"
|
const bind = typeof port === "string"
|
||||||
? "Pipe " + port
|
? "Pipe " + port
|
||||||
: "Port " + port;
|
: "Port " + port;
|
||||||
|
|
||||||
|
@ -69,30 +67,33 @@ function onError(error: any) {
|
||||||
db.serialize(function() {
|
db.serialize(function() {
|
||||||
// create the database schema for the embedders app
|
// create the database schema for the embedders app
|
||||||
db.run("CREATE TABLE IF NOT EXISTS users ( \
|
db.run("CREATE TABLE IF NOT EXISTS users ( \
|
||||||
id INTEGER PRIMARY KEY, \
|
id INTEGER PRIMARY KEY, \
|
||||||
username TEXT UNIQUE, \
|
username TEXT UNIQUE, \
|
||||||
hashed_password BLOB, \
|
hashed_password BLOB, \
|
||||||
salt BLOB \
|
salt BLOB \
|
||||||
)");
|
)");
|
||||||
|
|
||||||
db.run("CREATE TABLE IF NOT EXISTS media ( \
|
db.run("CREATE TABLE IF NOT EXISTS media ( \
|
||||||
id INTEGER PRIMARY KEY, \
|
id INTEGER PRIMARY KEY, \
|
||||||
path TEXT NOT NULL, \
|
path TEXT NOT NULL, \
|
||||||
expire INTEGER \, \
|
expire INTEGER, \
|
||||||
username TEXT \
|
username TEXT \
|
||||||
)");
|
)");
|
||||||
|
|
||||||
db.run("ALTER TABLE media ADD COLUMN username TEXT", (err) => {
|
db.run("ALTER TABLE media ADD COLUMN username TEXT", (err) => {
|
||||||
if(err)
|
if(err) return;
|
||||||
return;
|
|
||||||
}); //TODO, version new DB, run this command when detecting old DB
|
}); //TODO, version new DB, run this command when detecting old DB
|
||||||
|
|
||||||
|
db.run("ALTER TABLE users ADD COLUMN username TEXT", (err) => {
|
||||||
|
if (err) return;
|
||||||
|
});
|
||||||
|
|
||||||
createUser("admin", process.env.EBPASS || "changeme");
|
createUser("admin", process.env.EBPASS || "changeme");
|
||||||
});
|
});
|
||||||
|
|
||||||
function onListening() {
|
function onListening() {
|
||||||
var addr = server.address();
|
const addr = server.address();
|
||||||
var bind = typeof addr === "string"
|
const bind = typeof addr === "string"
|
||||||
? "pipe " + addr
|
? "pipe " + addr
|
||||||
: "port " + addr.port;
|
: "port " + addr.port;
|
||||||
console.log("Listening on " + bind);
|
console.log("Listening on " + bind);
|
||||||
|
@ -116,11 +117,10 @@ app.use(session({
|
||||||
secret: process.env.EBSECRET || "pleasechangeme",
|
secret: process.env.EBSECRET || "pleasechangeme",
|
||||||
resave: false,
|
resave: false,
|
||||||
saveUninitialized: false,
|
saveUninitialized: false,
|
||||||
// @ts-ignore
|
|
||||||
store: new SQLiteStore({
|
store: new SQLiteStore({
|
||||||
db: "sessions.db",
|
db: "sessions.db",
|
||||||
dir: "./var/db"
|
dir: "./var/db"
|
||||||
})
|
}) as session.Store
|
||||||
}));
|
}));
|
||||||
app.use(passport.authenticate("session"));
|
app.use(passport.authenticate("session"));
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type {RequestHandler as Middleware, Router, Request, Response, NextFunction} from 'express';
|
import type {RequestHandler as Middleware, Router, Request, Response, NextFunction} from "express";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
|
|
||||||
import {db, createUser} from "../types/db";
|
import {createUser} from "../types/db";
|
||||||
|
|
||||||
const router: Router = express.Router();
|
const router: Router = express.Router();
|
||||||
/**Middleware to check if a user is actually signed in */
|
/**Middleware to check if a user is actually signed in */
|
||||||
|
@ -14,16 +14,16 @@ const adminCheck: Middleware = (req: Request, res: Response, next: NextFunction)
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
}
|
};
|
||||||
|
|
||||||
router.get("/adduser", adminCheck, (req: Request, res: Response, next: NextFunction) => {
|
router.get("/adduser", adminCheck, (req: Request, res: Response) => {
|
||||||
res.locals.filter = null;
|
res.locals.filter = null;
|
||||||
res.render("adduser", { user: req.user });
|
res.render("adduser", { user: req.user });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/adduser", adminCheck, (req: Request, res: Response, next: NextFunction) => {
|
router.post("/adduser", adminCheck, (req: Request, res: Response) => {
|
||||||
createUser(req.body.username, req.body.password);
|
createUser(req.body.username, req.body.password);
|
||||||
res.redirect('/');
|
res.redirect("/");
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
|
@ -3,10 +3,10 @@ import express from "express";
|
||||||
import passport from "passport";
|
import passport from "passport";
|
||||||
import {Strategy as LocalStrategy} from "passport-local";
|
import {Strategy as LocalStrategy} from "passport-local";
|
||||||
|
|
||||||
import {User} from "../types/lib"
|
import {User} from "../types/lib";
|
||||||
import {db, UserRow} from "../types/db";
|
import {db, UserRow} from "../types/db";
|
||||||
|
|
||||||
let router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
passport.use(new LocalStrategy(function verify(username, password, cb) {
|
passport.use(new LocalStrategy(function verify(username, password, cb) {
|
||||||
db.get("SELECT * FROM users WHERE username = ?", [username], function(err: Error, row: UserRow) {
|
db.get("SELECT * FROM users WHERE username = ?", [username], function(err: Error, row: UserRow) {
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
import type {RequestHandler as Middleware, Request, Response, NextFunction} from 'express';
|
import type {RequestHandler as Middleware, Request, Response, NextFunction} from "express";
|
||||||
import multer from "multer";
|
import multer from "multer";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import ffmpeg from "fluent-ffmpeg";
|
import ffmpeg from "fluent-ffmpeg";
|
||||||
import imageProbe from "probe-image-size";
|
import imageProbe from "probe-image-size";
|
||||||
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
|
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
|
||||||
// @ts-ignore
|
|
||||||
import ffprobepath from "@ffprobe-installer/ffprobe";
|
import ffprobepath from "@ffprobe-installer/ffprobe";
|
||||||
|
|
||||||
ffmpeg.setFfmpegPath(ffmpegpath.path);
|
ffmpeg.setFfmpegPath(ffmpegpath.path);
|
||||||
|
@ -14,19 +13,19 @@ import fs from "fs";
|
||||||
|
|
||||||
import {extension} from "../types/lib";
|
import {extension} from "../types/lib";
|
||||||
import {db, MediaRow, getPath, deleteId} from "../types/db";
|
import {db, MediaRow, getPath, deleteId} from "../types/db";
|
||||||
import {fileStorage, fileFilter} from "../types/multer";
|
import {fileStorage} 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?
|
const upload = multer({ storage: fileStorage /**, fileFilter: fileFilter**/ }); //maybe make this a env variable?
|
||||||
/**Middleware to grab media from media database */
|
/**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
|
const admin: boolean = req.user.username == "admin" ? true : false;
|
||||||
/**Check if the user is an admin, if so, show all posts from all users */
|
/**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}'`;
|
const 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: []) => {
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
let files = rows.map((row: MediaRow)=> {
|
const files = rows.map((row: MediaRow)=> {
|
||||||
return {
|
return {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
path: row.path,
|
path: row.path,
|
||||||
|
@ -39,13 +38,13 @@ const fetchMedia: Middleware = (req, res, next) => {
|
||||||
res.locals.Count = files.length;
|
res.locals.Count = files.length;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
let router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get("/", (req: Request, res: Response, next: NextFunction) => {
|
router.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||||
if (!req.user)
|
if (!req.user)
|
||||||
return res.render("home")
|
return res.render("home");
|
||||||
next();
|
next();
|
||||||
}, fetchMedia, (req: Request, res: Response) => {
|
}, fetchMedia, (req: Request, res: Response) => {
|
||||||
res.locals.filter = null;
|
res.locals.filter = null;
|
||||||
|
@ -53,46 +52,46 @@ router.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/gifv/:file", async (req: Request, res: Response, next: NextFunction) => {
|
router.get("/gifv/:file", async (req: Request, res: Response, next: NextFunction) => {
|
||||||
let url = `${req.protocol}://${req.get("host")}/uploads/${req.params.file}`;
|
const url = `${req.protocol}://${req.get("host")}/uploads/${req.params.file}`;
|
||||||
let width; let height;
|
let width; let height;
|
||||||
|
|
||||||
let nameAndExtension = extension(`uploads/${req.params.file}`);
|
const nameAndExtension = extension(`uploads/${req.params.file}`);
|
||||||
if (nameAndExtension[1] == ".mp4" || nameAndExtension[1] == ".mov" || nameAndExtension[1] == ".webm" || nameAndExtension[1] == ".gif") {
|
if (nameAndExtension[1] == ".mp4" || nameAndExtension[1] == ".mov" || nameAndExtension[1] == ".webm" || nameAndExtension[1] == ".gif") {
|
||||||
ffmpeg()
|
ffmpeg()
|
||||||
.input(`uploads/${req.params.file}`)
|
.input(`uploads/${req.params.file}`)
|
||||||
.inputFormat(nameAndExtension[1].substring(1))
|
.inputFormat(nameAndExtension[1].substring(1))
|
||||||
.ffprobe((err: Error, data: ffmpeg.FfprobeData) => {
|
.ffprobe((err: Error, data: ffmpeg.FfprobeData) => {
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
width = data.streams[0].width;
|
width = data.streams[0].width;
|
||||||
height = data.streams[0].height;
|
height = data.streams[0].height;
|
||||||
return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: width, height: height });
|
return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: width, height: height });
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let imageData = await imageProbe(fs.createReadStream(`uploads/${req.params.file}`));
|
const imageData = await imageProbe(fs.createReadStream(`uploads/${req.params.file}`));
|
||||||
return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: imageData.width, height: imageData.height });
|
return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: imageData.width, height: imageData.height });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/", [checkAuth, upload.array("fileupload"), createEmbedData, handleUpload], (req: Request, res: Response) => {
|
router.post("/", [checkAuth, upload.array("fileupload"), createEmbedData, handleUpload], (req: Request, res: Response) => {
|
||||||
res.redirect("/")
|
res.redirect("/");
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/sharex", [checkSharexAuth, upload.single("fileupload"), createEmbedData, handleUpload], (req: Request, res: Response) => {
|
router.post("/sharex", [checkSharexAuth, upload.single("fileupload"), createEmbedData, handleUpload], (req: Request, res: Response) => {
|
||||||
return res.send(`${req.protocol}://${req.get("host")}/uploads/${req.file.filename}`);
|
return res.send(`${req.protocol}://${req.get("host")}/uploads/${req.file.filename}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/:id(\\d+)/delete", [checkAuth], async (req: Request, res: Response, next: NextFunction) => {
|
router.post("/:id(\\d+)/delete", [checkAuth], async (req: Request, res: Response) => {
|
||||||
const path: any = await getPath(req.params.id)
|
const path: any = await getPath(req.params.id);
|
||||||
fs.unlink(`uploads/${path.path}`, async (err) => {
|
fs.unlink(`uploads/${path.path}`, async (err) => {
|
||||||
if (err && err.errno == -4058) {
|
if (err && err.errno == -4058) {
|
||||||
await deleteId("media", req.params.id).then(()=> {
|
await deleteId("media", req.params.id).then(()=> {
|
||||||
return res.redirect("/");
|
return res.redirect("/");
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
await deleteId("media", req.params.id).then(()=> {
|
await deleteId("media", req.params.id).then(()=> {
|
||||||
return res.redirect("/");
|
return res.redirect("/");
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
|
@ -1,8 +1,7 @@
|
||||||
import type {RequestHandler as Middleware, Router, Request, Response, NextFunction} from 'express';
|
import type {RequestHandler as Middleware, NextFunction} from "express";
|
||||||
|
|
||||||
import ffmpeg from "fluent-ffmpeg";
|
import ffmpeg from "fluent-ffmpeg";
|
||||||
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
|
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
|
||||||
// @ts-ignore
|
|
||||||
import ffprobepath from "@ffprobe-installer/ffprobe";
|
import ffprobepath from "@ffprobe-installer/ffprobe";
|
||||||
ffmpeg.setFfmpegPath(ffmpegpath.path);
|
ffmpeg.setFfmpegPath(ffmpegpath.path);
|
||||||
ffmpeg.setFfprobePath(ffprobepath.path);
|
ffmpeg.setFfprobePath(ffprobepath.path);
|
||||||
|
@ -18,11 +17,11 @@ export const checkAuth: Middleware = (req, res, next) => {
|
||||||
return res.status(401);
|
return res.status(401);
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
}
|
};
|
||||||
|
|
||||||
/**Checks shareX auth key */
|
/**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";
|
const auth = process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY";
|
||||||
let key = null;
|
let key = null;
|
||||||
|
|
||||||
if (req.headers["key"]) {
|
if (req.headers["key"]) {
|
||||||
|
@ -35,18 +34,18 @@ export const checkSharexAuth: Middleware = (req, res, next) => {
|
||||||
return res.status(401).send("{success: false, message: '\"'Invalid key\", fix: \"Provide a valid key\"}");
|
return res.status(401).send("{success: false, message: '\"'Invalid key\", fix: \"Provide a valid key\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
let shortKey = key.substr(0, 3) + "...";
|
const shortKey = key.substr(0, 3) + "...";
|
||||||
console.log(`Authenicated user with key: ${shortKey}`);
|
console.log(`Authenicated user with key: ${shortKey}`);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
}
|
};
|
||||||
|
|
||||||
/**Creates oembed json file for embed metadata */
|
/**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 (const file in files) {
|
||||||
let nameAndExtension = extension(files[file].originalname);
|
const nameAndExtension = extension(files[file].originalname);
|
||||||
let oembed = {
|
const oembed = {
|
||||||
type: "video",
|
type: "video",
|
||||||
version: "1.0",
|
version: "1.0",
|
||||||
provider_name: "embedder",
|
provider_name: "embedder",
|
||||||
|
@ -63,12 +62,12 @@ export const createEmbedData: Middleware = (req, res, next) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
}
|
};
|
||||||
/** Converts video to gif and vice versa using ffmpeg */
|
/** 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 (const file in files) {
|
||||||
let nameAndExtension = extension(files[file].originalname);
|
const nameAndExtension = extension(files[file].originalname);
|
||||||
if (nameAndExtension[1] == ".mp4" || nameAndExtension[1] == ".webm" || nameAndExtension[1] == ".mkv" || nameAndExtension[1] == ".avi" || nameAndExtension[1] == ".mov") {
|
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("Converting " + nameAndExtension[0] + nameAndExtension[1] + " to gif");
|
||||||
ffmpeg()
|
ffmpeg()
|
||||||
|
@ -101,7 +100,7 @@ export const convert: Middleware = (req, res, next) => {
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
/**Middleware for handling uploaded files. Inserts it into the database */
|
/**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) {
|
||||||
|
@ -110,25 +109,25 @@ export const handleUpload: Middleware = (req, res, next) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const files = (req.files) ? req.files as Express.Multer.File[] : req.file; //Check if a single file was uploaded or multiple
|
const files = (req.files) ? req.files as Express.Multer.File[] : req.file; //Check if a single file was uploaded or multiple
|
||||||
const username = (req.user) ? req.user.username : "sharex" //if no username was provided, we can presume that it is sharex
|
const username = (req.user) ? req.user.username : "sharex"; //if no username was provided, we can presume that it is sharex
|
||||||
const expireDate: Date = (req.body.expire) ? new Date(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) {
|
if (files instanceof Array) {
|
||||||
for (let file in files) {
|
for (const file in files) {
|
||||||
insertToDB(files[file].filename, expireDate, username, next);
|
insertToDB(files[file].filename, expireDate, username, next);
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
insertToDB(files.filename, expireDate, username, next);
|
insertToDB(files.filename, expireDate, username, next);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
}
|
};
|
||||||
/**Inserts into media database */
|
/**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 = [
|
const params: MediaParams = [
|
||||||
filename,
|
filename,
|
||||||
expireDate,
|
expireDate,
|
||||||
username
|
username
|
||||||
]
|
];
|
||||||
|
|
||||||
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", params, function (err) {
|
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", params, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ export const db = new sqlite3.Database("./var/db/media.db");
|
||||||
|
|
||||||
/**Inserts a new user to the database */
|
/**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);
|
const salt = crypto.randomBytes(16);
|
||||||
|
|
||||||
db.run("INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)", [
|
db.run("INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)", [
|
||||||
username,
|
username,
|
||||||
|
@ -21,38 +21,39 @@ export function createUser(username: string, password: string) {
|
||||||
/**Selects a path for a file given ID */
|
/**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 = ?`;
|
const query = "SELECT path FROM media WHERE id = ?";
|
||||||
|
|
||||||
db.get(query, [id], (err: Error, path: object) => {
|
db.get(query, [id], (err: Error, path: object) => {
|
||||||
if (err) {reject(err)}
|
if (err) {reject(err);}
|
||||||
resolve(path)
|
resolve(path);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Deletes from database given an ID */
|
/**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 = ?`
|
const query = `DELETE FROM ${database} WHERE id = ?`;
|
||||||
|
|
||||||
db.run(query, [id], (err: Error) => {
|
db.run(query, [id], (err: Error) => {
|
||||||
if (err) {reject(err); return;}
|
if (err) {reject(err); return;}
|
||||||
resolve(null)
|
resolve(null);
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Expires a database row given a Date in unix time */
|
/**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} < ?`;
|
const query = `SELECT * FROM ${database} WHERE ${column} < ?`;
|
||||||
|
|
||||||
db.each(query, [expiration], async (err: Error, row: GenericRow) => {
|
db.each(query, [expiration], async (err: Error, row: GenericRow) => {
|
||||||
await deleteId(database, row.id)
|
if (err) reject(err);
|
||||||
|
await deleteId(database, row.id);
|
||||||
|
|
||||||
resolve(null);
|
resolve(null);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**A generic database row */
|
/**A generic database row */
|
||||||
|
@ -79,7 +80,7 @@ export type MediaParams = [
|
||||||
|
|
||||||
/**A row for the user database */
|
/**A row for the user database */
|
||||||
export interface UserRow {
|
export interface UserRow {
|
||||||
id? : Number,
|
id? : number,
|
||||||
username: string,
|
username: string,
|
||||||
hashed_password: any,
|
hashed_password: any,
|
||||||
salt: any
|
salt: any
|
||||||
|
|
1
app/types/declarations/ffprobepath.d.ts
vendored
Normal file
1
app/types/declarations/ffprobepath.d.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
declare module "@ffprobe-installer/ffprobe";
|
|
@ -1,3 +1,4 @@
|
||||||
|
/* eslint-disable @typescript-eslint/no-namespace */
|
||||||
declare global {
|
declare global {
|
||||||
namespace Express {
|
namespace Express {
|
||||||
interface User {
|
interface User {
|
||||||
|
@ -8,7 +9,7 @@ declare global {
|
||||||
}
|
}
|
||||||
/**Splits a file name into its name and then its extension */
|
/**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();
|
const 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 */
|
/**Type for user data */
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import {Request} from 'express';
|
import {Request} from "express";
|
||||||
import multer, {FileFilterCallback} from 'multer';
|
import multer, {FileFilterCallback} from "multer";
|
||||||
|
|
||||||
import {db, MediaRow} from './db'
|
import {db, MediaRow} from "./db";
|
||||||
import {extension} from './lib'
|
import {extension} from "./lib";
|
||||||
|
|
||||||
export type DestinationCallback = (error: Error | null, destination: string) => void
|
export type DestinationCallback = (error: Error | null, destination: string) => void
|
||||||
export type FileNameCallback = (error: Error | null, filename: string) => void
|
export type FileNameCallback = (error: Error | null, filename: string) => void
|
||||||
|
@ -20,15 +20,15 @@ export const fileStorage = multer.diskStorage({
|
||||||
file: Express.Multer.File,
|
file: Express.Multer.File,
|
||||||
callback: FileNameCallback
|
callback: FileNameCallback
|
||||||
): void => {
|
): void => {
|
||||||
let nameAndExtension = extension(file.originalname);
|
const nameAndExtension = extension(file.originalname);
|
||||||
console.log(`Uploading ${file}`);
|
console.log(`Uploading ${file}`);
|
||||||
db.all("SELECT * FROM media WHERE path = ?", [nameAndExtension[0] + nameAndExtension[1]], (err: Error, exists: []) => {
|
db.all("SELECT * FROM media WHERE path = ?", [nameAndExtension[0] + nameAndExtension[1]], (err: Error, exists: []) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err)
|
console.log(err);
|
||||||
callback(err, null)
|
callback(err, null);
|
||||||
}
|
}
|
||||||
if (exists.length != 0) {
|
if (exists.length != 0) {
|
||||||
let suffix = new Date().getTime() / 1000;
|
const suffix = new Date().getTime() / 1000;
|
||||||
|
|
||||||
if (request.body.title == "" || request.body.title == null || request.body.title == undefined) {
|
if (request.body.title == "" || request.body.title == null || request.body.title == undefined) {
|
||||||
callback(null, nameAndExtension[0] + "-" + suffix + nameAndExtension[1]);
|
callback(null, nameAndExtension[0] + "-" + suffix + nameAndExtension[1]);
|
||||||
|
@ -46,27 +46,27 @@ export const fileStorage = multer.diskStorage({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export let allowedMimeTypes = [
|
export const allowedMimeTypes = [
|
||||||
"image/png",
|
"image/png",
|
||||||
"image/jpg",
|
"image/jpg",
|
||||||
"image/jpeg",
|
"image/jpeg",
|
||||||
"image/gif",
|
"image/gif",
|
||||||
"image/webp",
|
"image/webp",
|
||||||
"video/mp4",
|
"video/mp4",
|
||||||
"video/mov",
|
"video/mov",
|
||||||
"video/webm",
|
"video/webm",
|
||||||
"audio/mpeg",
|
"audio/mpeg",
|
||||||
"audio/ogg"
|
"audio/ogg"
|
||||||
];
|
];
|
||||||
|
|
||||||
export const fileFilter = (
|
export const fileFilter = (
|
||||||
request: Request,
|
request: Request,
|
||||||
file: Express.Multer.File,
|
file: Express.Multer.File,
|
||||||
callback: FileFilterCallback
|
callback: FileFilterCallback
|
||||||
): void => {
|
): void => {
|
||||||
if (allowedMimeTypes.includes(file.mimetype)) {
|
if (allowedMimeTypes.includes(file.mimetype)) {
|
||||||
callback(null, true)
|
callback(null, true);
|
||||||
} else {
|
} else {
|
||||||
callback(null, false)
|
callback(null, false);
|
||||||
}
|
}
|
||||||
}
|
};
|
1327
package-lock.json
generated
1327
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -35,7 +35,7 @@
|
||||||
"cookie-parser": "~1.4.4",
|
"cookie-parser": "~1.4.4",
|
||||||
"dotenv": "^8.6.0",
|
"dotenv": "^8.6.0",
|
||||||
"ejs": "^3.1.8",
|
"ejs": "^3.1.8",
|
||||||
"express": "~4.16.1",
|
"express": "^4.18.2",
|
||||||
"express-session": "^1.17.3",
|
"express-session": "^1.17.3",
|
||||||
"fluent-ffmpeg": "^2.1.2",
|
"fluent-ffmpeg": "^2.1.2",
|
||||||
"http-errors": "~1.6.3",
|
"http-errors": "~1.6.3",
|
||||||
|
@ -59,7 +59,8 @@
|
||||||
"@types/passport": "^1.0.11",
|
"@types/passport": "^1.0.11",
|
||||||
"@types/passport-local": "^1.0.34",
|
"@types/passport-local": "^1.0.34",
|
||||||
"@types/probe-image-size": "^7.2.0",
|
"@types/probe-image-size": "^7.2.0",
|
||||||
"@typescript-eslint/parser": "^5.45.0",
|
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
||||||
|
"@typescript-eslint/parser": "^5.46.1",
|
||||||
"copyfiles": "^2.4.1",
|
"copyfiles": "^2.4.1",
|
||||||
"cypress": "^11.1.0",
|
"cypress": "^11.1.0",
|
||||||
"cypress-file-upload": "^5.0.8",
|
"cypress-file-upload": "^5.0.8",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue