@ -1,44 +0,0 @@
|
||||
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}`;
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
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}`);
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import bcrypt from "bcrypt";
|
||||
import { randomString } from "./stringHelpers";
|
||||
|
||||
/**
|
||||
* Generates a random salt for a password
|
||||
@ -7,7 +6,7 @@ import { randomString } from "./stringHelpers";
|
||||
* @return The random salt
|
||||
*/
|
||||
export function generateSalt() {
|
||||
return bcrypt.genSaltSync(10);
|
||||
return randomString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -16,7 +15,7 @@ export function generateSalt() {
|
||||
* @return The password
|
||||
*/
|
||||
export function generateRandomPassword() {
|
||||
return randomString(16);
|
||||
return randomString(8);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,10 +32,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(password, hashedPassword) {
|
||||
return bcrypt.compareSync(password, hashedPassword);
|
||||
export function isValidPassword(salt, password) {
|
||||
return bcrypt.compareSync(password, salt);
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
* @param {Number} length
|
||||
* @returns The random string
|
||||
*/
|
||||
export function randomString(length) {
|
||||
function randomString(length) {
|
||||
var result = "";
|
||||
var characters =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
@ -1,55 +1,10 @@
|
||||
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 username
|
||||
* Returns the user with the given email address
|
||||
*
|
||||
* @param {string} username The users username
|
||||
* @return The user object in mongo or null if not found
|
||||
* @param {string} email The users email address
|
||||
* @return The users object in mongo or null if not found
|
||||
*/
|
||||
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;
|
||||
export async function getUser(email) {
|
||||
const user = await UserModel.find({ email: email });
|
||||
return user;
|
||||
}
|
||||
|
Reference in New Issue
Block a user