finally fix race conditions for file progress

uses sse to deliver updates
This commit is contained in:
WaveringAna 2025-01-19 22:24:23 -05:00
parent 19613e1bb3
commit 6be6d3b15f
9 changed files with 492 additions and 483 deletions

View file

@ -18,7 +18,7 @@ import indexRouter from "./routes/index";
import adduserRouter from "./routes/adduser"; import adduserRouter from "./routes/adduser";
import settingsRouter from "./routes/settings"; import settingsRouter from "./routes/settings";
import {db, expire, createDatabase, updateDatabase, MediaRow} from "./lib/db"; import { db, expire, createDatabase, updateDatabase, MediaRow } from "./lib/db";
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
@ -74,7 +74,7 @@ db.get("SELECT * FROM sqlite_master WHERE name ='users' and type='table'", async
else checkVersion(); else checkVersion();
}); });
function checkVersion () { function checkVersion() {
db.get("PRAGMA user_version", (err: Error, row: any) => { db.get("PRAGMA user_version", (err: Error, row: any) => {
if (row && row.user_version) { if (row && row.user_version) {
const version = row.user_version; const version = row.user_version;
@ -128,7 +128,7 @@ app.use("/", settingsRouter);
app.use("/uploads", express.static("uploads")); app.use("/uploads", express.static("uploads"));
async function prune () { async function prune() {
db.all("SELECT * FROM media", (err: Error, rows: []) => { db.all("SELECT * FROM media", (err: Error, rows: []) => {
console.log("Uploaded files: " + rows.length); console.log("Uploaded files: " + rows.length);
console.log(rows); console.log(rows);

View file

@ -6,6 +6,7 @@ import process from "process";
import { extension, videoExtensions, imageExtensions, oembedObj } from "./lib"; import { extension, videoExtensions, imageExtensions, oembedObj } from "./lib";
import { insertToDB } from "./db"; import { insertToDB } from "./db";
import { ffmpegDownscale, ffProbe } from "./ffmpeg"; import { ffmpegDownscale, ffProbe } from "./ffmpeg";
import { MediaProcessor } from "../services/MediaProcesser";
export const checkAuth: Middleware = (req, res, next) => { export const checkAuth: Middleware = (req, res, next) => {
if (!req.user) { if (!req.user) {
@ -166,3 +167,25 @@ export const handleUpload: Middleware = async (req, res, next) => {
res.status(500).send("Error processing files."); 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);
}
};

View file

@ -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 };

View file

@ -27,13 +27,11 @@
height: 100px; height: 100px;
position: relative; position: relative;
margin: 50px auto; margin: 50px auto;
color: #555; color: #555;
text-align: center; text-align: center;
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
font-size: 14px; font-size: 14px;
padding-top: 80px; padding-top: 80px;
background-color: rgba(255, 255, 255, 0.8); background-color: rgba(255, 255, 255, 0.8);
border-radius: 10px; border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
@ -80,6 +78,7 @@
color: rgb(247, 248, 248); color: rgb(247, 248, 248);
appearance: none; appearance: none;
transition: border 0.15s ease 0s; transition: border 0.15s ease 0s;
:focus { :focus {
outline: none; outline: none;
box-shadow: none; box-shadow: none;

View file

@ -2,294 +2,255 @@
/* eslint-disable no-undef */ /* eslint-disable no-undef */
/* eslint-env browser: true */ /* eslint-env browser: true */
let newMediaList; const MEDIA_TYPES = {
video: ['.mp4', '.mov', '.avi', '.flv', '.mkv', '.wmv', '.webm'],
image: ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.tiff', '.webp']
};
const videoExtensions = [ const getFileExtension = filename => {
".mp4", const parts = filename.split('.');
".mov", return parts.length > 1 ? `.${parts.pop().toLowerCase()}` : '';
".avi", };
".flv",
".mkv",
".wmv",
".webm",
];
const imageExtensions = [ const getMediaType = filename => {
".jpg", const ext = getFileExtension(filename);
".jpeg", if (MEDIA_TYPES.video.includes(ext)) return 'video';
".png", if (MEDIA_TYPES.image.includes(ext)) return 'image';
".gif", return 'other';
".bmp", };
".svg",
".tiff",
".webp",
];
class FileUploader {
function copyURI(evt) { constructor() {
evt.preventDefault(); this.dropArea = document.getElementById('dropArea');
navigator.clipboard this.gallery = document.getElementById('gallery');
.writeText(absolutePath(evt.target.getAttribute("src"))) this.setupEventListeners();
.then( this.setupProgressUpdates();
() => {
console.log("copied");
},
() => {
console.log("failed");
} }
);
}
function copyA(evt) { setupEventListeners() {
evt.preventDefault(); // Drag and drop handlers
navigator.clipboard ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
.writeText(absolutePath(evt.target.getAttribute("href"))) this.dropArea.addEventListener(eventName, e => {
.then(
() => {
console.log("copied");
},
() => {
console.log("failed");
}
);
}
function copyPath(evt) {
navigator.clipboard.writeText(absolutePath(evt)).then(
() => {
console.log("copied");
},
() => {
console.log("failed");
}
);
}
function absolutePath(href) {
let link = document.createElement("a");
link.href = href;
return link.href;
}
function extension(string) {
return string.slice(((string.lastIndexOf(".") - 2) >>> 0) + 2);
}
let dropArea = document.getElementById("dropArea");
["dragenter", "dragover", "dragleave", "drop"].forEach((eventName) => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
}
["dragenter", "dragover"].forEach((eventName) => {
dropArea.addEventListener(eventName, highlight, false);
});
["dragleave", "drop"].forEach((eventName) => {
dropArea.addEventListener(eventName, unhighlight, false);
});
function highlight(e) {
dropArea.classList.add("highlight");
}
function unhighlight(e) {
dropArea.classList.remove("highlight");
}
dropArea.addEventListener("drop", handleDrop, false);
window.addEventListener("paste", handlePaste);
function handleDrop(e) {
let dt = e.dataTransfer;
let files = dt.files;
handleFiles(files);
}
function handlePaste(e) {
// Get the data of clipboard
const clipboardItems = e.clipboardData.items;
const items = [].slice.call(clipboardItems).filter(function (item) {
// Filter the image items only
return item.type.indexOf("image") !== -1;
}); });
if (items.length === 0) { });
['dragenter', 'dragover'].forEach(eventName => {
this.dropArea.addEventListener(eventName, () =>
this.dropArea.classList.add('highlight'));
});
['dragleave', 'drop'].forEach(eventName => {
this.dropArea.addEventListener(eventName, () =>
this.dropArea.classList.remove('highlight'));
});
// Handle file drops
this.dropArea.addEventListener('drop', e =>
this.handleFiles(e.dataTransfer.files));
// Handle paste events
window.addEventListener('paste', e => this.handlePaste(e));
// Handle manual file selection
document.getElementById('fileupload')
.addEventListener('change', e => this.handleFiles(e.target.files));
// Handle manual upload button
document.getElementById('submit')
.addEventListener('click', () => this.uploadSelectedFiles());
}
setupProgressUpdates() {
console.log("Setting up SSE connection...");
const evtSource = new EventSource('/progress-updates');
evtSource.onopen = () => {
console.log("SSE connection established");
};
evtSource.onmessage = event => {
console.log("Raw SSE data:", event.data);
const data = JSON.parse(event.data);
if (data.type === 'connected') {
console.log("Initial connection established");
return; return;
} }
const item = items[0]; const { filename, progress, status } = data;
// Get the blob of image const sanitizedFilename = sanitizeId(filename);
const blob = item.getAsFile();
console.log(blob);
uploadFile(blob); console.log("Looking for elements:", {
previewFile(blob); spinnerSelector: `spinner-${sanitizedFilename}`,
} containerSelector: `media-container-${sanitizedFilename}`,
});
function handleFiles(files) { const spinnerElement = document.getElementById(`spinner-${sanitizedFilename}`);
files = [...files]; const containerElement = document.getElementById(`media-container-${sanitizedFilename}`);
files.forEach(uploadFile);
files.forEach(previewFile);
}
function previewFile(file) { if (!spinnerElement || !containerElement) {
let reader = new FileReader(); console.warn("Could not find required elements for:", filename);
reader.readAsDataURL(file); return;
reader.onloadend = function () { }
let img = document.createElement("img");
img.src = reader.result; if (status === 'complete') {
img.className = "image"; console.log("Processing complete, showing video for:", filename);
document.getElementById("gallery").appendChild(img); spinnerElement.style.display = 'none';
document.getElementById("fileupload").src = img.src; containerElement.style.display = 'block';
} else if (status === 'processing') {
console.log("Updating progress for:", filename);
spinnerElement.textContent =
`Optimizing Video for Sharing: ${(progress * 100).toFixed(2)}% done`;
}
}; };
}
function uploadFile(file) { evtSource.onerror = (err) => {
let xhr = new XMLHttpRequest(); console.error("SSE Error:", err);
let formData = new FormData(); };
}
xhr.open("POST", "/", true); async handleFiles(files) {
const filesArray = [...files];
xhr.addEventListener("readystatechange", function () { for (const file of filesArray) {
if (xhr.readyState == 4) { await this.uploadFile(file);
if (xhr.status == 200) {
//document.getElementById("embedder-list").innerHTML = response;
htmx.ajax("GET", "/media-list", {target: "#embedder-list", swap: "innerHTML"});
document.getElementById("gallery").innerHTML = "";
htmx.process(document.body);
} else {
alert(`Upload failed, error code: ${xhr.status}`);
} }
} }
handlePaste(e) {
const items = [...e.clipboardData.items]
.filter(item => item.type.indexOf('image') !== -1);
if (items.length) {
const file = items[0].getAsFile();
this.uploadFile(file);
}
}
async uploadFile(file) {
const formData = new FormData();
formData.append('fileupload', file);
formData.append('expire', document.getElementById('expire').value);
try {
const response = await fetch('/', {
method: 'POST',
body: formData
}); });
if (file == null || file == undefined) { if (!response.ok) throw new Error(`Upload failed: ${response.status}`);
file = document.querySelector("#fileupload").files[0];
// Get the new file list HTML and insert it
const listResponse = await fetch('/media-list');
const html = await listResponse.text();
document.getElementById('embedder-list').innerHTML = html;
// Clear preview
this.gallery.innerHTML = '';
} catch (error) {
console.error('Upload error:', error);
alert('Upload failed: ' + error.message);
}
} }
formData.append("fileupload", file); uploadSelectedFiles() {
formData.append("expire", document.getElementById("expire").value); const fileInput = document.getElementById('fileupload');
xhr.send(formData); if (fileInput.files.length) {
this.handleFiles(fileInput.files);
}
}
showMediaElement(filename) {
const container = document.getElementById(`media-container-${filename}`);
const spinner = document.getElementById(`spinner-${filename}`);
if (container && spinner) {
const mediaType = getMediaType(filename);
if (mediaType === 'video') {
container.innerHTML = `
<video class="image" autoplay loop muted playsinline>
<source src="/uploads/720p-${filename}">
</video>`;
}
spinner.style.display = 'none';
container.style.display = 'block';
}
}
} }
function openFullSize(imageUrl) { // Initialize on page load
let modal = document.createElement("div"); document.addEventListener('DOMContentLoaded', () => {
modal.classList.add("modal"); new FileUploader();
let img = document.createElement("img");
let video = document.createElement("video");
img.src = imageUrl;
video.src = imageUrl;
video.controls = true;
if ( // Setup search functionality
imageExtensions.includes(extension(imageUrl)) const searchInput = document.getElementById('search');
) { if (searchInput) {
modal.appendChild(img); searchInput.addEventListener('input', e => {
} else if ( const searchValue = e.target.value.toLowerCase();
videoExtensions.includes(extension(imageUrl)) const mediaItems = document.querySelectorAll('ul.embedder-list li');
) {
modal.appendChild(video); mediaItems.forEach(item => {
const matches = item.id.toLowerCase().includes(searchValue);
item.classList.toggle('hide', !matches);
item.classList.toggle('show', matches);
item.addEventListener('animationend', function handler() {
if (!matches && searchValue !== '') {
this.style.display = 'none';
} }
this.removeEventListener('animationend', handler);
// Add the modal to the page
document.body.appendChild(modal);
// Add an event listener to close the modal when the user clicks on it
modal.addEventListener("click", function () {
modal.remove();
}); });
}
let searchInput = document.getElementById("search");
searchInput.addEventListener("input", () => {
let searchValue = searchInput.value;
let mediaList = document.querySelectorAll("ul.embedder-list li");
mediaList.forEach((li) => {
if (!li.id.toLowerCase().includes(searchValue)) {
//make lowercase to allow case insensitivity
li.classList.add("hide");
li.classList.remove("show");
li.addEventListener(
"animationend",
function () {
if (searchInput.value !== "") {
this.style.display = "none";
}
},
{ once: true }
); // The {once: true} option automatically removes the event listener after it has been called
} else {
li.style.display = "";
li.classList.remove("hide");
if (searchValue === "" && !li.classList.contains("show")) {
li.classList.add("show");
}
}
}); });
});
}
}); });
function p(num) { // Utility functions for the media list
return `${(num * 100).toFixed(2)}%`; window.copyURI = e => {
} e.preventDefault();
navigator.clipboard.writeText(absolutePath(e.target.getAttribute('src')))
.then(() => console.log('Copied to clipboard'))
.catch(err => console.error('Copy failed:', err));
};
function checkFileAvailability(filePath) { window.copyA = e => {
const checkFile = () => { e.preventDefault();
console.log(`Checking if ${filePath} is processed...`); navigator.clipboard.writeText(absolutePath(e.target.getAttribute('href')))
fetch(`/uploads/${filePath}-progress.json`) .then(() => console.log('Copied to clipboard'))
.then((response) => { .catch(err => console.error('Copy failed:', err));
if (response.ok) { };
console.log(`${filePath} still processing`);
return response.json().then(json => {
document.getElementById(`spinner-${filePath}`).innerText = `Optimizing Video for Sharing: ${p(json.progress)} done`;
return response;
});
} else if (response.status === 404) {
console.log(`${filePath} finished processing`);
console.log(`/uploads/720p-${filePath}-progress.json finished`);
clearInterval(interval);
createVideoElement(filePath);
} else {
throw new Error(`HTTP error: Status code ${response.status}`);
}
})
.catch((error) => console.error("Error:", error));
};
checkFile(); window.copyPath = evt => {
const interval = setInterval(checkFile, 1000); navigator.clipboard.writeText(absolutePath(evt))
} .then(() => console.log('Copied to clipboard'))
.catch(err => console.error('Copy failed:', err));
};
function createVideoElement(filePath) { window.absolutePath = href => {
const videoContainer = document.getElementById(`video-${filePath}`); const link = document.createElement('a');
videoContainer.outerHTML = ` link.href = href;
<video id='video-${filePath}' class="image" autoplay loop muted playsinline> return link.href;
<source src="/uploads/720p-${filePath}"> };
</video>
`;
videoContainer.style.display = "block";
document.getElementById(`spinner-${filePath}`).style.display = "none";
}
function updateMediaList() { window.openFullSize = imageUrl => {
htmx.ajax("GET", "/media-list", {target: "#embedder-list", swap: "innerHTML"}); const modal = document.createElement('div');
htmx.process(document.body); modal.className = 'modal';
}
function refreshMediaList(files) { const mediaType = getMediaType(imageUrl);
files.forEach(file => { const element = mediaType === 'video'
console.log(`Checking ${file.path}...`); ? `<video src="${imageUrl}" controls></video>`
if (videoExtensions.includes(extension(file.path))) { : `<img src="${imageUrl}">`;
const progressFileName = `uploads/${file.path}-progress.json`;
console.log(`Fetching ${progressFileName}...`); modal.innerHTML = element;
checkFileAvailability(file.path); document.body.appendChild(modal);
} else {
console.log(`File ${file.path} is not a video, displaying...`); modal.addEventListener('click', () => modal.remove());
} };
});
function sanitizeId(filename) {
return filename.replace(/[^a-z0-9]/gi, '_');
} }

View file

@ -17,12 +17,13 @@ import path from "path";
import { extension, videoExtensions, oembedObj } from "../lib/lib"; import { extension, videoExtensions, oembedObj } from "../lib/lib";
import { db, MediaRow, getPath, deleteId } from "../lib/db"; import { db, MediaRow, getPath, deleteId } from "../lib/db";
import { fileStorage } from "../lib/multer"; import { fileStorage } from "../lib/multer";
import { progressManager } from "../services/ProgressManager";
import { import {
checkAuth, checkAuth,
checkSharexAuth, checkSharexAuth,
convertTo720p,
createEmbedData, createEmbedData,
handleUpload, handleUpload,
processUploadedMedia,
} from "../lib/middleware"; } from "../lib/middleware";
const 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?
@ -30,7 +31,6 @@ const upload = multer({ storage: fileStorage /**, fileFilter: fileFilter**/ });
const fetchMedia: Middleware = (req, res, next) => { const fetchMedia: Middleware = (req, res, next) => {
const 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 */
const query: string = admin const query: string = admin
? "SELECT * FROM media" ? "SELECT * FROM media"
: "SELECT * FROM media WHERE username = ?"; : "SELECT * FROM media WHERE username = ?";
@ -43,15 +43,20 @@ const fetchMedia: Middleware = (req, res, next) => {
return res.status(500).send("Database error"); return res.status(500).send("Database error");
} }
const files = rows.map((row: MediaRow) => { const files = rows.map((row: MediaRow) => {
const isProcessed = videoExtensions.includes(extension(row.path)[1]) ?
fs.existsSync(`uploads/720p-${row.path}`) :
true;
return { return {
id: row.id, id: row.id,
path: row.path, path: row.path,
expire: row.expire, expire: row.expire,
username: row.username, username: row.username,
url: "/" + row.id, url: "/" + row.id,
isProcessed
}; };
}); });
res.locals.files = files.reverse(); //reverse so newest files appear first res.locals.files = files.reverse();
res.locals.Count = files.length; res.locals.Count = files.length;
next(); next();
}); });
@ -59,6 +64,29 @@ const fetchMedia: Middleware = (req, res, next) => {
const router = express.Router(); const router = express.Router();
router.get('/progress-updates', (req, res) => {
console.log("SSE connection requested"); // Debug log
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
// Send an initial message to confirm connection
res.write(`data: ${JSON.stringify({ type: 'connected' })}\n\n`);
const sendUpdate = (data: any) => {
res.write(`data: ${JSON.stringify(data)}\n\n`);
};
progressManager.subscribeToUpdates(sendUpdate);
// Clean up on client disconnect
req.on('close', () => {
console.log("SSE connection closed"); // Debug log
progressManager.unsubscribeFromUpdates(sendUpdate);
});
});
router.get( router.get(
"/", "/",
(req: Request, res: Response, next: NextFunction) => { (req: Request, res: Response, next: NextFunction) => {
@ -79,8 +107,7 @@ router.get("/media-list", fetchMedia, (req: Request, res: Response) => {
router.get( router.get(
"/gifv/:file", "/gifv/:file",
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const url = `${req.protocol}://${req.get("host")}/uploads/${ const url = `${req.protocol}://${req.get("host")}/uploads/${req.params.file
req.params.file
}`; }`;
let width; let width;
let height; let height;
@ -167,10 +194,10 @@ router.post(
[ [
checkAuth, checkAuth,
upload.array("fileupload"), upload.array("fileupload"),
convertTo720p,
createEmbedData,
handleUpload, handleUpload,
fetchMedia, fetchMedia,
processUploadedMedia,
createEmbedData,
], ],
(req: Request, res: Response) => { (req: Request, res: Response) => {
return res.render("partials/_fileList", { user: req.user }); // Render only the file list partial return res.render("partials/_fileList", { user: req.user }); // Render only the file list partial
@ -192,9 +219,9 @@ router.get(
[checkAuth], [checkAuth],
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const filename: any = await getPath(req.params.id); const filename: any = await getPath(req.params.id);
const filePath = path.join(__dirname , "../../uploads/" + filename.path); const filePath = path.join(__dirname, "../../uploads/" + filename.path);
const oembed = path.join( const oembed = path.join(
__dirname , "../../uploads/oembed-" + filename.path + ".json" __dirname, "../../uploads/oembed-" + filename.path + ".json"
); );
const [fileName, fileExtension] = extension(filePath); const [fileName, fileExtension] = extension(filePath);
@ -205,7 +232,7 @@ router.get(
fileExtension == ".gif" fileExtension == ".gif"
) { ) {
filesToDelete.push( filesToDelete.push(
path.join(__dirname , "../../uploads/720p-" + filename.path) path.join(__dirname, "../../uploads/720p-" + filename.path)
); );
} }

View file

@ -0,0 +1,56 @@
import ffmpeg from 'fluent-ffmpeg';
import { progressManager } from './ProgressManager';
import { EncodingType, currentEncoding } from '../lib/ffmpeg';
export class MediaProcessor {
static async processVideo(
inputPath: string,
filename: string,
extension: string
): Promise<void> {
console.log("Starting video processing:", filename); // Debug log
const outputPath = `uploads/720p-${filename}${extension}`;
const outputOptions = [
'-vf', 'scale=-2:720',
'-c:v', currentEncoding,
'-c:a', 'copy',
'-pix_fmt', 'yuv420p'
];
return new Promise((resolve, reject) => {
ffmpeg()
.input(inputPath)
.outputOptions(outputOptions)
.output(outputPath)
.on('progress', (progress) => {
console.log("Progress:", progress.percent); // Debug log
progressManager.updateProgress({
filename: `${filename}${extension}`,
progress: progress.percent / 100,
status: 'processing'
});
})
.on('end', () => {
console.log("Processing complete:", filename); // Debug log
progressManager.updateProgress({
filename: `${filename}${extension}`,
progress: 1,
status: 'complete'
});
resolve();
})
.on('error', (err) => {
console.error("Processing error:", err); // Debug log
progressManager.updateProgress({
filename: `${filename}${extension}`,
progress: 0,
status: 'error',
message: err.message
});
reject(err);
})
.run();
});
}
}

View file

@ -0,0 +1,45 @@
import { EventEmitter } from 'events';
export interface ProgressUpdate {
filename: string;
progress: number;
status: 'processing' | 'complete' | 'error';
message?: string;
}
class ProgressManager {
private static instance: ProgressManager;
private emitter: EventEmitter;
private activeJobs: Map<string, ProgressUpdate>;
private constructor() {
this.emitter = new EventEmitter();
this.activeJobs = new Map();
}
static getInstance(): ProgressManager {
if (!ProgressManager.instance) {
ProgressManager.instance = new ProgressManager();
}
return ProgressManager.instance;
}
updateProgress(update: ProgressUpdate) {
this.activeJobs.set(update.filename, update);
this.emitter.emit('progress', update);
}
subscribeToUpdates(callback: (update: ProgressUpdate) => void) {
this.emitter.on('progress', callback);
}
unsubscribeFromUpdates(callback: (update: ProgressUpdate) => void) {
this.emitter.off('progress', callback);
}
getJobStatus(filename: string): ProgressUpdate | undefined {
return this.activeJobs.get(filename);
}
}
export const progressManager = ProgressManager.getInstance();

View file

@ -1,73 +1,86 @@
<% <%
function extension(string) { const getMediaType = (filename) => {
return string.slice((string.lastIndexOf(".") - 2 >>> 0) + 2); const ext = filename.slice(((filename.lastIndexOf(".") - 2) >>> 0) + 2).toLowerCase();
} const videoExts = ['.mp4', '.mov', '.avi', '.flv', '.mkv', '.wmv', '.webm'];
const imageExts = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.tiff', '.webp'];
const videoExtensions = ['.mp4', '.mov', '.avi', '.flv', '.mkv', '.wmv', '.webm']; if (videoExts.includes(ext)) return 'video';
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.tiff', '.webp']; if (imageExts.includes(ext)) return 'image';
return 'other';
};
function sanitizeId(filename) {
return filename.replace(/[^a-z0-9]/gi, '_');
}
%> %>
<script>
newMediaList = JSON.parse('<%- JSON.stringify(files) %>');
console.log("executed")
refreshMediaList(newMediaList);
</script>
<!-- _fileList.ejs -->
<% files.forEach(function(file) { %> <% files.forEach(function(file) { %>
<li id="<%= file.path %>" class="show"> <li id="<%= file.path %>" class="show">
<div class="view"> <div class="view">
<% if (videoExtensions.includes(extension(file.path))) { %> <% const mediaType = getMediaType(file.path); %>
<!-- Show spinner initially -->
<div id="spinner-<%= file.path %>" class="spinner">Optimizing Video for Sharing...</div>
<!-- Hidden video container to be displayed later --> <% if (mediaType === 'video') { %>
<div class="video"> <div class="video">
<video id="video-<%= file.path %>" class="image" autoplay loop muted playsinline style="display: none;"> <% const sanitizedId = file.path.replace(/[^a-z0-9]/gi, '_'); %>
<div id="spinner-<%= sanitizedId %>" class="spinner" style="display: <%= file.isProcessed ? 'none' : 'block' %>;">
<div class="spinner-content">
Optimizing Video for Sharing...
</div>
</div>
<div id="media-container-<%= sanitizedId %>" class="video-container" style="display: <%= file.isProcessed ? 'block' : 'none' %>;">
<video class="image" autoplay loop muted playsinline>
<source src="/uploads/720p-<%= file.path %>"> <source src="/uploads/720p-<%= file.path %>">
</video> </video>
</div>
<div class="overlay"> <div class="overlay">
<% if(user.username == "admin" && file.username != "admin") { %> <% if(user.username === "admin" && file.username !== "admin") { %>
<small class="username"><%= file.username %></small> <small class="username"><%= file.username %></small>
<br> <br>
<% } %> <% } %>
<a href="/gifv/<%=file.path %>" onclick="copyA(event)">Copy as GIFv</a> <a href="/gifv/<%= file.path %>" onclick="copyA(event)">Copy as GIFv</a>
</div> </div>
</div> </div>
<% } else if (extension(file.path) == ".gif") { %> <% } else if (mediaType === 'image') { %>
<!-- Image container -->
<div class="video"> <div class="video">
<img class="image" src="/uploads/720p-<%=file.path %>" width="100%" onclick="copyURI(event);" loading="lazy"> <img class="image"
<div class="overlay"> src="/uploads/<%= file.path %>"
<% if(user.username == "admin" && file.username != "admin") { %> onclick="copyURI(event)"
<small class="username"><%= file.username %></small> loading="lazy">
<br>
<% } %> <% if(user.username === "admin" && file.username !== "admin") { %>
<a href="/gifv/<%=file.path %>" onclick="copyA(event)">Copy as GIFv</a>
</div>
</div>
<% } else if (imageExtensions.includes(extension(file.path))) { %>
<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"> <div class="overlay">
<small class="username"><%= file.username %></small> <small class="username"><%= file.username %></small>
</div> </div>
<% } %> <% } %>
</div> </div>
<% } else {%>
<!-- non-media file --> <% } else { %>
<div class="nonmedia" onclick="copyPath('/uploads/<%=file.path%>')"> <!-- Non-media file -->
<p><%=extension(file.path)%> file</p> <div class="nonmedia" onclick="copyPath('/uploads/<%= file.path %>')">
<% if(user.username == "admin" && file.username != "admin") { %> <p><%= file.path.split('.').pop().toUpperCase() %> file</p>
<% if(user.username === "admin" && file.username !== "admin") { %>
<div class="overlay"> <div class="overlay">
<small class="username"><%= file.username %></small> <small class="username"><%= file.username %></small>
</div> </div>
<% } %> <% } %>
</div> </div>
<% } %> <% } %>
<label><%= file.path %></label> <label><%= file.path %></label>
<button class="destroy" hx-get="<%=file.url%>/delete" hx-trigger="click" hx-target="#embedder-list" hx-swap="innerHTML"></button> <button class="destroy"
<button type="button" class="fullsize" onclick="openFullSize('/uploads/<%=file.path%>')"></button> hx-get="<%= file.url %>/delete"
hx-trigger="click"
hx-target="#embedder-list"
hx-swap="innerHTML">
</button>
<button type="button"
class="fullsize"
onclick="openFullSize('/uploads/<%= file.path %>')">
</button>
</div> </div>
</li> </li>
<% }); %> <% }); %>