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 indexRouter from "./routes/index";
|
||||||
import adduserRouter from "./routes/adduser";
|
import adduserRouter from "./routes/adduser";
|
||||||
|
|
||||||
import {db, createUser, MediaRow} from "./db";
|
import {db, createUser, MediaRow} from "./types/db";
|
||||||
|
|
||||||
let app = express();
|
let app = express();
|
||||||
let server = http.createServer(app);
|
let server = http.createServer(app);
|
||||||
|
|
|
@ -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 "../db";
|
import {db, createUser} from "../types/db";
|
||||||
|
|
||||||
const router: Router = express.Router();
|
const router: Router = express.Router();
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ const adminCheck: Middleware = (req: Request, res: Response, next: NextFunction)
|
||||||
if (!req.user)
|
if (!req.user)
|
||||||
return res.status(403).send("You are not authorized to perform this action");
|
return res.status(403).send("You are not authorized to perform this action");
|
||||||
else {
|
else {
|
||||||
//@ts-ignore
|
|
||||||
if (req.user.username != "admin")
|
if (req.user.username != "admin")
|
||||||
return res.status(403).send("You are not authorized to perform this action");
|
return res.status(403).send("You are not authorized to perform this action");
|
||||||
next();
|
next();
|
||||||
|
|
|
@ -3,7 +3,8 @@ 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 {db, UserRow} from "../db";
|
import {User} from "../types/lib"
|
||||||
|
import {db, UserRow} from "../types/db";
|
||||||
|
|
||||||
let router = express.Router();
|
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() {
|
process.nextTick(function() {
|
||||||
cb(null, {
|
cb(null, {
|
||||||
id: user.id,
|
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() {
|
process.nextTick(function() {
|
||||||
return cb(null, user);
|
return cb(null, user);
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,17 +12,15 @@ ffmpeg.setFfprobePath(ffprobepath.path);
|
||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
import {extension} from "../lib";
|
import {extension} from "../types/lib";
|
||||||
import {db, MediaRow} from "../db";
|
import {db, MediaRow} from "../types/db";
|
||||||
import {fileStorage, fileFilter} from "../multer";
|
import {fileStorage, fileFilter} 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?
|
let upload = multer({ storage: fileStorage /**, fileFilter: fileFilter**/ }); //maybe make this a env variable?
|
||||||
|
|
||||||
const fetchMedia: Middleware = (req, res, next) => {
|
const fetchMedia: Middleware = (req, res, next) => {
|
||||||
//@ts-ignore
|
|
||||||
let admin: boolean = req.user.username == "admin" ? true : false
|
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}'`;
|
let 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: []) => {
|
||||||
|
@ -53,7 +51,7 @@ router.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||||
res.render("index", { user: req.user });
|
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 url = `${req.protocol}://${req.get("host")}/uploads/${req.params.file}`;
|
||||||
let width; let height;
|
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 ffmpeg from "fluent-ffmpeg";
|
||||||
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
|
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
|
||||||
|
@ -10,8 +10,8 @@ ffmpeg.setFfprobePath(ffprobepath.path);
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import process from "process";
|
import process from "process";
|
||||||
|
|
||||||
import {db} from "../db";
|
import {extension} from "../types/lib";
|
||||||
import {extension} from "../lib";
|
import {db, MediaParams} from "../types/db";
|
||||||
|
|
||||||
export const checkAuth: Middleware = (req, res, next) => {
|
export const checkAuth: Middleware = (req, res, next) => {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
|
@ -70,7 +70,6 @@ export const convert: Middleware = (req, res, next) => {
|
||||||
let nameAndExtension = extension(files[file].originalname);
|
let 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");
|
||||||
console.log(nameAndExtension[0] + nameAndExtension[1]);
|
|
||||||
ffmpeg()
|
ffmpeg()
|
||||||
.input(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
.input(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
||||||
.inputFormat(nameAndExtension[1].substring(1))
|
.inputFormat(nameAndExtension[1].substring(1))
|
||||||
|
@ -104,33 +103,48 @@ export const convert: Middleware = (req, res, next) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handleUpload: 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");
|
console.log("No files were uploaded");
|
||||||
return res.status(400).send("No files were uploaded.");
|
return res.status(400).send("No files were uploaded.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const files = req.files as Express.Multer.File[]
|
//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"
|
||||||
|
|
||||||
for (let file in files) {
|
let expireDate: Date;
|
||||||
let currentdate = Date.now();
|
if (req.body.expire) {
|
||||||
let expireDate: Date;
|
expireDate = new Date(Date.now() + (req.body.expire * 24 * 60 * 60 * 1000));
|
||||||
if (req.body.expire) {
|
} else
|
||||||
expireDate = new Date(currentdate + (req.body.expire * 24 * 60 * 60 * 1000));
|
expireDate = null;
|
||||||
} else
|
|
||||||
expireDate = null;
|
if (files instanceof Array) {
|
||||||
//@ts-ignore
|
for (let file in files) {
|
||||||
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", [files[file].filename, expireDate, req.user.username], function (err) {
|
insertToDB(files[file].filename, expireDate, username, next);
|
||||||
if (err) {
|
}
|
||||||
console.log(err);
|
} else
|
||||||
return next(err);
|
insertToDB(files.filename, expireDate, username, next);
|
||||||
}
|
|
||||||
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}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
export interface MediaRow {
|
||||||
id? : Number,
|
id? : Number,
|
||||||
path: String,
|
path: String,
|
||||||
expire: Number,
|
expire: Date,
|
||||||
username: String
|
username?: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type MediaParams = [
|
||||||
|
path: String,
|
||||||
|
expire: Date,
|
||||||
|
username?: String
|
||||||
|
]
|
||||||
|
|
||||||
export interface UserRow {
|
export interface UserRow {
|
||||||
id? : Number,
|
id? : Number,
|
||||||
username: String,
|
username: String,
|
|
@ -1,4 +1,18 @@
|
||||||
|
declare global {
|
||||||
|
namespace Express {
|
||||||
|
interface User {
|
||||||
|
username: string;
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function extension(str: String){
|
export function extension(str: String){
|
||||||
let file = str.split("/").pop();
|
let 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()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface User {
|
||||||
|
username: string;
|
||||||
|
id?: string;
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ export const fileStorage = multer.diskStorage({
|
||||||
file: Express.Multer.File,
|
file: Express.Multer.File,
|
||||||
callback: DestinationCallback
|
callback: DestinationCallback
|
||||||
): void => {
|
): void => {
|
||||||
callback(null, __dirname + "/../uploads");
|
callback(null, __dirname + "/../../uploads");
|
||||||
},
|
},
|
||||||
filename: (
|
filename: (
|
||||||
request: Request,
|
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" ) { %>
|
<% } 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">
|
<div class="video">
|
||||||
<img class="image" src="/uploads/<%=file.path %>" width="100%" onclick="copyURI(event)" loading="lazy">
|
<img class="image" src="/uploads/<%=file.path %>" width="100%" onclick="copyURI(event)" loading="lazy">
|
||||||
|
<% if(user.username == "admin" && file.username != "admin") { %>
|
||||||
<div class="overlay">
|
<div class="overlay">
|
||||||
<% if(user.username == "admin" && file.username != "admin") { %>
|
|
||||||
<small class="username"><%= file.username %></small>
|
<small class="username"><%= file.username %></small>
|
||||||
<% } %>
|
|
||||||
</div>
|
</div>
|
||||||
|
<% } %>
|
||||||
</div>
|
</div>
|
||||||
<% } else {%> <!-- non-media file -->
|
<% } else {%> <!-- non-media file -->
|
||||||
<div class="nonmedia" onclick="copyPath('/uploads/<%=file.path%>')">
|
<div class="nonmedia" onclick="copyPath('/uploads/<%=file.path%>')">
|
||||||
<p><%=extension(file.path)%> file</p>
|
<p><%=extension(file.path)%> file</p>
|
||||||
|
<% if(user.username == "admin" && file.username != "admin") { %>
|
||||||
<div class="overlay">
|
<div class="overlay">
|
||||||
<% if(user.username == "admin" && file.username != "admin") { %>
|
|
||||||
<small class="username"><%= file.username %></small>
|
<small class="username"><%= file.username %></small>
|
||||||
<% } %>
|
|
||||||
</div>
|
</div>
|
||||||
|
<% } %>
|
||||||
</div>
|
</div>
|
||||||
<% } %>
|
<% } %>
|
||||||
<label><%= file.path %></label>
|
<label><%= file.path %></label>
|
||||||
|
|
|
@ -2,8 +2,7 @@ const { defineConfig } = require("cypress");
|
||||||
|
|
||||||
module.exports = defineConfig({
|
module.exports = defineConfig({
|
||||||
e2e: {
|
e2e: {
|
||||||
baseUrl: "http://localhost:4000",
|
baseUrl: "http://localhost:3000",
|
||||||
},
|
},
|
||||||
chromeWebSecurity: false,
|
chromeWebSecurity: false
|
||||||
"video": false
|
|
||||||
});
|
});
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 104 KiB |
Binary file not shown.
Before Width: | Height: | Size: 95 KiB |
Loading…
Add table
Add a link
Reference in a new issue