parent
89663c696f
commit
8c3f2db3b2
10 changed files with 161 additions and 253 deletions
50
app/app.ts
50
app/app.ts
|
@ -12,13 +12,12 @@ const SQLiteStore = sqlite3(session);
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import http from "http";
|
import http from "http";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { unlink } from 'fs/promises';
|
|
||||||
|
|
||||||
import authRouter from "./routes/auth";
|
import authRouter from "./routes/auth";
|
||||||
import indexRouter from "./routes/index";
|
import indexRouter from "./routes/index";
|
||||||
import adduserRouter from "./routes/adduser";
|
import adduserRouter from "./routes/adduser";
|
||||||
|
|
||||||
import {db, expire, createDatabase, updateDatabase, MediaRow, UserRow} from "./types/db";
|
import {db, expire, createDatabase, updateDatabase, MediaRow} from "./types/db";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
|
@ -69,19 +68,13 @@ function onError(error: any) {
|
||||||
|
|
||||||
|
|
||||||
// Check if there is an existing DB or not, then check if it needs to be updated to new schema
|
// Check if there is an existing DB or not, then check if it needs to be updated to new schema
|
||||||
const row = db.prepare("SELECT * FROM sqlite_master WHERE name ='users' and type='table'").get();
|
db.get("SELECT * FROM sqlite_master WHERE name ='users' and type='table'", async (err, row) => {
|
||||||
|
if (!row) createDatabase(2);
|
||||||
if (!row) {
|
else checkVersion();
|
||||||
createDatabase(2);
|
});
|
||||||
} else {
|
|
||||||
checkVersion();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function checkVersion () {
|
function checkVersion () {
|
||||||
// Using the synchronous API of better-sqlite3
|
db.get("PRAGMA user_version", (err: Error, row: any) => {
|
||||||
const row = db.prepare("PRAGMA user_version").get() as UserRow;
|
|
||||||
|
|
||||||
if (row && row.user_version) {
|
if (row && row.user_version) {
|
||||||
const version = row.user_version;
|
const version = row.user_version;
|
||||||
if (version != 2) console.log("DATABASE IS OUTDATED");
|
if (version != 2) console.log("DATABASE IS OUTDATED");
|
||||||
|
@ -91,9 +84,9 @@ function checkVersion() {
|
||||||
// Because ver 1 does not have user_version set, we can safely assume that it is ver 1
|
// Because ver 1 does not have user_version set, we can safely assume that it is ver 1
|
||||||
updateDatabase(1, 2);
|
updateDatabase(1, 2);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function onListening() {
|
function onListening() {
|
||||||
const addr = server.address();
|
const addr = server.address();
|
||||||
const bind = typeof addr === "string"
|
const bind = typeof addr === "string"
|
||||||
|
@ -135,37 +128,24 @@ app.use("/", adduserRouter);
|
||||||
app.use("/uploads", express.static("uploads"));
|
app.use("/uploads", express.static("uploads"));
|
||||||
|
|
||||||
async function prune () {
|
async function prune () {
|
||||||
try {
|
db.all("SELECT * FROM media", (err: Error, rows: []) => {
|
||||||
// Fetching all media entries
|
|
||||||
const rows = db.prepare("SELECT * FROM media").all() as MediaRow[];
|
|
||||||
console.log("Uploaded files: " + rows.length);
|
console.log("Uploaded files: " + rows.length);
|
||||||
console.log(rows);
|
console.log(rows);
|
||||||
|
});
|
||||||
|
|
||||||
// Vacuuming the database
|
|
||||||
console.log("Vacuuming database...");
|
console.log("Vacuuming database...");
|
||||||
db.prepare("VACUUM").run();
|
db.run("VACUUM");
|
||||||
|
|
||||||
// Deleting expired media files
|
db.each("SELECT path FROM media WHERE expire < ?", [Date.now()], (err: Error, row: MediaRow) => {
|
||||||
const expiredRows= db.prepare("SELECT path FROM media WHERE expire < ?").all(Date.now()) as MediaRow[];
|
|
||||||
|
|
||||||
for (const row of expiredRows) {
|
|
||||||
console.log(`Expired row: ${row}`);
|
console.log(`Expired row: ${row}`);
|
||||||
try {
|
fs.unlink(`uploads/${row.path}`, (err) => {
|
||||||
await unlink(`uploads/${row.path}`);
|
if (err && err.errno == -4058) {
|
||||||
} catch (err: any) {
|
|
||||||
if (err && err.code === 'ENOENT') {
|
|
||||||
console.log("File already deleted");
|
console.log("File already deleted");
|
||||||
} else {
|
|
||||||
console.error(`Failed to delete file: ${row.path}`, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
await expire("media", "expire", Date.now());
|
await expire("media", "expire", Date.now());
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Error in prune function:", err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(prune, 1000 * 60); //prune every minute
|
setInterval(prune, 1000 * 60); //prune every minute
|
|
@ -9,33 +9,30 @@ import {db, UserRow} from "../types/db";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
passport.use(new LocalStrategy(function verify(username, password, cb) {
|
passport.use(new LocalStrategy(function verify(username, password, cb) {
|
||||||
try {
|
db.get("SELECT * FROM users WHERE username = ?", [username], function(err: Error, row: UserRow) {
|
||||||
// Fetch user from database using better-sqlite3's synchronous API
|
if (err) {
|
||||||
const row = db.prepare("SELECT * FROM users WHERE username = ?").get(username) as UserRow;
|
return cb(err);
|
||||||
|
}
|
||||||
if (!row) {
|
if (!row) {
|
||||||
return cb(null, false, {
|
return cb(null, false, {
|
||||||
message: "Incorrect username or password."
|
message: "Incorrect username or password."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Synchronously hash the provided password with the stored salt
|
crypto.pbkdf2(password, row.salt, 310000, 32, "sha256", function(err, hashedPassword) {
|
||||||
const hashedPassword = crypto.pbkdf2Sync(password, row.salt, 310000, 32, "sha256");
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
if (!crypto.timingSafeEqual(row.hashed_password, hashedPassword)) {
|
||||||
return cb(null, false, {
|
return cb(null, false, {
|
||||||
message: "Incorrect username or password."
|
message: "Incorrect username or password."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return cb(null, row);
|
return cb(null, row);
|
||||||
|
});
|
||||||
} catch (err) {
|
});
|
||||||
return cb(err);
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
passport.serializeUser(function(user:User, cb) {
|
passport.serializeUser(function(user:User, cb) {
|
||||||
process.nextTick(function() {
|
process.nextTick(function() {
|
||||||
cb(null, {
|
cb(null, {
|
||||||
|
|
|
@ -17,32 +17,13 @@ import {fileStorage} from "../types/multer";
|
||||||
import {checkAuth, checkSharexAuth, convertTo720p, createEmbedData, handleUpload} from "../types/middleware";
|
import {checkAuth, checkSharexAuth, convertTo720p, createEmbedData, handleUpload} from "../types/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?
|
||||||
|
|
||||||
/**Middleware to grab media from media database */
|
/**Middleware to grab media from media database */
|
||||||
const fetchMedia: Middleware = (req, res, next) => {
|
const fetchMedia: Middleware = (req, res, next) => {
|
||||||
try {
|
|
||||||
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 */
|
/**Check if the user is an admin, if so, show all posts from all users */
|
||||||
const query: string = admin ? "SELECT * FROM media" : `SELECT * FROM media WHERE username = ?`;
|
const query: string = admin == true ? "SELECT * FROM media" : `SELECT * FROM media WHERE username = '${req.user.username}'`;
|
||||||
const rows = (admin ? db.prepare(query).all() : db.prepare(query).all(req.user.username)) as MediaRow[];
|
|
||||||
const files = rows.map((row: MediaRow) => {
|
|
||||||
return {
|
|
||||||
id: row.id,
|
|
||||||
path: row.path,
|
|
||||||
expire: row.expire,
|
|
||||||
sername: row.username,
|
|
||||||
url: "/" + row.id
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
res.locals.files = files.reverse(); //reverse so newest files appear first
|
db.all(query, (err:Error, rows: []) => {
|
||||||
res.locals.Count = files.length;
|
|
||||||
next();
|
|
||||||
} catch (err) {
|
|
||||||
next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**db.all(query, (err:Error, rows: []) => {
|
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
const files = rows.map((row: MediaRow)=> {
|
const files = rows.map((row: MediaRow)=> {
|
||||||
return {
|
return {
|
||||||
|
@ -56,7 +37,7 @@ const fetchMedia: Middleware = (req, res, next) => {
|
||||||
res.locals.files = files.reverse(); //reverse so newest files appear first
|
res.locals.files = files.reverse(); //reverse so newest files appear first
|
||||||
res.locals.Count = files.length;
|
res.locals.Count = files.length;
|
||||||
next();
|
next();
|
||||||
});**/
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
207
app/types/db.ts
207
app/types/db.ts
|
@ -1,57 +1,13 @@
|
||||||
//import sqlite3 from "sqlite3";
|
import sqlite3 from "sqlite3";
|
||||||
import mkdirp from "mkdirp";
|
import mkdirp from "mkdirp";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
|
||||||
import { Database } from "bun:sqlite";
|
|
||||||
|
|
||||||
mkdirp.sync("./uploads");
|
mkdirp.sync("./uploads");
|
||||||
mkdirp.sync("./var/db");
|
mkdirp.sync("./var/db");
|
||||||
|
|
||||||
export const db = new Database("./var/db/media.db");
|
export const db = new sqlite3.Database("./var/db/media.db");
|
||||||
|
|
||||||
/**
|
/**Create the database schema for the embedders app*/
|
||||||
* Represents a generic row structure in a database table.
|
|
||||||
*/
|
|
||||||
export interface GenericRow {
|
|
||||||
id? : number | string,
|
|
||||||
username?: string
|
|
||||||
expire? :Date
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a row structure in the media table.
|
|
||||||
*/
|
|
||||||
export interface MediaRow {
|
|
||||||
id? : number | string,
|
|
||||||
path: string,
|
|
||||||
expire: Date,
|
|
||||||
username?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A tuple representing parameters for media-related operations.
|
|
||||||
*/
|
|
||||||
export type MediaParams = [
|
|
||||||
path: string,
|
|
||||||
expire: string | number | null,
|
|
||||||
username?: string
|
|
||||||
]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a row structure in the user table.
|
|
||||||
*/
|
|
||||||
export interface UserRow {
|
|
||||||
id? : number | string,
|
|
||||||
username: string,
|
|
||||||
hashed_password: any,
|
|
||||||
salt: any,
|
|
||||||
user_version? : number
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the database tables and sets the user version pragma.
|
|
||||||
* @param {number} version - The desired user version for the database.
|
|
||||||
*/
|
|
||||||
export function createDatabase(version: number){
|
export function createDatabase(version: number){
|
||||||
console.log("Creating database");
|
console.log("Creating database");
|
||||||
|
|
||||||
|
@ -61,9 +17,7 @@ export function createDatabase(version: number){
|
||||||
hashed_password BLOB, \
|
hashed_password BLOB, \
|
||||||
expire INTEGER, \
|
expire INTEGER, \
|
||||||
salt BLOB \
|
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, \
|
id INTEGER PRIMARY KEY, \
|
||||||
|
@ -75,75 +29,58 @@ export function createDatabase(version: number){
|
||||||
db.run(`PRAGMA user_version = ${version}`);
|
db.run(`PRAGMA user_version = ${version}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**Updates old Database schema to new */
|
||||||
* Updates the database schema from an old version to a new one.
|
|
||||||
* @param {number} oldVersion - The current version of the database.
|
|
||||||
* @param {number} newVersion - The desired version to upgrade to.
|
|
||||||
*/
|
|
||||||
export function updateDatabase(oldVersion: number, newVersion: number){
|
export function updateDatabase(oldVersion: number, newVersion: number){
|
||||||
if (oldVersion == 1) {
|
if (oldVersion == 1) {
|
||||||
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
console.log(`Updating database from ${oldVersion} to ${newVersion}`);
|
||||||
db.run("PRAGMA user_version = 2");
|
db.run("PRAGMA user_version = 2", (err) => {
|
||||||
db.run("ALTER TABLE media ADD COLUMN username TEXT");
|
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");
|
db.run("ALTER TABLE users ADD COLUMN expire TEXT", (err) => {
|
||||||
|
if(err) return;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**Inserts into the media table */
|
||||||
* Inserts a new media record into the media table.
|
export function insertToDB (filename: string, expireDate: Date, username: string) {
|
||||||
* @param {string} filename - The name of the file.
|
|
||||||
* @param {string | number | null} expireDate - The expiration date for the media.
|
|
||||||
* @param {string} username - The name of the user.
|
|
||||||
*/
|
|
||||||
export function insertToDB (filename: string, expireDate: string | number | null, username: string) {
|
|
||||||
try {
|
|
||||||
const params: MediaParams = [
|
const params: MediaParams = [
|
||||||
filename,
|
filename,
|
||||||
expireDate,
|
expireDate,
|
||||||
username
|
username
|
||||||
];
|
];
|
||||||
|
|
||||||
const query = db.prepare("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)");
|
db.run("INSERT INTO media (path, expire, username) VALUES (?, ?, ?)", params, function (err) {
|
||||||
query.run(...params);
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
console.log(`Inserted ${filename} to database`)
|
return err;
|
||||||
|
}
|
||||||
|
console.log(`Uploaded ${filename} to database`);
|
||||||
if (expireDate == null)
|
if (expireDate == null)
|
||||||
console.log("It will not expire");
|
console.log("It will not expire");
|
||||||
else if (expireDate != null || expireDate != undefined)
|
else if (expireDate != null || expireDate != undefined)
|
||||||
console.log(`It will expire on ${expireDate}`);
|
console.log(`It will expire on ${expireDate}`);
|
||||||
} catch (err) {
|
});
|
||||||
console.log(err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**Searches the database and returns images with partial or exact keysearches */
|
||||||
* Searches for images in the database based on a name or keyword.
|
|
||||||
* @param {string} imagename - The name or keyword to search for.
|
|
||||||
* @param {boolean} partial - Whether to perform a partial search.
|
|
||||||
*/
|
|
||||||
export function searchImages(imagename: string, partial: boolean) {
|
export function searchImages(imagename: string, partial: boolean) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log(`searching for ${imagename}`);
|
console.log(`searching for ${imagename}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the name of an image in the database.
|
|
||||||
* @param {string} oldimagename - The current name of the image.
|
|
||||||
* @param {string} newname - The new name for the image.
|
|
||||||
*/
|
|
||||||
export function updateImageName(oldimagename: string, newname:string) {
|
export function updateImageName(oldimagename: string, newname:string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log(`updating ${oldimagename} to ${newname}`);
|
console.log(`updating ${oldimagename} to ${newname}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Adds a new user to the users table in the database.
|
/**Inserts a new user to the database */
|
||||||
* @param {string} username - The name of the user.
|
|
||||||
* @param {string} password - The user's password.
|
|
||||||
*/
|
|
||||||
export function createUser(username: string, password: string) {
|
export function createUser(username: string, password: string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log(`Creating user ${username}`);
|
console.log(`Creating user ${username}`);
|
||||||
|
@ -159,50 +96,70 @@ export function createUser(username: string, password: string) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**Selects the path for a file given ID */
|
||||||
* Retrieves the path for a file from the media table based on the given ID.
|
export function getPath(id: number | string) {
|
||||||
*
|
|
||||||
* @param {number|string} id - The ID of the record whose path needs to be fetched.
|
|
||||||
* @returns {Promise<any[]>} A promise that resolves with the selected paths.
|
|
||||||
* The resolved value is an array of records (usually containing a single item).
|
|
||||||
* @throws Will reject the promise with an error if the query fails.
|
|
||||||
*/
|
|
||||||
export function getPath(id: number | string): Promise<any[]> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
const query = "SELECT path FROM media WHERE id = ?";
|
||||||
const query = db.prepare("SELECT path FROM media WHERE id = ?");
|
|
||||||
resolve(query.all(id));
|
db.get(query, [id], (err: Error, path: object) => {
|
||||||
} catch (err) {
|
if (err) {reject(err);}
|
||||||
reject(err);
|
resolve(path);
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**Deletes from database given an ID */
|
||||||
* Deletes a row from a specified table based on its ID.
|
export function deleteId(database: string, id: number | string) {
|
||||||
*
|
return new Promise((resolve, reject) => {
|
||||||
* @param {string} database - The name of the database table.
|
const query = `DELETE FROM ${database} WHERE id = ?`;
|
||||||
* @param {number | string} id - The ID of the row to delete.
|
|
||||||
*/
|
db.run(query, [id], (err: Error) => {
|
||||||
export function deleteId(database: string, id: number | string): void {
|
if (err) {reject(err); return;}
|
||||||
const deleteQuery = `DELETE FROM ${database} WHERE id = ?`;
|
resolve(null);
|
||||||
db.prepare(deleteQuery).run(id);
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**Expires a database row given a Date in unix time */
|
||||||
* Removes rows from a table that are older than a given expiration date.
|
|
||||||
*
|
|
||||||
* @param {string} database - The name of the database table.
|
|
||||||
* @param {string} column - The column name used for date comparison.
|
|
||||||
* @param {number} expiration - The expiration date in UNIX time.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
export function expire(database: string, column: string, expiration:number) {
|
export function expire(database: string, column: string, expiration:number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const rows: GenericRow[] = db.query(`SELECT * FROM ${database} WHERE ${column} < ?`).all(expiration);
|
const query = `SELECT * FROM ${database} WHERE ${column} < ?`;
|
||||||
|
|
||||||
for (const row of rows) {
|
db.each(query, [expiration], async (err: Error, row: GenericRow) => {
|
||||||
deleteId(database, row.id);
|
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
|
||||||
|
}
|
|
@ -152,12 +152,9 @@ export const handleUpload: Middleware = (req, res, next) => {
|
||||||
return res.status(400).send("No files were uploaded.");
|
return res.status(400).send("No files were uploaded.");
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("running handleUpload")
|
|
||||||
|
|
||||||
const files = (req.files) ? req.files as Express.Multer.File[] : req.file; //Check if a single file was uploaded or multiple
|
const files = (req.files) ? req.files as Express.Multer.File[] : req.file; //Check if a single file was uploaded or multiple
|
||||||
console.log(files);
|
|
||||||
const username = (req.user) ? req.user.username : "sharex"; //if no username was provided, we can presume that it is sharex
|
const username = (req.user) ? req.user.username : "sharex"; //if no username was provided, we can presume that it is sharex
|
||||||
const expireDate: number | null = (req.body.expire) ? Date.now() + (req.body.expire * 24 * 60 * 60 * 1000) : null;
|
const expireDate: Date = (req.body.expire) ? new Date(Date.now() + (req.body.expire * 24 * 60 * 60 * 1000)) : null;
|
||||||
|
|
||||||
if (files instanceof Array) {
|
if (files instanceof Array) {
|
||||||
for (const file in files) {
|
for (const file in files) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import {Request} from "express";
|
import {Request} from "express";
|
||||||
import multer, {FileFilterCallback, MulterError} from "multer";
|
import multer, {FileFilterCallback} from "multer";
|
||||||
|
|
||||||
import {db, MediaRow} from "./db";
|
import {db, MediaRow} from "./db";
|
||||||
import {extension} from "./lib";
|
import {extension} from "./lib";
|
||||||
|
@ -22,26 +22,27 @@ export const fileStorage = multer.diskStorage({
|
||||||
): void => {
|
): void => {
|
||||||
const nameAndExtension = extension(file.originalname);
|
const nameAndExtension = extension(file.originalname);
|
||||||
console.log(`Uploading ${file}`);
|
console.log(`Uploading ${file}`);
|
||||||
try {
|
db.all("SELECT * FROM media WHERE path = ?", [nameAndExtension[0] + nameAndExtension[1]], (err: Error, exists: []) => {
|
||||||
console.log("querying")
|
if (err) {
|
||||||
const query = db.query(`SELECT * FROM media WHERE path = ?`);
|
|
||||||
const exists = query.all(nameAndExtension[0] + nameAndExtension[1]);
|
|
||||||
console.log(exists)
|
|
||||||
|
|
||||||
if (exists.length !== 0) {
|
|
||||||
const suffix = (new Date().getTime() / 1000).toString();
|
|
||||||
const newName = (request.body.title || nameAndExtension[0]) + "-" + suffix + nameAndExtension[1];
|
|
||||||
callback(null, newName);
|
|
||||||
console.log("ran callback with suffix")
|
|
||||||
} else {
|
|
||||||
const newName = (request.body.title || nameAndExtension[0]) + nameAndExtension[1];
|
|
||||||
callback(null, newName);
|
|
||||||
console.log("ran callback")
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
console.log(err);
|
console.log(err);
|
||||||
callback(err, null);
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
1
index.ts
1
index.ts
|
@ -1 +0,0 @@
|
||||||
console.log("Hello via Bun!");
|
|
|
@ -61,14 +61,11 @@
|
||||||
"@types/probe-image-size": "^7.2.0",
|
"@types/probe-image-size": "^7.2.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
||||||
"@typescript-eslint/parser": "^5.46.1",
|
"@typescript-eslint/parser": "^5.46.1",
|
||||||
"bun-types": "^1.0.2",
|
|
||||||
"copyfiles": "^2.4.1",
|
"copyfiles": "^2.4.1",
|
||||||
"eslint": "^8.28.0",
|
"eslint": "^8.28.0",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"ts-node": "^10.9.1",
|
"ts-node": "^10.9.1",
|
||||||
"ts-node-dev": "^2.0.0",
|
"ts-node-dev": "^2.0.0",
|
||||||
"typescript": "^4.9.3"
|
"typescript": "^4.9.3"
|
||||||
},
|
}
|
||||||
"module": "index.ts",
|
|
||||||
"type": "module"
|
|
||||||
}
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"types": ["bun-types"],
|
|
||||||
/* Visit https://aka.ms/tsconfig to read more about this file */
|
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||||
|
|
||||||
/* Projects */
|
/* Projects */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue