my nvim and vscode are finally using the same settings
This commit is contained in:
parent
58077a5d63
commit
a5e03facbe
11 changed files with 757 additions and 757 deletions
194
app/lib/db.ts
194
app/lib/db.ts
|
@ -9,176 +9,176 @@ 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");
|
||||
console.log("Creating database");
|
||||
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS users ( \
|
||||
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"),
|
||||
);
|
||||
() => createUser("admin", process.env.EBPASS || "changeme"),
|
||||
);
|
||||
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS media ( \
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS media ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
path TEXT NOT NULL, \
|
||||
expire INTEGER, \
|
||||
username TEXT \
|
||||
)",
|
||||
);
|
||||
);
|
||||
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
downsclaing BOOLEAN, \
|
||||
namerandomization BOOLEAN \
|
||||
)",
|
||||
);
|
||||
);
|
||||
|
||||
db.run(`PRAGMA user_version = ${version}`);
|
||||
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 = 3", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
if (oldVersion == 1) {
|
||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||
db.run("PRAGMA user_version = 3", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
|
||||
db.run("ALTER TABLE media ADD COLUMN username TEXT", (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;
|
||||
});
|
||||
db.run("ALTER TABLE users ADD COLUMN expire TEXT", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
downsclaing BOOLEAN, \
|
||||
namerandomization BOOLEAN \
|
||||
)",
|
||||
);
|
||||
}
|
||||
if (oldVersion == 2) {
|
||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||
db.run("PRAGMA user_version = 3", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
);
|
||||
}
|
||||
if (oldVersion == 2) {
|
||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||
db.run("PRAGMA user_version = 3", (err) => {
|
||||
if (err) return;
|
||||
});
|
||||
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
db.run(
|
||||
"CREATE TABLE IF NOT EXISTS settings ( \
|
||||
id INTEGER PRIMARY KEY, \
|
||||
downsclaing BOOLEAN, \
|
||||
namerandomization BOOLEAN \
|
||||
)",
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**Inserts into the media table */
|
||||
export function insertToDB(
|
||||
filename: string,
|
||||
expireDate: Date,
|
||||
username: string,
|
||||
filename: string,
|
||||
expireDate: Date,
|
||||
username: string,
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const params: MediaParams = [filename, expireDate, username];
|
||||
return new Promise((resolve, reject) => {
|
||||
const params: MediaParams = [filename, expireDate, username];
|
||||
|
||||
db.run(
|
||||
"INSERT INTO media (path, expire, username) VALUES (?, ?, ?)",
|
||||
params,
|
||||
function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
} else {
|
||||
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}`);
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
db.run(
|
||||
"INSERT INTO media (path, expire, username) VALUES (?, ?, ?)",
|
||||
params,
|
||||
function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
} else {
|
||||
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}`);
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**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}`);
|
||||
});
|
||||
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}`);
|
||||
});
|
||||
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);
|
||||
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],
|
||||
);
|
||||
db.run(
|
||||
"INSERT OR IGNORE INTO users (username, hashed_password, salt) VALUES (?, ?, ?)",
|
||||
[username, crypto.pbkdf2Sync(password, salt, 310000, 32, "sha256"), salt],
|
||||
);
|
||||
|
||||
resolve(null);
|
||||
});
|
||||
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 = ?";
|
||||
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);
|
||||
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 = ?`;
|
||||
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);
|
||||
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} < ?`;
|
||||
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);
|
||||
db.each(query, [expiration], async (err: Error, row: GenericRow) => {
|
||||
if (err) reject(err);
|
||||
await deleteId(database, row.id);
|
||||
|
||||
resolve(null);
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**A generic database row */
|
||||
|
|
|
@ -37,7 +37,7 @@ export let currentEncoding: EncodingType = EncodingType.CPU;
|
|||
* @param {EncodingType} type - The encoding type to set.
|
||||
*/
|
||||
export const setEncodingType = (type: EncodingType) => {
|
||||
currentEncoding = type;
|
||||
currentEncoding = type;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -52,31 +52,31 @@ export const setEncodingType = (type: EncodingType) => {
|
|||
* @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 },
|
||||
envVar: string,
|
||||
executable: string,
|
||||
installer: { path: string },
|
||||
): string => {
|
||||
if (process.env[envVar]) {
|
||||
return process.env[envVar];
|
||||
}
|
||||
if (process.env[envVar]) {
|
||||
return process.env[envVar];
|
||||
}
|
||||
|
||||
try {
|
||||
return which.sync(executable);
|
||||
} catch (error) {
|
||||
return installer.path;
|
||||
}
|
||||
try {
|
||||
return which.sync(executable);
|
||||
} catch (error) {
|
||||
return installer.path;
|
||||
}
|
||||
};
|
||||
|
||||
const ffmpegPath = getExecutablePath(
|
||||
"EB_FFMPEG_PATH",
|
||||
"ffmpeg",
|
||||
ffmpegInstaller,
|
||||
"EB_FFMPEG_PATH",
|
||||
"ffmpeg",
|
||||
ffmpegInstaller,
|
||||
);
|
||||
|
||||
const ffprobePath = getExecutablePath(
|
||||
"EB_FFPROBE_PATH",
|
||||
"ffprobe",
|
||||
ffprobeInstaller,
|
||||
"EB_FFPROBE_PATH",
|
||||
"ffprobe",
|
||||
ffprobeInstaller,
|
||||
);
|
||||
|
||||
console.log(`Using ffmpeg from path: ${ffmpegPath}`);
|
||||
|
@ -86,20 +86,20 @@ ffmpeg.setFfmpegPath(ffmpegPath!);
|
|||
ffmpeg.setFfprobePath(ffprobePath!);
|
||||
|
||||
const checkEnvForEncoder = () => {
|
||||
const envEncoder = process.env.EB_ENCODER?.toUpperCase();
|
||||
const envEncoder = process.env.EB_ENCODER?.toUpperCase();
|
||||
|
||||
if (envEncoder && Object.keys(EncodingType).includes(envEncoder)) {
|
||||
setEncodingType(
|
||||
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.`,
|
||||
);
|
||||
}
|
||||
);
|
||||
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();
|
||||
|
@ -120,57 +120,57 @@ checkEnvForEncoder();
|
|||
* });
|
||||
*/
|
||||
export const ffmpegDownscale = (
|
||||
path: string,
|
||||
filename: string,
|
||||
extension: string,
|
||||
path: string,
|
||||
filename: string,
|
||||
extension: string,
|
||||
): Promise<void> => {
|
||||
const startTime = Date.now();
|
||||
const outputOptions = [
|
||||
"-vf",
|
||||
"scale=-2:720",
|
||||
"-c:v",
|
||||
currentEncoding,
|
||||
"-c:a",
|
||||
"copy",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
];
|
||||
const startTime = Date.now();
|
||||
const outputOptions = [
|
||||
"-vf",
|
||||
"scale=-2:720",
|
||||
"-c:v",
|
||||
currentEncoding,
|
||||
"-c:a",
|
||||
"copy",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
];
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const progressFile = `uploads/${filename}${extension}-progress.json`;
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const progressFile = `uploads/${filename}${extension}-progress.json`;
|
||||
|
||||
ffmpeg()
|
||||
.input(path)
|
||||
.outputOptions(outputOptions)
|
||||
.output(`uploads/720p-${filename}${extension}`)
|
||||
.on("progress", function (progress) {
|
||||
fs.writeFileSync(
|
||||
progressFile,
|
||||
JSON.stringify({ progress: progress.percent / 100 }),
|
||||
);
|
||||
})
|
||||
.on("end", () => {
|
||||
console.log(
|
||||
`720p copy complete using ${currentEncoding}, took ${
|
||||
Date.now() - startTime
|
||||
}ms to complete`,
|
||||
);
|
||||
ffmpeg()
|
||||
.input(path)
|
||||
.outputOptions(outputOptions)
|
||||
.output(`uploads/720p-${filename}${extension}`)
|
||||
.on("progress", function (progress) {
|
||||
fs.writeFileSync(
|
||||
progressFile,
|
||||
JSON.stringify({ progress: progress.percent / 100 }),
|
||||
);
|
||||
})
|
||||
.on("end", () => {
|
||||
console.log(
|
||||
`720p copy complete using ${currentEncoding}, took ${
|
||||
Date.now() - startTime
|
||||
}ms to complete`,
|
||||
);
|
||||
|
||||
// Delete the .processing file
|
||||
fs.unlinkSync(progressFile);
|
||||
// Delete the .processing file
|
||||
fs.unlinkSync(progressFile);
|
||||
|
||||
resolve();
|
||||
})
|
||||
.on("error", (e) => {
|
||||
// Ensure to delete the .processing file even on error
|
||||
if (fs.existsSync(progressFile)) {
|
||||
fs.unlinkSync(progressFile);
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
.on("error", (e) => {
|
||||
// Ensure to delete the .processing file even on error
|
||||
if (fs.existsSync(progressFile)) {
|
||||
fs.unlinkSync(progressFile);
|
||||
}
|
||||
|
||||
reject(new Error(e));
|
||||
})
|
||||
.run();
|
||||
});
|
||||
reject(new Error(e));
|
||||
})
|
||||
.run();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -189,80 +189,80 @@ export const ffmpegDownscale = (
|
|||
* });
|
||||
*/
|
||||
export const ffmpegConvert = (
|
||||
path: string,
|
||||
filename: string,
|
||||
extension: string,
|
||||
path: string,
|
||||
filename: string,
|
||||
extension: string,
|
||||
): Promise<void> => {
|
||||
const startTime = Date.now();
|
||||
const outputOptions = [
|
||||
"-vf",
|
||||
"scale=-2:720",
|
||||
"-c:v",
|
||||
currentEncoding,
|
||||
"-c:a",
|
||||
"copy",
|
||||
"-movflags",
|
||||
"+faststart",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
];
|
||||
const startTime = Date.now();
|
||||
const outputOptions = [
|
||||
"-vf",
|
||||
"scale=-2:720",
|
||||
"-c:v",
|
||||
currentEncoding,
|
||||
"-c:a",
|
||||
"copy",
|
||||
"-movflags",
|
||||
"+faststart",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
];
|
||||
|
||||
let outputFormat: string;
|
||||
let outputFormat: string;
|
||||
|
||||
if (videoExtensions.includes(extension)) {
|
||||
outputFormat = ".gif";
|
||||
} else if (extension == ".gif") {
|
||||
outputFormat = ".mp4";
|
||||
} else {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
reject(`Submitted file is neither a video nor a gif: ${path}`);
|
||||
});
|
||||
}
|
||||
|
||||
if (videoExtensions.includes(extension)) {
|
||||
outputFormat = ".gif";
|
||||
} else if (extension == ".gif") {
|
||||
outputFormat = ".mp4";
|
||||
} else {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
reject(`Submitted file is neither a video nor a gif: ${path}`);
|
||||
const progressFile = `uploads/${filename}${extension}-progress.json`;
|
||||
|
||||
ffmpeg()
|
||||
.input(path)
|
||||
.outputOptions(outputOptions)
|
||||
.output("uploads/")
|
||||
.outputFormat(outputFormat)
|
||||
.output(`uploads/${filename}${outputFormat}`)
|
||||
.on("progress", function (progress) {
|
||||
fs.writeFileSync(
|
||||
progressFile,
|
||||
JSON.stringify({ progress: progress.percent / 100 }),
|
||||
);
|
||||
})
|
||||
.on("end", function () {
|
||||
console.log(
|
||||
`Conversion complete, took ${Date.now() - startTime} to complete`,
|
||||
);
|
||||
console.log(`uploads/${filename}${outputFormat}`);
|
||||
resolve();
|
||||
})
|
||||
.on("error", (e) => reject(e))
|
||||
.run();
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const progressFile = `uploads/${filename}${extension}-progress.json`;
|
||||
|
||||
ffmpeg()
|
||||
.input(path)
|
||||
.outputOptions(outputOptions)
|
||||
.output("uploads/")
|
||||
.outputFormat(outputFormat)
|
||||
.output(`uploads/${filename}${outputFormat}`)
|
||||
.on("progress", function (progress) {
|
||||
fs.writeFileSync(
|
||||
progressFile,
|
||||
JSON.stringify({ progress: progress.percent / 100 }),
|
||||
);
|
||||
})
|
||||
.on("end", function () {
|
||||
console.log(
|
||||
`Conversion complete, took ${Date.now() - startTime} to complete`,
|
||||
);
|
||||
console.log(`uploads/${filename}${outputFormat}`);
|
||||
resolve();
|
||||
})
|
||||
.on("error", (e) => reject(e))
|
||||
.run();
|
||||
});
|
||||
};
|
||||
|
||||
export const ffProbe = async (
|
||||
path: string,
|
||||
filename: string,
|
||||
extension: string,
|
||||
path: string,
|
||||
filename: string,
|
||||
extension: string,
|
||||
) => {
|
||||
return new Promise<FfprobeData>((resolve, reject) => {
|
||||
if (
|
||||
!videoExtensions.includes(extension) &&
|
||||
return new Promise<FfprobeData>((resolve, reject) => {
|
||||
if (
|
||||
!videoExtensions.includes(extension) &&
|
||||
!imageExtensions.includes(extension)
|
||||
) {
|
||||
console.log(`Extension is ${extension}`);
|
||||
reject(`Submitted file is neither a video nor an image: ${path}`);
|
||||
}
|
||||
) {
|
||||
console.log(`Extension is ${extension}`);
|
||||
reject(`Submitted file is neither a video nor an image: ${path}`);
|
||||
}
|
||||
|
||||
ffprobe(path, (err, data) => {
|
||||
if (err) reject(err);
|
||||
resolve(data);
|
||||
ffprobe(path, (err, data) => {
|
||||
if (err) reject(err);
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -11,11 +11,11 @@ declare global {
|
|||
}
|
||||
/**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(),
|
||||
];
|
||||
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 {
|
||||
|
@ -37,21 +37,21 @@ export interface oembedObj {
|
|||
}
|
||||
|
||||
export const videoExtensions = [
|
||||
".mp4",
|
||||
".mov",
|
||||
".avi",
|
||||
".flv",
|
||||
".mkv",
|
||||
".wmv",
|
||||
".webm",
|
||||
".mp4",
|
||||
".mov",
|
||||
".avi",
|
||||
".flv",
|
||||
".mkv",
|
||||
".wmv",
|
||||
".webm",
|
||||
];
|
||||
export const imageExtensions = [
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".png",
|
||||
".gif",
|
||||
".bmp",
|
||||
".svg",
|
||||
".tiff",
|
||||
".webp",
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".png",
|
||||
".gif",
|
||||
".bmp",
|
||||
".svg",
|
||||
".tiff",
|
||||
".webp",
|
||||
];
|
||||
|
|
|
@ -8,40 +8,40 @@ import { insertToDB } from "./db";
|
|||
import { ffmpegDownscale, ffProbe } from "./ffmpeg";
|
||||
|
||||
export const checkAuth: Middleware = (req, res, next) => {
|
||||
if (!req.user) {
|
||||
return res.status(401);
|
||||
}
|
||||
next();
|
||||
if (!req.user) {
|
||||
return res.status(401);
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
/**Checks shareX auth key */
|
||||
export const checkSharexAuth: Middleware = (req, res, next) => {
|
||||
const auth =
|
||||
const auth =
|
||||
process.env.EBAPI_KEY || process.env.EBPASS || "pleaseSetAPI_KEY";
|
||||
let key = null;
|
||||
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 (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'}",
|
||||
);
|
||||
}
|
||||
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}`);
|
||||
const shortKey = key.substr(0, 3) + "...";
|
||||
console.log(`Authenicated user with key: ${shortKey}`);
|
||||
|
||||
next();
|
||||
next();
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -53,50 +53,50 @@ export const checkSharexAuth: Middleware = (req, res, next) => {
|
|||
*
|
||||
*/
|
||||
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 =
|
||||
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 oembed: oembedObj = {
|
||||
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/${filename}${fileExtension}'></iframe>`,
|
||||
};
|
||||
const oembed: oembedObj = {
|
||||
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/${filename}${fileExtension}'></iframe>`,
|
||||
};
|
||||
|
||||
if (isMedia) {
|
||||
let ffProbeData;
|
||||
try {
|
||||
ffProbeData = await ffProbe(
|
||||
`uploads/${files[file].filename}`,
|
||||
filename,
|
||||
fileExtension,
|
||||
if (isMedia) {
|
||||
let ffProbeData;
|
||||
try {
|
||||
ffProbeData = await ffProbe(
|
||||
`uploads/${files[file].filename}`,
|
||||
filename,
|
||||
fileExtension,
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(`Error: ${error}`);
|
||||
}
|
||||
|
||||
oembed.width = ffProbeData.streams[0].width;
|
||||
oembed.height = ffProbeData.streams[0].height;
|
||||
}
|
||||
|
||||
fs.writeFile(
|
||||
`uploads/oembed-${filename}${fileExtension}.json`,
|
||||
JSON.stringify(oembed),
|
||||
function (err) {
|
||||
if (err) return next(err);
|
||||
console.log(`oembed file created ${filename}${fileExtension}.json`);
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(`Error: ${error}`);
|
||||
}
|
||||
|
||||
oembed.width = ffProbeData.streams[0].width;
|
||||
oembed.height = ffProbeData.streams[0].height;
|
||||
}
|
||||
|
||||
fs.writeFile(
|
||||
`uploads/oembed-${filename}${fileExtension}.json`,
|
||||
JSON.stringify(oembed),
|
||||
function (err) {
|
||||
if (err) return next(err);
|
||||
console.log(`oembed file created ${filename}${fileExtension}.json`);
|
||||
},
|
||||
);
|
||||
}
|
||||
next();
|
||||
next();
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -108,59 +108,59 @@ export const createEmbedData: Middleware = async (req, res, next) => {
|
|||
*
|
||||
*/
|
||||
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 [filename, fileExtension] = extension(files[file].filename);
|
||||
const files = req.files as Express.Multer.File[];
|
||||
console.log("convert to 720p running");
|
||||
for (const file in files) {
|
||||
const [filename, fileExtension] = extension(files[file].filename);
|
||||
|
||||
//Skip if not a video
|
||||
if (!videoExtensions.includes(fileExtension) && fileExtension !== ".gif") {
|
||||
console.log(`${files[file].filename} is not a video file`);
|
||||
continue;
|
||||
//Skip if not a video
|
||||
if (!videoExtensions.includes(fileExtension) && fileExtension !== ".gif") {
|
||||
console.log(`${files[file].filename} is not a video file`);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`Creating 720p for ${files[file].filename}`);
|
||||
|
||||
ffmpegDownscale(
|
||||
`uploads/${filename}${fileExtension}`,
|
||||
filename,
|
||||
fileExtension,
|
||||
)
|
||||
.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}`);
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`Creating 720p for ${files[file].filename}`);
|
||||
|
||||
ffmpegDownscale(
|
||||
`uploads/${filename}${fileExtension}`,
|
||||
filename,
|
||||
fileExtension,
|
||||
)
|
||||
.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();
|
||||
next();
|
||||
};
|
||||
|
||||
/**Middleware for handling uploaded files. Inserts it into the database */
|
||||
export const handleUpload: Middleware = async (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;
|
||||
const username = req.user ? req.user.username : "sharex";
|
||||
const expireDate: Date = req.body.expire
|
||||
? new Date(Date.now() + req.body.expire * 24 * 60 * 60 * 1000)
|
||||
: null;
|
||||
|
||||
try {
|
||||
if (files instanceof Array) {
|
||||
await Promise.all(
|
||||
files.map((file) => insertToDB(file.filename, expireDate, username)),
|
||||
);
|
||||
} else {
|
||||
await insertToDB(files.filename, expireDate, username);
|
||||
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;
|
||||
const username = req.user ? req.user.username : "sharex";
|
||||
const expireDate: Date = req.body.expire
|
||||
? new Date(Date.now() + req.body.expire * 24 * 60 * 60 * 1000)
|
||||
: null;
|
||||
|
||||
try {
|
||||
if (files instanceof Array) {
|
||||
await Promise.all(
|
||||
files.map((file) => insertToDB(file.filename, expireDate, username)),
|
||||
);
|
||||
} else {
|
||||
await insertToDB(files.filename, expireDate, username);
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error("Error in handleUpload:", error);
|
||||
res.status(500).send("Error processing files.");
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error("Error in handleUpload:", error);
|
||||
res.status(500).send("Error processing files.");
|
||||
}
|
||||
};
|
||||
|
|
|
@ -14,126 +14,126 @@ export type FileNameCallback = (error: Error | null, filename: string) => void;
|
|||
let randomizeNames = false;
|
||||
|
||||
if (process.env["EB_RANDOMIZE_NAMES"] === "true") {
|
||||
randomizeNames = true;
|
||||
randomizeNames = true;
|
||||
}
|
||||
|
||||
console.log(`Randomize names is set ${randomizeNames}`);
|
||||
|
||||
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 [filename, fileExtension] = extension(file.originalname);
|
||||
console.log(`Uploading ${file}`);
|
||||
db.all(
|
||||
"SELECT * FROM media WHERE path = ?",
|
||||
[filename + fileExtension],
|
||||
(err: Error, exists: []) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(err, null);
|
||||
}
|
||||
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 [filename, fileExtension] = extension(file.originalname);
|
||||
console.log(`Uploading ${file}`);
|
||||
db.all(
|
||||
"SELECT * FROM media WHERE path = ?",
|
||||
[filename + fileExtension],
|
||||
(err: Error, exists: []) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(err, null);
|
||||
}
|
||||
|
||||
let filenameSet = true;
|
||||
let existsBool = false;
|
||||
let suffix: number;
|
||||
let filenameSet = true;
|
||||
let existsBool = false;
|
||||
let suffix: number;
|
||||
|
||||
if (
|
||||
request.body.title != "" ||
|
||||
if (
|
||||
request.body.title != "" ||
|
||||
request.body.title != null ||
|
||||
request.body.title != undefined
|
||||
) {
|
||||
filenameSet = false;
|
||||
}
|
||||
) {
|
||||
filenameSet = false;
|
||||
}
|
||||
|
||||
if (exists.length != 0) {
|
||||
existsBool = true;
|
||||
suffix = new Date().getTime() / 1000;
|
||||
}
|
||||
if (exists.length != 0) {
|
||||
existsBool = true;
|
||||
suffix = new Date().getTime() / 1000;
|
||||
}
|
||||
|
||||
console.log(request.body.title);
|
||||
console.log(request.body.title);
|
||||
|
||||
if (randomizeNames) {
|
||||
//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,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (randomizeNames) {
|
||||
//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,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (filenameSet && 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}`,
|
||||
);
|
||||
callback(null, request.body.title + "-" + suffix + fileExtension);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!filenameSet && 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}`,
|
||||
);
|
||||
callback(null, filename + "-" + suffix + fileExtension);
|
||||
return;
|
||||
}
|
||||
|
||||
if (filenameSet && !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}`,
|
||||
);
|
||||
callback(null, request.body.title + fileExtension);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!filenameSet && !existsBool) {
|
||||
console.log(
|
||||
`filenameSet is ${filenameSet} and existsBool is ${existsBool}`,
|
||||
);
|
||||
callback(null, filename + fileExtension);
|
||||
return;
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
if (!filenameSet && !existsBool) {
|
||||
console.log(
|
||||
`filenameSet is ${filenameSet} and existsBool is ${existsBool}`,
|
||||
);
|
||||
callback(null, filename + fileExtension);
|
||||
return;
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export let allowedMimeTypes = [
|
||||
"image/png",
|
||||
"image/jpg",
|
||||
"image/jpeg",
|
||||
"image/gif",
|
||||
"image/webp",
|
||||
"video/mp4",
|
||||
"video/mov",
|
||||
"video/webm",
|
||||
"audio/mpeg",
|
||||
"audio/ogg",
|
||||
"image/png",
|
||||
"image/jpg",
|
||||
"image/jpeg",
|
||||
"image/gif",
|
||||
"image/webp",
|
||||
"video/mp4",
|
||||
"video/mov",
|
||||
"video/webm",
|
||||
"audio/mpeg",
|
||||
"audio/ogg",
|
||||
];
|
||||
|
||||
export const setAllowedMimeTypes = (mimeTypes: string[]): void => {
|
||||
allowedMimeTypes = mimeTypes;
|
||||
allowedMimeTypes = mimeTypes;
|
||||
};
|
||||
|
||||
export const fileFilter = (
|
||||
request: Request,
|
||||
file: Express.Multer.File,
|
||||
callback: FileFilterCallback,
|
||||
request: Request,
|
||||
file: Express.Multer.File,
|
||||
callback: FileFilterCallback,
|
||||
): void => {
|
||||
if (allowedMimeTypes.includes(file.mimetype)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, false);
|
||||
}
|
||||
if (allowedMimeTypes.includes(file.mimetype)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, false);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue