Revert "Add file model"

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

View File

@ -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}`;
}