fix sharex
This commit is contained in:
parent
5290f4e960
commit
52b1aeea98
12 changed files with 80 additions and 49 deletions
|
@ -15,7 +15,7 @@ import authRouter from "./routes/auth";
|
|||
import indexRouter from "./routes/index";
|
||||
import adduserRouter from "./routes/adduser";
|
||||
|
||||
import {db, createUser, MediaRow} from "./db";
|
||||
import {db, createUser, MediaRow} from "./types/db";
|
||||
|
||||
let app = express();
|
||||
let server = http.createServer(app);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import type {RequestHandler as Middleware, Router, Request, Response, NextFunction} from 'express';
|
||||
import express from "express";
|
||||
|
||||
import {db, createUser} from "../db";
|
||||
import {db, createUser} from "../types/db";
|
||||
|
||||
const router: Router = express.Router();
|
||||
|
||||
|
@ -9,7 +9,6 @@ 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 {
|
||||
//@ts-ignore
|
||||
if (req.user.username != "admin")
|
||||
return res.status(403).send("You are not authorized to perform this action");
|
||||
next();
|
||||
|
|
|
@ -3,7 +3,8 @@ import express from "express";
|
|||
import passport from "passport";
|
||||
import {Strategy as LocalStrategy} from "passport-local";
|
||||
|
||||
import {db, UserRow} from "../db";
|
||||
import {User} from "../types/lib"
|
||||
import {db, UserRow} from "../types/db";
|
||||
|
||||
let router = express.Router();
|
||||
|
||||
|
@ -32,7 +33,7 @@ passport.use(new LocalStrategy(function verify(username, password, cb) {
|
|||
});
|
||||
}));
|
||||
|
||||
passport.serializeUser(function(user:any, cb) {
|
||||
passport.serializeUser(function(user:User, cb) {
|
||||
process.nextTick(function() {
|
||||
cb(null, {
|
||||
id: user.id,
|
||||
|
@ -41,7 +42,7 @@ passport.serializeUser(function(user:any, cb) {
|
|||
});
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(user, cb) {
|
||||
passport.deserializeUser(function(user:User, cb) {
|
||||
process.nextTick(function() {
|
||||
return cb(null, user);
|
||||
});
|
||||
|
|
|
@ -12,17 +12,15 @@ ffmpeg.setFfprobePath(ffprobepath.path);
|
|||
|
||||
import fs from "fs";
|
||||
|
||||
import {extension} from "../lib";
|
||||
import {db, MediaRow} from "../db";
|
||||
import {fileStorage, fileFilter} from "../multer";
|
||||
import {extension} from "../types/lib";
|
||||
import {db, MediaRow} from "../types/db";
|
||||
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?
|
||||
|
||||
const fetchMedia: Middleware = (req, res, next) => {
|
||||
//@ts-ignore
|
||||
let admin: boolean = req.user.username == "admin" ? true : false
|
||||
//@ts-ignore
|
||||
let query: string = admin == true ? "SELECT * FROM media" : `SELECT * FROM media WHERE username = '${req.user.username}'`;
|
||||
|
||||
db.all(query, (err:Error, rows: []) => {
|
||||
|
@ -53,7 +51,7 @@ router.get("/", (req: Request, res: Response, next: NextFunction) => {
|
|||
res.render("index", { user: req.user });
|
||||
});
|
||||
|
||||
router.get("/gifv/:file", async (req, res, next) => {
|
||||
router.get("/gifv/:file", async (req: Request, res: Response, next: NextFunction) => {
|
||||
let url = `${req.protocol}://${req.get("host")}/uploads/${req.params.file}`;
|
||||
let width; let height;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import type {RequestHandler as Middleware, Router, Request, Response} from 'express';
|
||||
import type {RequestHandler as Middleware, Router, Request, Response, NextFunction} from 'express';
|
||||
|
||||
import ffmpeg from "fluent-ffmpeg";
|
||||
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
|
||||
|
@ -10,8 +10,8 @@ ffmpeg.setFfprobePath(ffprobepath.path);
|
|||
import fs from "fs";
|
||||
import process from "process";
|
||||
|
||||
import {db} from "../db";
|
||||
import {extension} from "../lib";
|
||||
import {extension} from "../types/lib";
|
||||
import {db, MediaParams} from "../types/db";
|
||||
|
||||
export const checkAuth: Middleware = (req, res, next) => {
|
||||
if (!req.user) {
|
||||
|
@ -70,7 +70,6 @@ export const convert: Middleware = (req, res, next) => {
|
|||
let nameAndExtension = extension(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(nameAndExtension[1].substring(1))
|
||||
|
@ -104,33 +103,48 @@ export const convert: Middleware = (req, res, next) => {
|
|||
}
|
||||
|
||||
export const handleUpload: Middleware = (req, res, next) => {
|
||||
if (!req.files || Object.keys(req.files).length === 0) {
|
||||
if (!req.file && !req.files) {
|
||||
console.log("No files were uploaded");
|
||||
return res.status(400).send("No files were uploaded.");
|
||||
}
|
||||
|
||||
const files = req.files as Express.Multer.File[]
|
||||
|
||||
for (let file in files) {
|
||||
let currentdate = Date.now();
|
||||
let expireDate: Date;
|
||||
if (req.body.expire) {
|
||||
expireDate = new Date(currentdate + (req.body.expire * 24 * 60 * 60 * 1000));
|
||||
} else
|
||||
expireDate = null;
|
||||
//@ts-ignore
|
||||
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", [files[file].filename, expireDate, req.user.username], function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return next(err);
|
||||
}
|
||||
console.log(`Uploaded ${files[file].filename} to database`);
|
||||
if (expireDate == null)
|
||||
console.log("It will not expire");
|
||||
else if (expireDate != null || expireDate != undefined)
|
||||
console.log(`It will expire on ${expireDate}`);
|
||||
});
|
||||
}
|
||||
//Check if a single file was uploaded or multiple
|
||||
const files = (req.files) ? req.files as Express.Multer.File[] : req.file;
|
||||
//if no username was provided, we can presume that it is sharex
|
||||
const username = (req.user) ? req.user.username : "sharex"
|
||||
|
||||
let expireDate: Date;
|
||||
if (req.body.expire) {
|
||||
expireDate = new Date(Date.now() + (req.body.expire * 24 * 60 * 60 * 1000));
|
||||
} else
|
||||
expireDate = null;
|
||||
|
||||
if (files instanceof Array) {
|
||||
for (let file in files) {
|
||||
insertToDB(files[file].filename, expireDate, username, next);
|
||||
}
|
||||
} else
|
||||
insertToDB(files.filename, expireDate, username, next);
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
function insertToDB (filename: String, expireDate: Date, username: String, next: NextFunction) {
|
||||
let params: MediaParams = [
|
||||
filename,
|
||||
expireDate,
|
||||
username
|
||||
]
|
||||
|
||||
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", params, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return next(err);
|
||||
}
|
||||
console.log(`Uploaded ${filename} to database`);
|
||||
if (expireDate == null)
|
||||
console.log("It will not expire");
|
||||
else if (expireDate != null || expireDate != undefined)
|
||||
console.log(`It will expire on ${expireDate}`);
|
||||
});
|
||||
}
|
|
@ -19,10 +19,16 @@ export function createUser(username: string, password: string) {
|
|||
export interface MediaRow {
|
||||
id? : Number,
|
||||
path: String,
|
||||
expire: Number,
|
||||
username: String
|
||||
expire: Date,
|
||||
username?: String
|
||||
}
|
||||
|
||||
export type MediaParams = [
|
||||
path: String,
|
||||
expire: Date,
|
||||
username?: String
|
||||
]
|
||||
|
||||
export interface UserRow {
|
||||
id? : Number,
|
||||
username: String,
|
|
@ -1,4 +1,18 @@
|
|||
declare global {
|
||||
namespace Express {
|
||||
interface User {
|
||||
username: string;
|
||||
id?: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function extension(str: String){
|
||||
let file = str.split("/").pop();
|
||||
return [file.substr(0,file.lastIndexOf(".")),file.substr(file.lastIndexOf("."),file.length).toLowerCase()];
|
||||
}
|
||||
|
||||
export interface User {
|
||||
username: string;
|
||||
id?: string;
|
||||
}
|
|
@ -13,7 +13,7 @@ export const fileStorage = multer.diskStorage({
|
|||
file: Express.Multer.File,
|
||||
callback: DestinationCallback
|
||||
): void => {
|
||||
callback(null, __dirname + "/../uploads");
|
||||
callback(null, __dirname + "/../../uploads");
|
||||
},
|
||||
filename: (
|
||||
request: Request,
|
|
@ -95,20 +95,20 @@
|
|||
<% } else if (extension(file.path) == ".jpg" || extension(file.path) == ".jpeg" || extension(file.path) == ".png" || extension(file.path) == ".gif" || extension(file.path) == ".webp" ) { %>
|
||||
<div class="video">
|
||||
<img class="image" src="/uploads/<%=file.path %>" width="100%" onclick="copyURI(event)" loading="lazy">
|
||||
<% if(user.username == "admin" && file.username != "admin") { %>
|
||||
<div class="overlay">
|
||||
<% if(user.username == "admin" && file.username != "admin") { %>
|
||||
<small class="username"><%= file.username %></small>
|
||||
<% } %>
|
||||
</div>
|
||||
<% } %>
|
||||
</div>
|
||||
<% } else {%> <!-- non-media file -->
|
||||
<div class="nonmedia" onclick="copyPath('/uploads/<%=file.path%>')">
|
||||
<p><%=extension(file.path)%> file</p>
|
||||
<% if(user.username == "admin" && file.username != "admin") { %>
|
||||
<div class="overlay">
|
||||
<% if(user.username == "admin" && file.username != "admin") { %>
|
||||
<small class="username"><%= file.username %></small>
|
||||
<% } %>
|
||||
</div>
|
||||
<% } %>
|
||||
</div>
|
||||
<% } %>
|
||||
<label><%= file.path %></label>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue