Skip to content

Commit

Permalink
πŸ› Fix config retrieval and type handling (#739)
Browse files Browse the repository at this point in the history
πŸ› Fix config retrieval and type handling
  • Loading branch information
MontaGhanmy authored Nov 22, 2024
1 parent c1856ca commit f12f089
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
12 changes: 6 additions & 6 deletions tdrive/backend/node/src/services/av/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class AVServiceImpl implements TdriveServiceProvider, Initializable {
version: "1";
av: NodeClam = null;
logger: TdriveLogger = getLogger("Antivirus Service");
avEnabled = getConfigOrDefault("drive.featureAntivirus", false);
private MAX_FILE_SIZE = getConfigOrDefault("av.maxFileSize", 26214400); // 25 MB
avEnabled: boolean = getConfigOrDefault<boolean>("drive.featureAntivirus", false);
private MAX_FILE_SIZE: number = getConfigOrDefault<number>("av.maxFileSize", 26214400); // 25 MB

async init(): Promise<this> {
try {
Expand All @@ -23,11 +23,11 @@ export class AVServiceImpl implements TdriveServiceProvider, Initializable {
removeInfected: false, // Do not remove infected files
quarantineInfected: false, // Do not quarantine, just alert
scanLog: null, // No log file for this test
debugMode: getConfigOrDefault("av.debugMode", false), // Enable debug messages
debugMode: getConfigOrDefault<boolean>("av.debugMode", false), // Enable debug messages
clamdscan: {
host: getConfigOrDefault("av.host", "localhost"), // IP of the server
port: getConfigOrDefault("av.port", 3310) as number, // ClamAV server port
timeout: getConfigOrDefault("av.timeout", 2000), // Timeout for scans
host: getConfigOrDefault<string>("av.host", "localhost"), // IP of the server
port: getConfigOrDefault<number>("av.port", 3310) as number, // ClamAV server port
timeout: getConfigOrDefault<number>("av.timeout", 2000), // Timeout for scans
localFallback: true, // Use local clamscan if needed
},
});
Expand Down
6 changes: 3 additions & 3 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export class DocumentsService {
userRepository: Repository<User>;
ROOT: RootType = "root";
TRASH: TrashType = "trash";
quotaEnabled: boolean = getConfigOrDefault("drive.featureUserQuota", false);
defaultQuota: number = getConfigOrDefault("drive.defaultUserQuota", 0);
manageAccessEnabled: boolean = getConfigOrDefault("drive.featureManageAccess", false);
quotaEnabled: boolean = getConfigOrDefault<boolean>("drive.featureUserQuota", false);
defaultQuota: number = getConfigOrDefault<number>("drive.defaultUserQuota", 0);
manageAccessEnabled: boolean = getConfigOrDefault<boolean>("drive.featureManageAccess", false);
logger: TdriveLogger = getLogger("Documents Service");

async init(): Promise<this> {
Expand Down
2 changes: 1 addition & 1 deletion tdrive/backend/node/src/services/global-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class GlobalResolver {
};

// AV service is optional
if (getConfigOrDefault("drive.featureAntivirus", false))
if (getConfigOrDefault<boolean>("drive.featureAntivirus", false))
this.services.av = await new AVServiceImpl().init();

Object.keys(this.services).forEach((key: keyof TdriveServices) => {
Expand Down
11 changes: 9 additions & 2 deletions tdrive/backend/node/src/utils/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import config from "config";

export const getConfigOrDefault = (key: string, defaultValue: any) => {
return config.has(key) ? config.get(key) : defaultValue;
export const getConfigOrDefault = <T>(key: string, defaultValue: T): T => {
const value = config.has(key) ? config.get(key) : defaultValue;

// Handle specific cases for boolean
if (typeof defaultValue === "boolean") {
return (value === "true" || value === true) as T;
}

return value as T;
};

0 comments on commit f12f089

Please sign in to comment.