This commit is contained in:
waveringana 2024-01-16 11:50:40 -05:00
parent 270c681438
commit ba9eee1307
No known key found for this signature in database
3 changed files with 49 additions and 37 deletions

View file

@ -19,7 +19,7 @@ export function createDatabase(version: number) {
expire INTEGER, \ expire INTEGER, \
salt BLOB \ salt BLOB \
)", )",
() => createUser("admin", process.env.EBPASS || "changeme") () => createUser("admin", process.env.EBPASS || "changeme"),
); );
db.run( db.run(
@ -28,7 +28,7 @@ export function createDatabase(version: number) {
path TEXT NOT NULL, \ path TEXT NOT NULL, \
expire INTEGER, \ expire INTEGER, \
username TEXT \ username TEXT \
)" )",
); );
db.run( db.run(
@ -36,7 +36,7 @@ export function createDatabase(version: number) {
id INTEGER PRIMARY KEY, \ id INTEGER PRIMARY KEY, \
downsclaing BOOLEAN, \ downsclaing BOOLEAN, \
namerandomization BOOLEAN \ namerandomization BOOLEAN \
)" )",
); );
db.run(`PRAGMA user_version = ${version}`); db.run(`PRAGMA user_version = ${version}`);
@ -49,7 +49,7 @@ export function updateDatabase(oldVersion: number, newVersion: number) {
db.run("PRAGMA user_version = 3", (err) => { db.run("PRAGMA user_version = 3", (err) => {
if (err) return; if (err) return;
}); });
db.run("ALTER TABLE media ADD COLUMN username TEXT", (err) => { db.run("ALTER TABLE media ADD COLUMN username TEXT", (err) => {
if (err) return; if (err) return;
}); });
@ -63,7 +63,7 @@ export function updateDatabase(oldVersion: number, newVersion: number) {
id INTEGER PRIMARY KEY, \ id INTEGER PRIMARY KEY, \
downsclaing BOOLEAN, \ downsclaing BOOLEAN, \
namerandomization BOOLEAN \ namerandomization BOOLEAN \
)" )",
); );
} }
if (oldVersion == 2) { if (oldVersion == 2) {
@ -77,7 +77,7 @@ export function updateDatabase(oldVersion: number, newVersion: number) {
id INTEGER PRIMARY KEY, \ id INTEGER PRIMARY KEY, \
downsclaing BOOLEAN, \ downsclaing BOOLEAN, \
namerandomization BOOLEAN \ namerandomization BOOLEAN \
)" )",
); );
} }
} }
@ -86,7 +86,7 @@ export function updateDatabase(oldVersion: number, newVersion: number) {
export function insertToDB( export function insertToDB(
filename: string, filename: string,
expireDate: Date, expireDate: Date,
username: string username: string,
): Promise<void> { ): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const params: MediaParams = [filename, expireDate, username]; const params: MediaParams = [filename, expireDate, username];
@ -105,7 +105,7 @@ export function insertToDB(
console.log(`It will expire on ${expireDate}`); console.log(`It will expire on ${expireDate}`);
resolve(); resolve();
} }
} },
); );
}); });
} }
@ -131,7 +131,7 @@ export function createUser(username: string, password: string) {
db.run( db.run(
"INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)", "INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)",
[username, crypto.pbkdf2Sync(password, salt, 310000, 32, "sha256"), salt] [username, crypto.pbkdf2Sync(password, salt, 310000, 32, "sha256"), salt],
); );
resolve(null); resolve(null);

View file

@ -46,17 +46,19 @@ export const checkSharexAuth: Middleware = (req, res, next) => {
/** /**
* Creates oembed data for uploaded files * Creates oembed data for uploaded files
* *
* @param {Express Request Object} Express request object * @param {Express Request Object} Express request object
* @param {Express Response Object} Express response object * @param {Express Response Object} Express response object
* @param {Express NextFunction variable} Express next function * @param {Express NextFunction variable} Express next function
* *
*/ */
export const createEmbedData: Middleware = async (req, res, next) => { export const createEmbedData: Middleware = async (req, res, next) => {
const files = req.files as Express.Multer.File[]; const files = req.files as Express.Multer.File[];
for (const file in files) { for (const file in files) {
const [filename, fileExtension] = extension(files[file].filename); const [filename, fileExtension] = extension(files[file].filename);
const isMedia = videoExtensions.includes(fileExtension) || imageExtensions.includes(fileExtension); const isMedia =
videoExtensions.includes(fileExtension) ||
imageExtensions.includes(fileExtension);
const oembed: oembedObj = { const oembed: oembedObj = {
type: "video", type: "video",
@ -64,18 +66,20 @@ export const createEmbedData: Middleware = async (req, res, next) => {
provider_name: "embedder", provider_name: "embedder",
provider_url: "https://github.com/WaveringAna/embedder", provider_url: "https://github.com/WaveringAna/embedder",
cache_age: 86400, cache_age: 86400,
html: `<iframe src='${req.protocol}://${req.get("host")}/gifv/${ html: `<iframe src='${req.protocol}://${req.get(
filename "host",
}${fileExtension}'></iframe>` )}/gifv/${filename}${fileExtension}'></iframe>`,
}; };
if (isMedia) { if (isMedia) {
let ffProbeData; let ffProbeData;
try { ffProbeData = await ffProbe( try {
`uploads/${files[file].filename}`, ffProbeData = await ffProbe(
filename, `uploads/${files[file].filename}`,
fileExtension, filename,
); } catch (error) { fileExtension,
);
} catch (error) {
console.log(`Error: ${error}`); console.log(`Error: ${error}`);
} }
@ -88,9 +92,7 @@ export const createEmbedData: Middleware = async (req, res, next) => {
JSON.stringify(oembed), JSON.stringify(oembed),
function (err) { function (err) {
if (err) return next(err); if (err) return next(err);
console.log( console.log(`oembed file created ${filename}${fileExtension}.json`);
`oembed file created ${filename}${fileExtension}.json`,
);
}, },
); );
} }
@ -99,11 +101,11 @@ export const createEmbedData: Middleware = async (req, res, next) => {
/** /**
* Creates a 720p copy of uploaded videos * Creates a 720p copy of uploaded videos
* *
* @param {Express Request Object} req Express request object * @param {Express Request Object} req Express request object
* @param {Express Response Object} res Express response object * @param {Express Response Object} res Express response object
* @param {Express NextFunction} next Express next function * @param {Express NextFunction} next Express next function
* *
*/ */
export const convertTo720p: Middleware = (req, res, next) => { export const convertTo720p: Middleware = (req, res, next) => {
const files = req.files as Express.Multer.File[]; const files = req.files as Express.Multer.File[];
@ -112,10 +114,7 @@ export const convertTo720p: Middleware = (req, res, next) => {
const [filename, fileExtension] = extension(files[file].filename); const [filename, fileExtension] = extension(files[file].filename);
//Skip if not a video //Skip if not a video
if ( if (!videoExtensions.includes(fileExtension) && fileExtension !== ".gif") {
!videoExtensions.includes(fileExtension) &&
fileExtension !== ".gif"
) {
console.log(`${files[file].filename} is not a video file`); console.log(`${files[file].filename} is not a video file`);
continue; continue;
} }
@ -153,7 +152,9 @@ export const handleUpload: Middleware = async (req, res, next) => {
try { try {
if (files instanceof Array) { if (files instanceof Array) {
await Promise.all(files.map(file => insertToDB(file.filename, expireDate, username))); await Promise.all(
files.map((file) => insertToDB(file.filename, expireDate, username)),
);
} else { } else {
await insertToDB(files.filename, expireDate, username); await insertToDB(files.filename, expireDate, username);
} }

View file

@ -54,7 +54,7 @@ export const fileStorage = multer.diskStorage({
) { ) {
filenameSet = false; filenameSet = false;
} }
if (exists.length != 0) { if (exists.length != 0) {
existsBool = true; existsBool = true;
suffix = new Date().getTime() / 1000; suffix = new Date().getTime() / 1000;
@ -66,30 +66,41 @@ export const fileStorage = multer.diskStorage({
//Random string of 8 alphanumeric characters //Random string of 8 alphanumeric characters
//Chance of collision is extremely low, not worth checking for //Chance of collision is extremely low, not worth checking for
console.log("Randomizing name"); console.log("Randomizing name");
callback(null, Math.random().toString(36).slice(2, 10) + fileExtension); callback(
null,
Math.random().toString(36).slice(2, 10) + fileExtension,
);
return; return;
} }
if (filenameSet && existsBool) { if (filenameSet && existsBool) {
console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); console.log(
`filenameSet is ${filenameSet} and existsBool is ${existsBool}`,
);
callback(null, request.body.title + "-" + suffix + fileExtension); callback(null, request.body.title + "-" + suffix + fileExtension);
return; return;
} }
if (!filenameSet && existsBool) { if (!filenameSet && existsBool) {
console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); console.log(
`filenameSet is ${filenameSet} and existsBool is ${existsBool}`,
);
callback(null, filename + "-" + suffix + fileExtension); callback(null, filename + "-" + suffix + fileExtension);
return; return;
} }
if (filenameSet && !existsBool) { if (filenameSet && !existsBool) {
console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); console.log(
`filenameSet is ${filenameSet} and existsBool is ${existsBool}`,
);
callback(null, request.body.title + fileExtension); callback(null, request.body.title + fileExtension);
return; return;
} }
if (!filenameSet && !existsBool) { if (!filenameSet && !existsBool) {
console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); console.log(
`filenameSet is ${filenameSet} and existsBool is ${existsBool}`,
);
callback(null, filename + fileExtension); callback(null, filename + fileExtension);
return; return;
} }