finally fix race conditions for file progress
uses sse to deliver updates
This commit is contained in:
parent
19613e1bb3
commit
6be6d3b15f
9 changed files with 492 additions and 483 deletions
|
@ -6,6 +6,7 @@ import process from "process";
|
|||
import { extension, videoExtensions, imageExtensions, oembedObj } from "./lib";
|
||||
import { insertToDB } from "./db";
|
||||
import { ffmpegDownscale, ffProbe } from "./ffmpeg";
|
||||
import { MediaProcessor } from "../services/MediaProcesser";
|
||||
|
||||
export const checkAuth: Middleware = (req, res, next) => {
|
||||
if (!req.user) {
|
||||
|
@ -17,7 +18,7 @@ export const checkAuth: Middleware = (req, res, next) => {
|
|||
/**Checks shareX auth key */
|
||||
export const checkSharexAuth: Middleware = (req, res, next) => {
|
||||
const auth =
|
||||
process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY";
|
||||
process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY";
|
||||
let key = null;
|
||||
|
||||
if (req.headers["key"]) {
|
||||
|
@ -57,8 +58,8 @@ export const createEmbedData: Middleware = async (req, res, next) => {
|
|||
for (const file in files) {
|
||||
const [filename, fileExtension] = extension(files[file].filename);
|
||||
const isMedia =
|
||||
videoExtensions.includes(fileExtension) ||
|
||||
imageExtensions.includes(fileExtension);
|
||||
videoExtensions.includes(fileExtension) ||
|
||||
imageExtensions.includes(fileExtension);
|
||||
|
||||
const oembed: oembedObj = {
|
||||
type: "video",
|
||||
|
@ -166,3 +167,25 @@ export const handleUpload: Middleware = async (req, res, next) => {
|
|||
res.status(500).send("Error processing files.");
|
||||
}
|
||||
};
|
||||
|
||||
export const processUploadedMedia: Middleware = async (req, res, next) => {
|
||||
try {
|
||||
const files = req.files as Express.Multer.File[];
|
||||
|
||||
for (const file of files) {
|
||||
const [filename, fileExtension] = extension(file.filename);
|
||||
|
||||
if (videoExtensions.includes(fileExtension)) {
|
||||
MediaProcessor.processVideo(
|
||||
file.path,
|
||||
filename,
|
||||
fileExtension
|
||||
).catch(err => console.error("Error processing video:", err));
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
115
app/lib/ws.ts
115
app/lib/ws.ts
|
@ -1,115 +0,0 @@
|
|||
import { EventEmitter } from "events";
|
||||
|
||||
import WebSocket from "ws";
|
||||
|
||||
const eventEmitter = new EventEmitter();
|
||||
|
||||
const wsPort = normalizePort(process.env.EBWSPORT || "3001");
|
||||
|
||||
const clients: WebSocket[] = [];
|
||||
|
||||
/**
|
||||
* Normalizes a port number to ensure it is a valid integer.
|
||||
*
|
||||
* @param {string} val - The port number as a string.
|
||||
* @returns {number} The normalized port number.
|
||||
*/
|
||||
function normalizePort(val: string) {
|
||||
const port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
return parseInt(val);
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
return port;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* The WebSocket server instance.
|
||||
*/
|
||||
const wss = new WebSocket.Server({port: wsPort});
|
||||
|
||||
wss.on("connection", (ws) => {
|
||||
clients.push(ws);
|
||||
|
||||
ws.on("message", handleMessage);
|
||||
|
||||
ws.on("close", handleMessage);
|
||||
|
||||
ws.on("error", handleMessage);
|
||||
|
||||
ws.on("close", () => {
|
||||
const index = clients.indexOf(ws);
|
||||
if (index !== -1) {
|
||||
clients.splice(index, 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Handles incoming messages from clients.
|
||||
*
|
||||
* @param {string} message - The incoming message.
|
||||
*/
|
||||
function handleMessage(message: string) {
|
||||
try {
|
||||
const data = JSON.parse(message);
|
||||
|
||||
switch (data.type) {
|
||||
case "message":
|
||||
eventEmitter.emit("message", data.message);
|
||||
break;
|
||||
case "close":
|
||||
eventEmitter.emit("close", data.userId);
|
||||
break;
|
||||
case "error":
|
||||
eventEmitter.emit("error", data.error);
|
||||
break;
|
||||
default:
|
||||
console.log(`Unknown message type: ${data.type}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`Error parsing message: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcasts a message to all connected clients.
|
||||
*
|
||||
* @param {string} message - The message to broadcast.
|
||||
*/
|
||||
function broadcast(message: string) {
|
||||
wss.clients.forEach((client) => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all connected clients.
|
||||
*
|
||||
* @returns {WebSocket[]} An array of connected clients.
|
||||
*/
|
||||
function getClients() {
|
||||
return clients;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a specific client.
|
||||
*
|
||||
* @param {string} clientId - The ID of the client to send the message to.
|
||||
* @param {string} message - The message to send.
|
||||
*/
|
||||
/*function sendMessageToClient(clientId: string, message: string) {
|
||||
const client = clients.find((client) => client.id === clientId);
|
||||
if (client) {
|
||||
client.send(message);
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
//export { wss, eventEmitter, broadcast, getClients, sendMessageToClient };
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue