Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates, fixes and just some code tidy #96

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/engine/impl/FileEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion src/model/db/FileUpload.model.ts
Original file line number Diff line number Diff line change
@@ -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"], {
Expand Down Expand Up @@ -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}`);
}
}
5 changes: 5 additions & 0 deletions src/public/secure/stats.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<%- include('../snippets/head.ejs'); %>
<link href="/assets/custom/css/index.css" rel="stylesheet">
<title>Statistics</title>
<style>
.am5-modal-curtain,.am5-modal-wrapper{
background: #1515150f !important;
}
</style>
</head>
<body>
<div class="container mt-4">
Expand Down
4 changes: 2 additions & 2 deletions src/services/EncryptionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]);
Expand Down
2 changes: 1 addition & 1 deletion src/services/FileCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 3 additions & 6 deletions src/services/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down Expand Up @@ -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);
Expand Down