my nvim and vscode are finally using the same settings
This commit is contained in:
parent
58077a5d63
commit
a5e03facbe
11 changed files with 757 additions and 757 deletions
|
@ -6,24 +6,24 @@ import {createUser} from "../lib/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");
|
||||
else {
|
||||
if (req.user.username != "admin")
|
||||
return res.status(403).send("You are not authorized to perform this action");
|
||||
}
|
||||
if (!req.user)
|
||||
return res.status(403).send("You are not authorized to perform this action");
|
||||
else {
|
||||
if (req.user.username != "admin")
|
||||
return res.status(403).send("You are not authorized to perform this action");
|
||||
}
|
||||
|
||||
next();
|
||||
next();
|
||||
};
|
||||
|
||||
router.get("/adduser", adminCheck, (req: Request, res: Response) => {
|
||||
res.locals.filter = null;
|
||||
res.render("adduser", { user: req.user });
|
||||
res.locals.filter = null;
|
||||
res.render("adduser", { user: req.user });
|
||||
});
|
||||
|
||||
router.post("/adduser", adminCheck, (req: Request, res: Response) => {
|
||||
createUser(req.body.username, req.body.password);
|
||||
res.redirect("/");
|
||||
createUser(req.body.username, req.body.password);
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
export default router;
|
|
@ -9,77 +9,77 @@ import { db, UserRow } from "../lib/db";
|
|||
const router = express.Router();
|
||||
|
||||
passport.use(
|
||||
new LocalStrategy(function verify(username, password, cb) {
|
||||
db.get(
|
||||
"SELECT * FROM users WHERE username = ?",
|
||||
[username],
|
||||
function (err: Error, row: UserRow) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!row) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password.",
|
||||
});
|
||||
}
|
||||
new LocalStrategy(function verify(username, password, cb) {
|
||||
db.get(
|
||||
"SELECT * FROM users WHERE username = ?",
|
||||
[username],
|
||||
function (err: Error, row: UserRow) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!row) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password.",
|
||||
});
|
||||
}
|
||||
|
||||
crypto.pbkdf2(
|
||||
password,
|
||||
row.salt,
|
||||
310000,
|
||||
32,
|
||||
"sha256",
|
||||
function (err, hashedPassword) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password.",
|
||||
});
|
||||
}
|
||||
return cb(null, row);
|
||||
},
|
||||
crypto.pbkdf2(
|
||||
password,
|
||||
row.salt,
|
||||
310000,
|
||||
32,
|
||||
"sha256",
|
||||
function (err, hashedPassword) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
||||
return cb(null, false, {
|
||||
message: "Incorrect username or password.",
|
||||
});
|
||||
}
|
||||
return cb(null, row);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
passport.serializeUser(function (user: User, cb) {
|
||||
process.nextTick(function () {
|
||||
cb(null, {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
process.nextTick(function () {
|
||||
cb(null, {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
passport.deserializeUser(function (user: User, cb) {
|
||||
process.nextTick(function () {
|
||||
return cb(null, user);
|
||||
});
|
||||
process.nextTick(function () {
|
||||
return cb(null, user);
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/login", function (req, res) {
|
||||
res.render("login");
|
||||
res.render("login");
|
||||
});
|
||||
|
||||
router.post(
|
||||
"/login/password",
|
||||
passport.authenticate("local", {
|
||||
successRedirect: "/",
|
||||
failureRedirect: "/login",
|
||||
}),
|
||||
"/login/password",
|
||||
passport.authenticate("local", {
|
||||
successRedirect: "/",
|
||||
failureRedirect: "/login",
|
||||
}),
|
||||
);
|
||||
|
||||
router.post("/logout", function (req, res, next) {
|
||||
req.logout(function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.redirect("/");
|
||||
});
|
||||
req.logout(function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.redirect("/");
|
||||
});
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import type {
|
||||
RequestHandler as Middleware,
|
||||
Request,
|
||||
Response,
|
||||
NextFunction,
|
||||
RequestHandler as Middleware,
|
||||
Request,
|
||||
Response,
|
||||
NextFunction,
|
||||
} from "express";
|
||||
|
||||
import multer from "multer";
|
||||
|
@ -18,184 +18,184 @@ import { extension, videoExtensions } from "../lib/lib";
|
|||
import { db, MediaRow, getPath, deleteId } from "../lib/db";
|
||||
import { fileStorage } from "../lib/multer";
|
||||
import {
|
||||
checkAuth,
|
||||
checkSharexAuth,
|
||||
convertTo720p,
|
||||
createEmbedData,
|
||||
handleUpload,
|
||||
checkAuth,
|
||||
checkSharexAuth,
|
||||
convertTo720p,
|
||||
createEmbedData,
|
||||
handleUpload,
|
||||
} from "../lib/middleware";
|
||||
|
||||
const 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 admin: boolean = req.user.username == "admin" ? true : false;
|
||||
/**Check if the user is an admin, if so, show all posts from all users */
|
||||
const query: string =
|
||||
const admin: boolean = req.user.username == "admin" ? true : false;
|
||||
/**Check if the user is an admin, if so, show all posts from all users */
|
||||
const query: string =
|
||||
admin == true
|
||||
? "SELECT * FROM media"
|
||||
: `SELECT * FROM media WHERE username = '${req.user.username}'`;
|
||||
? "SELECT * FROM media"
|
||||
: `SELECT * FROM media WHERE username = '${req.user.username}'`;
|
||||
|
||||
db.all(query, (err: Error, rows: []) => {
|
||||
if (err) return next(err);
|
||||
const files = rows.map((row: MediaRow) => {
|
||||
return {
|
||||
id: row.id,
|
||||
path: row.path,
|
||||
expire: row.expire,
|
||||
username: row.username,
|
||||
url: "/" + row.id,
|
||||
};
|
||||
db.all(query, (err: Error, rows: []) => {
|
||||
if (err) return next(err);
|
||||
const files = rows.map((row: MediaRow) => {
|
||||
return {
|
||||
id: row.id,
|
||||
path: row.path,
|
||||
expire: row.expire,
|
||||
username: row.username,
|
||||
url: "/" + row.id,
|
||||
};
|
||||
});
|
||||
res.locals.files = files.reverse(); //reverse so newest files appear first
|
||||
res.locals.Count = files.length;
|
||||
next();
|
||||
});
|
||||
res.locals.files = files.reverse(); //reverse so newest files appear first
|
||||
res.locals.Count = files.length;
|
||||
next();
|
||||
});
|
||||
};
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get(
|
||||
"/",
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
if (!req.user) return res.render("home");
|
||||
next();
|
||||
},
|
||||
fetchMedia,
|
||||
(req: Request, res: Response) => {
|
||||
res.locals.filter = null;
|
||||
res.render("index", { user: req.user });
|
||||
}
|
||||
"/",
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
if (!req.user) return res.render("home");
|
||||
next();
|
||||
},
|
||||
fetchMedia,
|
||||
(req: Request, res: Response) => {
|
||||
res.locals.filter = null;
|
||||
res.render("index", { user: req.user });
|
||||
}
|
||||
);
|
||||
|
||||
router.get("/media-list", fetchMedia, (req: Request, res: Response) => {
|
||||
res.render("partials/_fileList", { user: req.user });
|
||||
res.render("partials/_fileList", { user: req.user });
|
||||
});
|
||||
|
||||
router.get(
|
||||
"/gifv/:file",
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const url = `${req.protocol}://${req.get("host")}/uploads/${
|
||||
req.params.file
|
||||
}`;
|
||||
let width;
|
||||
let height;
|
||||
"/gifv/:file",
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const url = `${req.protocol}://${req.get("host")}/uploads/${
|
||||
req.params.file
|
||||
}`;
|
||||
let width;
|
||||
let height;
|
||||
|
||||
const [filename, fileExtension] = extension(`uploads/${req.params.file}`);
|
||||
if (
|
||||
fileExtension == ".mp4" ||
|
||||
const [filename, fileExtension] = extension(`uploads/${req.params.file}`);
|
||||
if (
|
||||
fileExtension == ".mp4" ||
|
||||
fileExtension == ".mov" ||
|
||||
fileExtension == ".webm" ||
|
||||
fileExtension == ".gif"
|
||||
) {
|
||||
const imageData = ffProbe(
|
||||
`uploads/${req.params.file}`,
|
||||
filename,
|
||||
fileExtension
|
||||
);
|
||||
) {
|
||||
const imageData = ffProbe(
|
||||
`uploads/${req.params.file}`,
|
||||
filename,
|
||||
fileExtension
|
||||
);
|
||||
|
||||
width = (await imageData).streams[0].width;
|
||||
height = (await imageData).streams[0].height;
|
||||
width = (await imageData).streams[0].width;
|
||||
height = (await imageData).streams[0].height;
|
||||
|
||||
return res.render("gifv", {
|
||||
url: url,
|
||||
host: `${req.protocol}://${req.get("host")}`,
|
||||
width: width,
|
||||
height: height,
|
||||
});
|
||||
} else {
|
||||
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: width,
|
||||
height: height,
|
||||
});
|
||||
} else {
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/",
|
||||
[
|
||||
checkAuth,
|
||||
upload.array("fileupload"),
|
||||
convertTo720p,
|
||||
createEmbedData,
|
||||
handleUpload,
|
||||
fetchMedia,
|
||||
],
|
||||
(req: Request, res: Response) => {
|
||||
return res.render("partials/_fileList", { user: req.user }); // Render only the file list partial
|
||||
}
|
||||
"/",
|
||||
[
|
||||
checkAuth,
|
||||
upload.array("fileupload"),
|
||||
convertTo720p,
|
||||
createEmbedData,
|
||||
handleUpload,
|
||||
fetchMedia,
|
||||
],
|
||||
(req: Request, res: Response) => {
|
||||
return res.render("partials/_fileList", { user: req.user }); // Render only the file list partial
|
||||
}
|
||||
);
|
||||
|
||||
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}`
|
||||
);
|
||||
}
|
||||
"/sharex",
|
||||
[checkSharexAuth, upload.single("fileupload"), createEmbedData, handleUpload],
|
||||
(req: Request, res: Response) => {
|
||||
return res.send(
|
||||
`${req.protocol}://${req.get("host")}/uploads/${req.file.filename}`
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/:id(\\d+)/delete",
|
||||
[checkAuth],
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const filename: any = await getPath(req.params.id);
|
||||
const filePath = path.join(__dirname , "../../uploads/" + filename.path);
|
||||
const oembed = path.join(
|
||||
__dirname , "../../uploads/oembed-" + filename.path + ".json"
|
||||
);
|
||||
"/:id(\\d+)/delete",
|
||||
[checkAuth],
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const filename: any = await getPath(req.params.id);
|
||||
const filePath = path.join(__dirname , "../../uploads/" + filename.path);
|
||||
const oembed = path.join(
|
||||
__dirname , "../../uploads/oembed-" + filename.path + ".json"
|
||||
);
|
||||
|
||||
const [fileName, fileExtension] = extension(filePath);
|
||||
const filesToDelete = [filePath, oembed];
|
||||
const [fileName, fileExtension] = extension(filePath);
|
||||
const filesToDelete = [filePath, oembed];
|
||||
|
||||
if (
|
||||
videoExtensions.includes(fileExtension) ||
|
||||
if (
|
||||
videoExtensions.includes(fileExtension) ||
|
||||
fileExtension == ".gif"
|
||||
) {
|
||||
filesToDelete.push(
|
||||
path.join(__dirname , "../../uploads/720p-" + filename.path)
|
||||
);
|
||||
}
|
||||
) {
|
||||
filesToDelete.push(
|
||||
path.join(__dirname , "../../uploads/720p-" + filename.path)
|
||||
);
|
||||
}
|
||||
|
||||
// Wait for all file deletions and database operations to complete
|
||||
await Promise.all(
|
||||
filesToDelete.map(async (path) => {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
fs.unlink(path, async (err) => {
|
||||
console.log(`Deleting ${path}`);
|
||||
if (err) {
|
||||
if ([-4058, -2].includes(err.errno)) {
|
||||
//file not found
|
||||
console.log("File not found, deleting from database");
|
||||
await deleteId("media", req.params.id);
|
||||
}
|
||||
console.error(`Error deleting file ${path}:`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
await deleteId("media", req.params.id);
|
||||
resolve();
|
||||
});
|
||||
// Wait for all file deletions and database operations to complete
|
||||
await Promise.all(
|
||||
filesToDelete.map(async (path) => {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
fs.unlink(path, async (err) => {
|
||||
console.log(`Deleting ${path}`);
|
||||
if (err) {
|
||||
if ([-4058, -2].includes(err.errno)) {
|
||||
//file not found
|
||||
console.log("File not found, deleting from database");
|
||||
await deleteId("media", req.params.id);
|
||||
}
|
||||
console.error(`Error deleting file ${path}:`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
await deleteId("media", req.params.id);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
})
|
||||
).catch((err) => {
|
||||
console.error("Error deleting files:", err);
|
||||
return next(err);
|
||||
});
|
||||
})
|
||||
).catch((err) => {
|
||||
console.error("Error deleting files:", err);
|
||||
return next(err);
|
||||
});
|
||||
|
||||
next();
|
||||
},
|
||||
[fetchMedia],
|
||||
(req: Request, res: Response) => {
|
||||
return res.render("partials/_fileList", { user: req.user });
|
||||
}
|
||||
next();
|
||||
},
|
||||
[fetchMedia],
|
||||
(req: Request, res: Response) => {
|
||||
return res.render("partials/_fileList", { user: req.user });
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -20,7 +20,7 @@ const fetchUsers = (): Promise<[UserRow]> => {
|
|||
resolve(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const fetchSettings: Middleware = async (req, res, next) => {
|
||||
res.locals.users = req.user.username == "admin" ? await fetchUsers() : null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue