Revert "Revert "Add file model""

This reverts commit 97733da8fb.
This commit is contained in:
Liam
2022-11-13 22:45:51 +00:00
parent 97733da8fb
commit 162235a1d3
12 changed files with 314 additions and 23 deletions

View File

@ -0,0 +1,44 @@
import path from "path";
import { FILE_STORAGE_LOCATION } from "../../consts/filePaths";
import FileModel from "../../models/FileModel";
import { createFileIO } from "./ioHelpers";
import { connectMongo } from "./mongoHelpers";
import { randomString } from "./stringHelpers";
connectMongo();
/**
* Returns the the files object in mongo for the given id
*
* @param {string} fileId The files id
* @return The file object or null if not found
*/
export async function getFile(fileId) {
return await FileModel.findOne({ fileId: fileId });
}
/**
* Creates the file object in mongo and stores it to the storage location
*
* @param {UserModel} uploader The user who uploaded the file
* @param {[]} fileData The file data for the upload
*/
export async function createFile(uploader, fileName, buffer, contentType) {
const fileId = randomString(process.env.FILE_ID_LENGTH);
const extention = fileName.split(".")[1].toLowerCase();
// Todo: Check if the file was actually saved to
// disk and create a return type so we can notify the user what happened
await createFileIO(
`${FILE_STORAGE_LOCATION}${path.sep}${uploader.uploadKey}`,
`${fileId}.${extention}`,
buffer
);
const file = await FileModel.create({
uploader: uploader._id,
fileId: fileId,
uploadDate: new Date(),
contentType: contentType,
});
await file.save();
return `${fileId}.${extention}`;
}

View File

@ -0,0 +1,64 @@
import fs from "fs";
import NodeCache from "node-cache";
import path from "path";
const existsCache = new NodeCache({
stdTTL: 300, // 5 minutes
});
/**
* Checks if the given file/directory exists
*
* @param {string} path The path to the file/directory
* @returns If the file/directory exists
*/
export function exists(path) {
if (existsCache.has(path)) {
return existsCache.get(path);
}
const exists = fs.existsSync(path);
existsCache.set(path, exists);
return exists;
}
/**
* Creates a file in the given directory
*
* @param {string} path The path to the file
* @param {Buffer} bytes The bytes of the file
*/
export function createFileIO(dir, fileName, bytes) {
if (!exists(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const fileLocation = dir + path.sep + fileName;
console.log(fileLocation);
fs.writeFile(
fileLocation,
bytes,
{
encoding: "utf-8",
},
(err) => {
console.log(err);
}
);
}
/**
* Creates a file in the given directory
*
* @param {string} path The path to the file
* @return The file
*/
export function readFileIO(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
});
}

View File

@ -0,0 +1,9 @@
import mongoose from "mongoose";
export async function connectMongo() {
try {
await mongoose.connect(process.env.MONGODB_CONNECTION_STRING);
} catch (e) {
console.log(`Mongo connection failed: ${e.message}`);
}
}

View File

@ -1,4 +1,5 @@
import bcrypt from "bcrypt";
import { randomString } from "./stringHelpers";
/**
* Generates a random salt for a password
@ -6,7 +7,7 @@ import bcrypt from "bcrypt";
* @return The random salt
*/
export function generateSalt() {
return randomString(16);
return bcrypt.genSaltSync(10);
}
/**
@ -15,7 +16,7 @@ export function generateSalt() {
* @return The password
*/
export function generateRandomPassword() {
return randomString(8);
return randomString(16);
}
/**
@ -32,10 +33,10 @@ export function hashPassword(salt, password) {
/**
* Checks if the password is valid with the salt
*
* @param {string} salt The salt
* @param {string} password The password that the user gave
* @param {string} hashedPassword The password in the users account
* @return If the password is valid or not
*/
export function isValidPassword(salt, password) {
return bcrypt.compareSync(password, salt);
export function isValidPassword(password, hashedPassword) {
return bcrypt.compareSync(password, hashedPassword);
}

View File

@ -4,7 +4,7 @@
* @param {Number} length
* @returns The random string
*/
function randomString(length) {
export function randomString(length) {
var result = "";
var characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

View File

@ -1,10 +1,55 @@
import UserModel from "../../models/UserModel";
import { connectMongo } from "./mongoHelpers";
import { generateSalt, hashPassword } from "./passwordHelpers";
import { randomString } from "./stringHelpers";
connectMongo();
/**
* Returns the user with the given email address
* Returns the user with the given username
*
* @param {string} email The users email address
* @return The users object in mongo or null if not found
* @param {string} username The users username
* @return The user object in mongo or null if not found
*/
export async function getUser(email) {
const user = await UserModel.find({ email: email });
return user;
export async function getUser(username) {
return await UserModel.findOne({ username: username });
}
/**
* Returns the user with the given upload key
*
* @param {string} uploadKey The users uploadKey
* @return The user object in mongo or null if not found
*/
export async function getUserByUploadKey(uploadKey) {
return await UserModel.findOne({ uploadKey: uploadKey });
}
/**
* Creates a new user and returns the user object
*
* @param {string} username The username of the account
* @param {string} password The non hashed password of the account
*
* @return null if user already exists, true if success, false if fail
*/
export async function createUser(username, password) {
let user = await getUser(username);
if (user !== null) {
return null;
}
try {
const salt = generateSalt();
user = await UserModel.create({
username: username,
password: hashPassword(salt, password),
salt: salt,
uploadKey: randomString(16),
});
user.save();
} catch (e) {
return false;
}
return true;
}