Skip to content

Commit

Permalink
feat: allow to configure clamav with environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed May 9, 2023
1 parent 2dc0fc9 commit 1df5c71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions .prettierignore
@@ -0,0 +1 @@
/backend/src/constants.ts
19 changes: 10 additions & 9 deletions backend/src/clamscan/clamscan.service.ts
@@ -1,21 +1,22 @@
import { Injectable } from "@nestjs/common";
import { Injectable, Logger } from "@nestjs/common";
import * as NodeClam from "clamscan";
import * as fs from "fs";
import { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service";
import { SHARE_DIRECTORY } from "../constants";
import { CLAMAV_HOST, CLAMAV_PORT, SHARE_DIRECTORY } from "../constants";

const clamscanConfig = {
clamdscan: {
host: process.env.NODE_ENV == "docker" ? "clamav" : "127.0.0.1",
port: 3310,
host: CLAMAV_HOST,
port: CLAMAV_PORT,
localFallback: false,
},
preference: "clamdscan",
};

@Injectable()
export class ClamScanService {
private readonly logger = new Logger(ClamScanService.name);

constructor(
private fileService: FileService,
private prisma: PrismaService
Expand All @@ -24,11 +25,11 @@ export class ClamScanService {
private ClamScan: Promise<NodeClam | null> = new NodeClam()
.init(clamscanConfig)
.then((res) => {
console.log("ClamAV is active");
this.logger.log("ClamAV is active");
return res;
})
.catch(() => {
console.log("ClamAV is not active");
this.logger.log("ClamAV is not active");
return null;
});

Expand All @@ -47,7 +48,7 @@ export class ClamScanService {
const { isInfected } = await clamScan
.isInfected(`${SHARE_DIRECTORY}/${shareId}/${fileId}`)
.catch(() => {
console.log("ClamAV is not active");
this.logger.log("ClamAV is not active");
return { isInfected: false };
});

Expand Down Expand Up @@ -79,7 +80,7 @@ export class ClamScanService {
},
});

console.log(
this.logger.warn(
`Share ${shareId} deleted because it contained ${infectedFiles.length} malicious file(s)`
);
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/constants.ts
@@ -1,3 +1,5 @@
export const DATA_DIRECTORY = process.env.DATA_DIRECTORY || "./data";
export const SHARE_DIRECTORY = `${DATA_DIRECTORY}/uploads/shares`
export const DATABASE_URL = process.env.DATABASE_URL || "file:../data/pingvin-share.db?connection_limit=1";
export const DATABASE_URL = process.env.DATABASE_URL || "file:../data/pingvin-share.db?connection_limit=1";
export const CLAMAV_HOST = process.env.CLAMAV_HOST || (process.env.NODE_ENV == "docker" ? "clamav" : "127.0.0.1");
export const CLAMAV_PORT = parseInt(process.env.CLAMAV_PORT) || 3310;

0 comments on commit 1df5c71

Please sign in to comment.