From 47eb07314b4c46a6bfc5dd08bde69e30acf1cdb5 Mon Sep 17 00:00:00 2001 From: VictoriqueMoe Date: Sat, 9 Mar 2024 15:25:02 +0000 Subject: [PATCH] updates, fixes and just some code tidy --- src/engine/impl/FileEngine.ts | 2 +- src/model/db/FileUpload.model.ts | 11 ++++++++++- src/public/secure/stats.ejs | 5 +++++ src/services/EncryptionService.ts | 4 ++-- src/services/FileCleaner.ts | 2 +- src/services/FileService.ts | 9 +++------ 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/engine/impl/FileEngine.ts b/src/engine/impl/FileEngine.ts index 75d6f0a..15eecde 100644 --- a/src/engine/impl/FileEngine.ts +++ b/src/engine/impl/FileEngine.ts @@ -21,7 +21,7 @@ export class FileEngine { public getFilePath(file: string | PlatformMulterFile | FileUploadModel): string { if (file instanceof FileUploadModel) { - return `${filesDir}/${file.fullFileNameOnSystem}`; + return file.fullLocationOnDisk; } return typeof file === "string" ? `${filesDir}/${file}` : file.path; } diff --git a/src/model/db/FileUpload.model.ts b/src/model/db/FileUpload.model.ts index 2428898..398650a 100644 --- a/src/model/db/FileUpload.model.ts +++ b/src/model/db/FileUpload.model.ts @@ -1,7 +1,8 @@ import { Column, Entity, Index } from "typeorm"; import { AbstractModel } from "./AbstractModel.js"; -import { FileUtils } from "../../utils/Utils.js"; +import { filesDir, FileUtils } from "../../utils/Utils.js"; import type { EntrySettings, ProtectionLevel } from "../../utils/typeings.js"; +import path from "node:path"; @Entity() @Index(["token"], { @@ -117,4 +118,12 @@ export class FileUploadModel extends AbstractModel { } return this.fileName; } + + /** + * Get the full absolute location on disk + * @returns {string} + */ + public get fullLocationOnDisk(): string { + return path.resolve(`${filesDir}/${this.fullFileNameOnSystem}`); + } } diff --git a/src/public/secure/stats.ejs b/src/public/secure/stats.ejs index ce54728..cc864fd 100644 --- a/src/public/secure/stats.ejs +++ b/src/public/secure/stats.ejs @@ -4,6 +4,11 @@ <%- include('../snippets/head.ejs'); %> Statistics +
diff --git a/src/services/EncryptionService.ts b/src/services/EncryptionService.ts index a9b085a..efe5059 100644 --- a/src/services/EncryptionService.ts +++ b/src/services/EncryptionService.ts @@ -13,7 +13,7 @@ import { promisify } from "node:util"; export class EncryptionService implements OnInit { private readonly algorithm = "aes-256-ctr"; - private readonly promisifyRandomBytes = promisify(crypto.randomBytes); + private readonly randomBytes = promisify(crypto.randomBytes); @Constant(GlobalEnv.SALT) private readonly salt: string | undefined; @@ -34,7 +34,7 @@ export class EncryptionService implements OnInit { } const fileSource = this.fileEngine.getFilePath(Path.basename(filePath)); const buffer = await fs.readFile(fileSource); - const iv = await this.promisifyRandomBytes(16); + const iv = await this.randomBytes(16); const key = await this.getKey(password); const cipher = crypto.createCipheriv(this.algorithm, key, iv); const encryptedBuffer = Buffer.concat([iv, cipher.update(buffer), cipher.final()]); diff --git a/src/services/FileCleaner.ts b/src/services/FileCleaner.ts index 046ff32..61fca01 100644 --- a/src/services/FileCleaner.ts +++ b/src/services/FileCleaner.ts @@ -17,7 +17,7 @@ export class FileCleaner implements OnInit { @Inject() private fileEngine: FileEngine, ) {} - // default to every day at 12am + // default to every hour at :00 @Constant(GlobalEnv.FILE_CLEANER_CRON, "0 * * * *") private readonly cronToRun: string; diff --git a/src/services/FileService.ts b/src/services/FileService.ts index 6e94061..0708c9c 100644 --- a/src/services/FileService.ts +++ b/src/services/FileService.ts @@ -163,14 +163,11 @@ export class FileService { requestedFileName?: string, password?: string, ): Promise<[Buffer, FileUploadModel]> { - const entry = await this.repo.getEntryFileName(path.parse(fileNameOnSystem).name); + const entry = await this.repo.getEntryFileName(fileNameOnSystem); const resource = requestedFileName ?? fileNameOnSystem; - if (entry === null || (requestedFileName && entry.originalFileName !== requestedFileName)) { + if (entry === null || (requestedFileName && entry.originalFileName !== requestedFileName) || entry.hasExpired) { throw new NotFound(`resource ${resource} is not found`); } - if (entry.hasExpired) { - throw new NotFound(`Resource ${resource} is not found`); - } return Promise.all([this.encryptionService.decrypt(entry, password), entry]); } @@ -221,7 +218,7 @@ export class FileService { this.fileEngine.deleteFile(entry.fullFileNameOnSystem, true); return Promise.reject("Entry does not exist"); } - this.fileEngine.deleteFile(entry.fullFileNameOnSystem, true); + return this.fileEngine.deleteFile(entry.fullFileNameOnSystem, true); }); try { await Promise.all(fileDeletePArr);