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

View file

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

View file

@ -54,7 +54,7 @@ export const fileStorage = multer.diskStorage({
) {
filenameSet = false;
}
if (exists.length != 0) {
existsBool = true;
suffix = new Date().getTime() / 1000;
@ -66,30 +66,41 @@ export const fileStorage = multer.diskStorage({
//Random string of 8 alphanumeric characters
//Chance of collision is extremely low, not worth checking for
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;
}
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);
return;
}
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);
return;
}
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);
return;
}
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);
return;
}