move ffmpeg code to its own file, rename types folder to lib
This commit is contained in:
parent
f322b45ee1
commit
6d779e7811
13 changed files with 310 additions and 253 deletions
165
app/lib/db.ts
Normal file
165
app/lib/db.ts
Normal file
|
@ -0,0 +1,165 @@
|
|||
import sqlite3 from "sqlite3";
|
||||
import mkdirp from "mkdirp";
|
||||
import crypto from "crypto";
|
||||
|
||||
mkdirp.sync("./uploads");
|
||||
mkdirp.sync("./var/db");
|
||||
|
||||
export const db = new sqlite3.Database("./var/db/media.db");
|
||||
|
||||
/**Create the database schema for the embedders app*/
|
||||
export function createDatabase(version: number){
|
||||
console.log("Creating database");
|
||||
|
||||
db.run("CREATE TABLE IF NOT EXISTS users ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
username TEXT UNIQUE, \
|
||||
hashed_password BLOB, \
|
||||
expire INTEGER, \
|
||||
salt BLOB \
|
||||
)", () => createUser("admin", process.env.EBPASS || "changeme"));
|
||||
|
||||
db.run("CREATE TABLE IF NOT EXISTS media ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
path TEXT NOT NULL, \
|
||||
expire INTEGER, \
|
||||
username TEXT \
|
||||
)");
|
||||
|
||||
db.run(`PRAGMA user_version = ${version}`);
|
||||
}
|
||||
|
||||
/**Updates old Database schema to new */
|
||||
export function updateDatabase(oldVersion: number, newVersion: number){
|
||||
if (oldVersion == 1) {
|
||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||
db.run("PRAGMA user_version = 2", (err) => {
|
||||
if(err) return;
|
||||
});
|
||||
db.run("ALTER TABLE media ADD COLUMN username TEXT", (err) => {
|
||||
if(err) return;
|
||||
});
|
||||
|
||||
db.run("ALTER TABLE users ADD COLUMN expire TEXT", (err) => {
|
||||
if(err) return;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**Inserts into the media table */
|
||||
export function insertToDB (filename: string, expireDate: Date, username: string) {
|
||||
const params: MediaParams = [
|
||||
filename,
|
||||
expireDate,
|
||||
username
|
||||
];
|
||||
|
||||
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", params, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return 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}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**Searches the database and returns images with partial or exact keysearches */
|
||||
export function searchImages(imagename: string, partial: boolean) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`searching for ${imagename}`);
|
||||
});
|
||||
}
|
||||
|
||||
export function updateImageName(oldimagename: string, newname:string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`updating ${oldimagename} to ${newname}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**Inserts a new user to the database */
|
||||
export function createUser(username: string, password: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`Creating user ${username}`);
|
||||
const salt = crypto.randomBytes(16);
|
||||
|
||||
db.run("INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)", [
|
||||
username,
|
||||
crypto.pbkdf2Sync(password, salt, 310000, 32, "sha256"),
|
||||
salt
|
||||
]);
|
||||
|
||||
resolve(null);
|
||||
});
|
||||
}
|
||||
|
||||
/**Selects the path for a file given ID */
|
||||
export function getPath(id: number | string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = "SELECT path FROM media WHERE id = ?";
|
||||
|
||||
db.get(query, [id], (err: Error, path: object) => {
|
||||
if (err) {reject(err);}
|
||||
resolve(path);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**Deletes from database given an ID */
|
||||
export function deleteId(database: string, id: number | string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = `DELETE FROM ${database} WHERE id = ?`;
|
||||
|
||||
db.run(query, [id], (err: Error) => {
|
||||
if (err) {reject(err); return;}
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**Expires a database row given a Date in unix time */
|
||||
export function expire(database: string, column: string, expiration:number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = `SELECT * FROM ${database} WHERE ${column} < ?`;
|
||||
|
||||
db.each(query, [expiration], async (err: Error, row: GenericRow) => {
|
||||
if (err) reject(err);
|
||||
await deleteId(database, row.id);
|
||||
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**A generic database row */
|
||||
export interface GenericRow {
|
||||
id? : number | string,
|
||||
username?: string
|
||||
expire? :Date
|
||||
}
|
||||
|
||||
/**A row for the media database */
|
||||
export interface MediaRow {
|
||||
id? : number | string,
|
||||
path: string,
|
||||
expire: Date,
|
||||
username?: string
|
||||
}
|
||||
|
||||
/**Params type for doing work with media database */
|
||||
export type MediaParams = [
|
||||
path: string,
|
||||
expire: Date,
|
||||
username?: string
|
||||
]
|
||||
|
||||
/**A row for the user database */
|
||||
export interface UserRow {
|
||||
id? : number | string,
|
||||
username: string,
|
||||
hashed_password: any,
|
||||
salt: any
|
||||
}
|
1
app/lib/declarations/ffprobepath.d.ts
vendored
Normal file
1
app/lib/declarations/ffprobepath.d.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
declare module "@ffprobe-installer/ffprobe";
|
188
app/lib/ffmpeg.ts
Normal file
188
app/lib/ffmpeg.ts
Normal file
|
@ -0,0 +1,188 @@
|
|||
import { extension, videoExtensions, imageExtensions } from "./lib";
|
||||
|
||||
import ffmpeg from 'fluent-ffmpeg';
|
||||
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
|
||||
import ffprobeInstaller from '@ffprobe-installer/ffprobe';
|
||||
import which from 'which';
|
||||
|
||||
/**
|
||||
* Enum to represent different types of video encoding methods.
|
||||
*
|
||||
* @enum {string}
|
||||
* @property {string} CPU - Uses the libx264 codec for CPU-based encoding
|
||||
* @property {string} NVIDIA - Uses the h264_nvenc codec for NVIDIA GPU-based encoding
|
||||
* @property {string} AMD - Uses the h264_amf codec for AMD GPU-based encoding
|
||||
* @property {string} INTEL - Uses the h264_qsv codec for Intel GPU-based encoding
|
||||
* @property {string} APPLE - Uses the h264_videotoolbox codec for Apple GPU-based encoding
|
||||
*/
|
||||
export enum EncodingType {
|
||||
CPU = 'libx264',
|
||||
NVIDIA = 'h264_nvenc',
|
||||
AMD = 'h264_amf',
|
||||
INTEL = 'h264_qsv',
|
||||
APPLE = 'h264_videotoolbox'
|
||||
}
|
||||
|
||||
/**
|
||||
* The current encoding type being used for video encoding.
|
||||
* @type {EncodingType}
|
||||
*/
|
||||
export let currentEncoding: EncodingType = EncodingType.CPU;
|
||||
|
||||
/**
|
||||
* Sets the current encoding type.
|
||||
*
|
||||
* @param {EncodingType} type - The encoding type to set.
|
||||
*/
|
||||
export const setEncodingType = (type: EncodingType) => {
|
||||
currentEncoding = type;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the path to an executable by checking environment variables, the system path, or a default installer.
|
||||
*
|
||||
* @param {string} envVar - The environment variable to check for the executable's path.
|
||||
* @param {string} executable - The name of the executable to search for in the system path.
|
||||
* @param {Object} installer - An object containing the default installer path.
|
||||
* @param {string} installer.path - The default path to use if the executable is not found in the environment or system path.
|
||||
*
|
||||
* @returns {string} - The path to the executable.
|
||||
* @throws Will throw an error if the executable is not found and the installer path is not available.
|
||||
*/
|
||||
const getExecutablePath = (envVar: string, executable: string, installer: { path: string }) => {
|
||||
if (process.env[envVar]) {
|
||||
return process.env[envVar];
|
||||
}
|
||||
|
||||
try {
|
||||
return which.sync(executable);
|
||||
} catch (error) {
|
||||
return installer.path;
|
||||
}
|
||||
};
|
||||
|
||||
const ffmpegPath = getExecutablePath('EB_FFMPEG_PATH', 'ffmpeg', ffmpegInstaller);
|
||||
const ffprobePath = getExecutablePath('EB_FFPROBE_PATH', 'ffprobe', ffprobeInstaller);
|
||||
|
||||
console.log(`Using ffmpeg from path: ${ffmpegPath}`);
|
||||
console.log(`Using ffprobe from path: ${ffprobePath}`);
|
||||
|
||||
ffmpeg.setFfmpegPath(ffmpegPath!);
|
||||
ffmpeg.setFfprobePath(ffprobePath!);
|
||||
|
||||
const checkEnvForEncoder = () => {
|
||||
const envEncoder = process.env.EB_ENCODER?.toUpperCase();
|
||||
|
||||
if (envEncoder && Object.keys(EncodingType).includes(envEncoder)) {
|
||||
setEncodingType(EncodingType[envEncoder as keyof typeof EncodingType] as EncodingType);
|
||||
console.log(`Setting encoding type to ${envEncoder} based on environment variable.`);
|
||||
} else if (envEncoder) {
|
||||
console.warn(`Invalid encoder value "${envEncoder}" in environment variable, defaulting to CPU.`);
|
||||
}
|
||||
};
|
||||
|
||||
checkEnvForEncoder();
|
||||
|
||||
/**
|
||||
* Downscale a video using ffmpeg with various encoding options.
|
||||
*
|
||||
* @param {string} path - The input video file path.
|
||||
* @param {string} filename - The name of the file.
|
||||
* @param {string} extension - The file extension of the file
|
||||
* @returns {Promise<void>} - A promise that resolves when the downscaling is complete, and rejects on error.
|
||||
*
|
||||
* @example
|
||||
* ffmpegDownscale('input.mp4').then(() => {
|
||||
* console.log('Downscaling complete.');
|
||||
* }).catch((error) => {
|
||||
* console.log(`Error: ${error}`);
|
||||
* });
|
||||
*/
|
||||
export const ffmpegDownscale = (path: string, filename: string, extension: string) => {
|
||||
const startTime = Date.now();
|
||||
const outputOptions = [
|
||||
'-vf', 'scale=-2:720',
|
||||
'-c:v', currentEncoding,
|
||||
'-c:a', 'copy',
|
||||
];
|
||||
|
||||
// Adjust output options based on encoder for maximum quality
|
||||
switch (currentEncoding) {
|
||||
case EncodingType.CPU:
|
||||
outputOptions.push('-crf', '0');
|
||||
break;
|
||||
case EncodingType.NVIDIA:
|
||||
outputOptions.push('-rc', 'cqp', '-qp', '0');
|
||||
break;
|
||||
case EncodingType.AMD:
|
||||
outputOptions.push('-qp_i', '0', '-qp_p', '0', '-qp_b', '0');
|
||||
break;
|
||||
case EncodingType.INTEL:
|
||||
outputOptions.push('-global_quality', '1'); // Intel QSV specific setting for high quality
|
||||
break;
|
||||
case EncodingType.APPLE:
|
||||
outputOptions.push('-global_quality', '1');
|
||||
break;
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
ffmpeg()
|
||||
.input(path)
|
||||
.outputOptions(outputOptions)
|
||||
.output(`uploads/720p-${filename}${extension}`)
|
||||
.on('end', () => {
|
||||
console.log(`720p copy complete using ${currentEncoding}, took ${Date.now() - startTime}ms to complete`);
|
||||
resolve();
|
||||
})
|
||||
.on('error', (e) => reject(new Error(e)))
|
||||
.run();
|
||||
});
|
||||
}
|
||||
|
||||
/** Converts video to gif and vice versa using ffmpeg */
|
||||
/**export const convert: Middleware = (req, res, next) => {
|
||||
const files = req.files as Express.Multer.File[];
|
||||
|
||||
for (const file in files) {
|
||||
const nameAndExtension = extension(files[file].originalname);
|
||||
|
||||
if (videoExtensions.includes(nameAndExtension[1])) {
|
||||
console.log("Converting " + nameAndExtension[0] + nameAndExtension[1] + " to gif");
|
||||
console.log(`Using ${currentEncoding} as encoder`);
|
||||
const startTime = Date.now();
|
||||
ffmpeg()
|
||||
.input(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
||||
.inputFormat(nameAndExtension[1].substring(1))
|
||||
.outputOptions(`-c:v ${currentEncoding}`)
|
||||
.outputFormat("gif")
|
||||
.output(`uploads/${nameAndExtension[0]}.gif`)
|
||||
.on("end", function() {
|
||||
console.log(`Conversion complete, took ${Date.now() - startTime} to complete`);
|
||||
console.log(`Uploaded to uploads/${nameAndExtension[0]}.gif`);
|
||||
})
|
||||
.on("error", (e) => console.log(e))
|
||||
.run();
|
||||
} else if (nameAndExtension[1] == ".gif") {
|
||||
console.log(`Converting ${nameAndExtension[0]}${nameAndExtension[1]} to mp4`);
|
||||
console.log(`Using ${currentEncoding} as encoder`);
|
||||
|
||||
const startTime = Date.now();
|
||||
ffmpeg(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
||||
.inputFormat("gif")
|
||||
.outputFormat("mp4")
|
||||
.outputOptions([
|
||||
"-pix_fmt yuv420p",
|
||||
`-c:v ${currentEncoding}`,
|
||||
"-movflags +faststart"
|
||||
])
|
||||
.noAudio()
|
||||
.output(`uploads/${nameAndExtension[0]}.mp4`)
|
||||
.on("end", function() {
|
||||
console.log(`Conversion complete, took ${Date.now() - startTime} to complete`);
|
||||
console.log(`Uploaded to uploads/${nameAndExtension[0]}.mp4`);
|
||||
next();
|
||||
})
|
||||
.run();
|
||||
}
|
||||
}
|
||||
};**/
|
26
app/lib/lib.ts
Normal file
26
app/lib/lib.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* eslint-disable @typescript-eslint/no-namespace */
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface User {
|
||||
id? : number | string,
|
||||
username: string,
|
||||
hashed_password?: any,
|
||||
salt?: any
|
||||
}
|
||||
}
|
||||
}
|
||||
/**Splits a file name into its name and then its extension */
|
||||
export function extension(str: string){
|
||||
const file = str.split("/").pop();
|
||||
return [file.substr(0,file.lastIndexOf(".")),file.substr(file.lastIndexOf("."),file.length).toLowerCase()];
|
||||
}
|
||||
/**Type for user data */
|
||||
export interface User {
|
||||
id? : number | string,
|
||||
username: string,
|
||||
hashed_password?: any,
|
||||
salt?: any
|
||||
}
|
||||
|
||||
export const videoExtensions = [".mp4", ".mov", ".avi", ".flv", ".mkv", ".wmv", ".webm"];
|
||||
export const imageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg", ".tiff", ".webp"];
|
107
app/lib/middleware.ts
Normal file
107
app/lib/middleware.ts
Normal file
|
@ -0,0 +1,107 @@
|
|||
import type {RequestHandler as Middleware, NextFunction} from "express";
|
||||
|
||||
import fs from "fs";
|
||||
import process from "process";
|
||||
|
||||
import {extension, videoExtensions, imageExtensions} from "./lib";
|
||||
import {db, MediaParams, insertToDB} from "./db";
|
||||
import {ffmpegDownscale} from "./ffmpeg";
|
||||
|
||||
export const checkAuth: Middleware = (req, res, next) => {
|
||||
if (!req.user) {
|
||||
return res.status(401);
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
/**Checks shareX auth key */
|
||||
export const checkSharexAuth: Middleware = (req, res, next) => {
|
||||
const auth = process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY";
|
||||
let key = null;
|
||||
|
||||
if (req.headers["key"]) {
|
||||
key = req.headers["key"];
|
||||
} else {
|
||||
return res.status(400).send("{success: false, message: \"No key provided\", fix: \"Provide a key\"}");
|
||||
}
|
||||
|
||||
if (auth != key) {
|
||||
return res.status(401).send("{success: false, message: '\"'Invalid key\", fix: \"Provide a valid key\"}");
|
||||
}
|
||||
|
||||
const shortKey = key.substr(0, 3) + "...";
|
||||
console.log(`Authenicated user with key: ${shortKey}`);
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
/**Creates oembed json file for embed metadata */
|
||||
export const createEmbedData: Middleware = (req, res, next) => {
|
||||
const files = req.files as Express.Multer.File[];
|
||||
for (const file in files) {
|
||||
const nameAndExtension = extension(files[file].originalname);
|
||||
const oembed = {
|
||||
type: "video",
|
||||
version: "1.0",
|
||||
provider_name: "embedder",
|
||||
provider_url: "https://github.com/WaveringAna/embedder",
|
||||
cache_age: 86400,
|
||||
html: `<iframe src='${req.protocol}://${req.get("host")}/gifv/${nameAndExtension[0]}${nameAndExtension[1]}'></iframe>`,
|
||||
width: 640,
|
||||
height: 360
|
||||
};
|
||||
|
||||
fs.writeFile(`uploads/oembed-${nameAndExtension[0]}${nameAndExtension[1]}.json`, JSON.stringify(oembed), function (err) {
|
||||
if (err) return next(err);
|
||||
console.log(`oembed file created ${nameAndExtension[0]}${nameAndExtension[1]}.json`);
|
||||
});
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
/**Creates a 720p copy of video for smaller file */
|
||||
export const convertTo720p: Middleware = (req, res, next) => {
|
||||
const files = req.files as Express.Multer.File[];
|
||||
console.log("convert to 720p running");
|
||||
for (const file in files) {
|
||||
const nameAndExtension = extension(files[file].originalname);
|
||||
|
||||
//Skip if not a video
|
||||
if (!videoExtensions.includes(nameAndExtension[1]) && nameAndExtension[1] !== ".gif") {
|
||||
console.log(`${files[file].originalname} is not a video file`);
|
||||
console.log(nameAndExtension[1]);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`Creating 720p for ${files[file].originalname}`);
|
||||
|
||||
ffmpegDownscale(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`, nameAndExtension[0], nameAndExtension[1]).then(() => {
|
||||
//Nothing for now, can fire event flag that it is done to front end when react conversion is done
|
||||
}).catch((error) => {
|
||||
console.log(`Error: ${error}`);
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
/**Middleware for handling uploaded files. Inserts it into the database */
|
||||
export const handleUpload: Middleware = (req, res, next) => {
|
||||
if (!req.file && !req.files) {
|
||||
console.log("No files were uploaded");
|
||||
return res.status(400).send("No files were uploaded.");
|
||||
}
|
||||
|
||||
const files = (req.files) ? req.files as Express.Multer.File[] : req.file; //Check if a single file was uploaded or multiple
|
||||
const username = (req.user) ? req.user.username : "sharex"; //if no username was provided, we can presume that it is sharex
|
||||
const expireDate: Date = (req.body.expire) ? new Date(Date.now() + (req.body.expire * 24 * 60 * 60 * 1000)) : null;
|
||||
|
||||
if (files instanceof Array) {
|
||||
for (const file in files) {
|
||||
insertToDB(files[file].filename, expireDate, username);
|
||||
}
|
||||
} else
|
||||
insertToDB(files.filename, expireDate, username);
|
||||
|
||||
next();
|
||||
};
|
72
app/lib/multer.ts
Normal file
72
app/lib/multer.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import {Request} from "express";
|
||||
import multer, {FileFilterCallback} from "multer";
|
||||
|
||||
import {db, MediaRow} from "./db";
|
||||
import {extension} from "./lib";
|
||||
|
||||
export type DestinationCallback = (error: Error | null, destination: string) => void
|
||||
export type FileNameCallback = (error: Error | null, filename: string) => void
|
||||
|
||||
export const fileStorage = multer.diskStorage({
|
||||
destination: (
|
||||
request: Request,
|
||||
file: Express.Multer.File,
|
||||
callback: DestinationCallback
|
||||
): void => {
|
||||
callback(null, __dirname + "/../../uploads");
|
||||
},
|
||||
filename: (
|
||||
request: Request,
|
||||
file: Express.Multer.File,
|
||||
callback: FileNameCallback
|
||||
): void => {
|
||||
const nameAndExtension = extension(file.originalname);
|
||||
console.log(`Uploading ${file}`);
|
||||
db.all("SELECT * FROM media WHERE path = ?", [nameAndExtension[0] + nameAndExtension[1]], (err: Error, exists: []) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(err, null);
|
||||
}
|
||||
if (exists.length != 0) {
|
||||
const suffix = new Date().getTime() / 1000;
|
||||
|
||||
if (request.body.title == "" || request.body.title == null || request.body.title == undefined) {
|
||||
callback(null, nameAndExtension[0] + "-" + suffix + nameAndExtension[1]);
|
||||
} else {
|
||||
callback(null, request.body.title + "-" + suffix + nameAndExtension[1]);
|
||||
}
|
||||
} else {
|
||||
if (request.body.title == "" || request.body.title == null || request.body.title == undefined) {
|
||||
callback(null, nameAndExtension[0] + nameAndExtension[1]);
|
||||
} else {
|
||||
callback(null, request.body.title + nameAndExtension[1]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const allowedMimeTypes = [
|
||||
"image/png",
|
||||
"image/jpg",
|
||||
"image/jpeg",
|
||||
"image/gif",
|
||||
"image/webp",
|
||||
"video/mp4",
|
||||
"video/mov",
|
||||
"video/webm",
|
||||
"audio/mpeg",
|
||||
"audio/ogg"
|
||||
];
|
||||
|
||||
export const fileFilter = (
|
||||
request: Request,
|
||||
file: Express.Multer.File,
|
||||
callback: FileFilterCallback
|
||||
): void => {
|
||||
if (allowedMimeTypes.includes(file.mimetype)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, false);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue