Skip to content

Commit

Permalink
chore: refactor ResourceTokenManager to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
zunderscore committed Dec 26, 2024
1 parent 8b13242 commit 5fec4f6
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function buildModules(scriptManifest) {
frontendCommunicator: require("../../frontend-communicator"),
counterManager: require("../../../counters/counter-manager"),
utils: require("../../../utility"),
resourceTokenManager: require("../../../resourceTokenManager"),
resourceTokenManager: require("../../../resource-token-manager").ResourceTokenManager,

notificationManager: {
addNotification: (notificationBase, permanentlySave = true) => {
Expand Down
8 changes: 4 additions & 4 deletions src/backend/common/handlers/mediaProcessor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { SettingsManager } = require("../settings-manager");
const resourceTokenManager = require("../../resourceTokenManager.js");
const { ResourceTokenManager } = require("../../resource-token-manager");
const util = require("../../utility");
const logger = require("../../logwrapper");
const webServer = require("../../../server/http-server-manager");
Expand Down Expand Up @@ -47,7 +47,7 @@ function soundProcessor(effect) {
data.audioOutputDevice = selectedOutputDevice;

if (selectedOutputDevice.deviceId === "overlay") {
const resourceToken = resourceTokenManager.storeResourcePath(effect.file, 30);
const resourceToken = ResourceTokenManager.storeResourcePath(effect.file, 30);
data.resourceToken = resourceToken;
}

Expand Down Expand Up @@ -107,7 +107,7 @@ async function imageProcessor(effect, trigger) {
}

if (effect.imageType === "local") {
const resourceToken = resourceTokenManager.storeResourcePath(
const resourceToken = ResourceTokenManager.storeResourcePath(
effect.file,
effect.length
);
Expand Down Expand Up @@ -158,7 +158,7 @@ function videoProcessor(effect) {
}
}

const resourceToken = resourceTokenManager.storeResourcePath(
const resourceToken = ResourceTokenManager.storeResourcePath(
effect.file,
effect.length
);
Expand Down
4 changes: 2 additions & 2 deletions src/backend/effects/builtin/play-sound.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { SettingsManager } = require("../../common/settings-manager");
const resourceTokenManager = require("../../resourceTokenManager");
const { ResourceTokenManager } = require("../../resource-token-manager");
const webServer = require("../../../server/http-server-manager");
const fs = require('fs/promises');
const logger = require("../../logwrapper");
Expand Down Expand Up @@ -140,7 +140,7 @@ const playSound = {
// Generate token if going to overlay, otherwise send to gui.
if (selectedOutputDevice.deviceId === "overlay") {
if (effect.soundType !== "url") {
const resourceToken = resourceTokenManager.storeResourcePath(
const resourceToken = ResourceTokenManager.storeResourcePath(
data.filepath,
30
);
Expand Down
4 changes: 2 additions & 2 deletions src/backend/effects/builtin/play-video.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { SettingsManager } = require("../../common/settings-manager");
const resourceTokenManager = require("../../resourceTokenManager");
const { ResourceTokenManager } = require("../../resource-token-manager");
const webServer = require("../../../server/http-server-manager");
const mediaProcessor = require("../../common/handlers/mediaProcessor");
const { EffectCategory } = require('../../../shared/effect-constants');
Expand Down Expand Up @@ -544,7 +544,7 @@ const playVideo = {
if (!isNaN(result)) {
duration = result;
}
resourceToken = resourceTokenManager.storeResourcePath(data.filepath, duration);
resourceToken = ResourceTokenManager.storeResourcePath(data.filepath, duration);
}
if ((data.videoDuration == null || data.videoDuration === "" || data.videoDuration === 0) && duration != null) {
data.videoDuration = duration;
Expand Down
6 changes: 3 additions & 3 deletions src/backend/effects/builtin/show-image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { SettingsManager } = require("../../common/settings-manager");
const resourceTokenManager = require("../../resourceTokenManager");
const { ResourceTokenManager } = require("../../resource-token-manager");
const mediaProcessor = require("../../common/handlers/mediaProcessor");
const webServer = require("../../../server/http-server-manager");
const fs = require("fs/promises");
Expand Down Expand Up @@ -200,7 +200,7 @@ const showImage = {
}

if (effect.imageType === "local") {
const resourceToken = resourceTokenManager.storeResourcePath(effect.file, effect.length);
const resourceToken = ResourceTokenManager.storeResourcePath(effect.file, effect.length);
data.resourceToken = resourceToken;
}

Expand All @@ -218,7 +218,7 @@ const showImage = {

const fullFilePath = path.join(effect.folder, chosenFile);

const resourceToken = resourceTokenManager.storeResourcePath(fullFilePath, effect.length);
const resourceToken = ResourceTokenManager.storeResourcePath(fullFilePath, effect.length);

data.resourceToken = resourceToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { getPathInTmpDir } = require("../../../common/data-access");
const { SettingsManager } = require("../../../common/settings-manager");
const resourceTokenManager = require("../../../resourceTokenManager");
const { ResourceTokenManager } = require("../../../resource-token-manager");
const webServer = require("../../../../server/http-server-manager");
const { v4: uuid } = require("uuid");
const fs = require('fs');
Expand Down Expand Up @@ -597,7 +597,7 @@ const playSound = {

// Generate token if going to overlay, otherwise send to gui.
if (selectedOutputDevice.deviceId === "overlay") {
const resourceToken = resourceTokenManager.storeResourcePath(
const resourceToken = ResourceTokenManager.storeResourcePath(
data.filepath,
30
);
Expand Down
50 changes: 50 additions & 0 deletions src/backend/resource-token-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { v4 as uuid } from "uuid";
import logger from "./logwrapper";

interface ResourceToken {
path: string;
length: number;
}

class ResourceTokenManager {
tokens: Record<string, ResourceToken> = {};

private deleteToken(token: string) {
logger.debug(`Deleting token: ${token}`);
if (this.tokens[token] !== undefined) {
delete this.tokens[token];
}
}

getResourcePath(token: string) {
const resource = this.tokens[token];

// delete the token if we actually had something saved.
// delay for the given length before deletion to allow multiple requests at once and loading.
if (resource != null) {
setTimeout((t) => {
this.deleteToken(t);
}, resource.length, token);
return resource.path;
}
return null;
}

storeResourcePath(path: string, length: string | number) {
let tokenLength = 5;

if (typeof length === "string" && length != null && length !== "") {
tokenLength = parseFloat(length);
} else if (typeof length === "number" && length != null && !isNaN(length)) {
tokenLength = length;
}

const token = uuid();
this.tokens[token] = { path: path, length: tokenLength * 1000 };
return token;
}
}

const resourceTokenManager = new ResourceTokenManager();

export { resourceTokenManager as ResourceTokenManager };
54 changes: 0 additions & 54 deletions src/backend/resourceTokenManager.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/server/http-server-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path from 'path';
import logger from "../backend/logwrapper";
import { SettingsManager } from "../backend/common/settings-manager";
import effectManager from "../backend/effects/effectManager";
import resourceTokenManager from "../backend/resourceTokenManager";
import { ResourceTokenManager } from "../backend/resource-token-manager";
import websocketServerManager from "./websocket-server-manager";
import { CustomWebSocketHandler } from "../types/websocket";

Expand Down Expand Up @@ -136,7 +136,7 @@ class HttpServerManager extends EventEmitter {
app.get("/resource/:token", function(req, res) {
const token = req.params.token || null;
if (token !== null) {
let resourcePath = resourceTokenManager.getResourcePath(token) || null;
let resourcePath = ResourceTokenManager.getResourcePath(token) || null;
if (resourcePath !== null) {
resourcePath = resourcePath.replace(/\\/g, "/");
res.sendFile(resourcePath);
Expand Down

0 comments on commit 5fec4f6

Please sign in to comment.