Skip to content

Commit

Permalink
Keep rate-change beatmap files for 5 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebola3461 committed Dec 28, 2023
1 parent 8143f3d commit 5525ebc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
6 changes: 5 additions & 1 deletion models/core/AxerBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { UserEventsListener } from "./UserEventsListener";
import { handleMapperTrackerUserEvent } from "../../modules/tracking/mapperTracker";
import { RemindersManager } from "../../modules/reminders/remindersChecker";
import { AxerBancho } from "../../modules/bancho/client";

import { RateChangeDeletionManager } from "../../modules/osu/ratechanger/RateChangeDeletionManager";
import "../../modules/osu/fetcher/startConnection";
import "../../modules/automation/start";

Expand All @@ -20,6 +20,7 @@ export class AxerBot extends Client {
public UserEvents = new UserEventsListener();
public Reminders = new RemindersManager(this);
public Bancho = new AxerBancho(this);
public RateChangeDeletionManager = new RateChangeDeletionManager();

constructor(options: ClientOptions) {
super(options);
Expand All @@ -33,6 +34,7 @@ export class AxerBot extends Client {
this.Discussions.events.on.bind(this.Discussions);
this.UserEvents.listen.bind(this.UserEvents);
this.UserEvents.events.on.bind(this.UserEvents);
this.RateChangeDeletionManager.listen.bind(this.RateChangeDeletionManager);

this.login(process.env.TOKEN).then(() => {
this.Bancho.connect().catch(console.error);
Expand All @@ -41,6 +43,8 @@ export class AxerBot extends Client {
startAvatarListener(this);
this.Reminders.start();

this.RateChangeDeletionManager.listen();

this.Discussions.listen();
this.Discussions.events.on("any", handleDiscussionEvent);

Expand Down
14 changes: 13 additions & 1 deletion modules/osu/ratechanger/BeatmapRateChanger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { bufferToStream } from "../../../helpers/transform/bufferToStream";
import { BeatmapEncoder, HoldableObject, SpinnableObject } from "osu-parsers";
import Ffmpeg from "fluent-ffmpeg";
import archiver from "archiver";
import { bot } from "../../..";

export interface BeatmapRateChangerOptions {
scaleOd?: boolean;
Expand Down Expand Up @@ -43,7 +44,14 @@ export class BeatmapRateChanger {
this.options = options;
}

private addToDeletionQueue() {
console.log("test");
bot.RateChangeDeletionManager.addToQueue(this.fileHash, new Date());
}

generate() {
this.addToDeletionQueue.bind(this);

return new Promise<string>((resolve, reject) => {
const encoder = new BeatmapEncoder();

Expand All @@ -64,7 +72,11 @@ export class BeatmapRateChanger {

encoder
.encodeToPath(path.join(this.tempPath, output), this.beatmap)
.then(() => resolve(this.fileHash));
.then(() => {
this.addToDeletionQueue();

resolve(this.fileHash);
});
})
.catch(reject);
});
Expand Down
117 changes: 117 additions & 0 deletions modules/osu/ratechanger/RateChangeDeletionManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// This should delete rate change files after 5 minutes

import { existsSync, readFileSync, rmSync, unlinkSync, writeFileSync } from "fs";
import path from "path";
import { LoggerClient } from "../../../models/core/LoggerClient";

export interface RateChangeTempFile {
info: string;
format: string;
duration: number;
files: RateChangeTempFileEntry[];
}

export interface RateChangeTempFileEntry {
fileId: string;
createdAt: string;
}

export class RateChangeDeletionManager {
private tempFilePath = path.resolve(path.join("./temp/ratechange/temp.json"));
private baseTempPath = path.resolve("./temp/ratechange");
private logger = new LoggerClient("RateChangeDeletionManager");

constructor() {}

private tempFileBase = {
info: "Files here will be deleted after the time scheduled below",
format: "{ fileId: string, createdAt: Date }",
duration: 300,
files: [],
};

private checkAndCreateTempFile() {
if (!existsSync(this.tempFilePath)) {
writeFileSync(this.tempFilePath, JSON.stringify(this.tempFileBase));

this.logger.printSuccess("temp.json created!");
}
}

public addToQueue(fileId: string, createdAt?: Date) {
const fileContent: RateChangeTempFile = JSON.parse(readFileSync(this.tempFilePath, "utf8"));

if (fileContent.files.find((file) => file.fileId == fileId)) return;

fileContent.files.push({
fileId: fileId.trim(),
createdAt: (createdAt || new Date()).toISOString(),
});

writeFileSync(this.tempFilePath, JSON.stringify(fileContent));

this.logger.printSuccess(`Added file ${fileId} to deletion queue!`);
}

public removeFromQueue(fileId: string) {
const fileContent: RateChangeTempFile = JSON.parse(readFileSync(this.tempFilePath, "utf8"));

if (!fileContent.files.find((file) => file.fileId == fileId)) return;

fileContent.files = fileContent.files.filter((file) => file.fileId != fileId);

writeFileSync(this.tempFilePath, JSON.stringify(fileContent));

this.logger.printSuccess(`Removed file ${fileId} from deletion queue!`);
}

private getMaxFileAge() {
const fileContent: RateChangeTempFile = JSON.parse(readFileSync(this.tempFilePath, "utf8"));

return fileContent.duration;
}

private checkTimeDifference(from: Date, to: Date) {
return (to.getTime() - from.getTime()) / 1000;
}

private canDelete(date: Date, maxAge: number) {
return this.checkTimeDifference(date, new Date()) > maxAge;
}

private checkQueue() {
const fileContent: RateChangeTempFile = JSON.parse(readFileSync(this.tempFilePath, "utf8"));
const entries = fileContent.files;

const filesToDelete = entries.filter((entry) =>
this.canDelete(new Date(entry.createdAt), fileContent.duration)
);

for (const file of filesToDelete) {
try {
this.logger.printInfo(`Removing ${file.fileId}...`);

rmSync(path.join(this.baseTempPath, file.fileId), { recursive: true });
rmSync(path.join(this.baseTempPath, "osz", file.fileId), { recursive: true });

this.removeFromQueue(file.fileId);

this.logger.printSuccess(`${file.fileId} removed!`);
} catch (e: any) {
if (e.code == "ENOENT") {
this.removeFromQueue(file.fileId);
} else {
this.logger.printError(`${file.fileId} can't be removed!`, e);
}
}
}
}

public listen() {
this.logger.printInfo("Starting listener...");

this.checkAndCreateTempFile();

setInterval(this.checkQueue.bind(this), 1000);
}
}

0 comments on commit 5525ebc

Please sign in to comment.