Add files via upload

This commit is contained in:
Kosztyk 2025-12-04 09:50:19 +02:00 committed by GitHub
parent 718f87ec31
commit 179abac88d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 69 additions and 3 deletions

45
src/pages/antivirus.tsx Normal file
View file

@ -0,0 +1,45 @@
// src/pages/antivirus.tsx
import { Elysia, t } from "elysia";
import { userService } from "./user";
import {
isAntivirusAvailable,
isAntivirusEnabled,
setAntivirusEnabled,
} from "../helpers/avToggle";
export const antivirus = new Elysia()
.use(userService)
// Get current AV status
.get("/api/antivirus", () => {
const available = isAntivirusAvailable();
const enabled = isAntivirusEnabled();
return { available, enabled };
})
// Update AV status
.post(
"/api/antivirus",
({ body }) => {
const { enabled } = body;
if (!isAntivirusAvailable()) {
// CLAMAV_URL missing: force disabled and report unavailable
return {
available: false,
enabled: false,
};
}
setAntivirusEnabled(Boolean(enabled));
return {
available: true,
enabled: isAntivirusEnabled(),
};
},
{
body: t.Object({
enabled: t.Boolean(),
}),
},
);

View file

@ -1,9 +1,12 @@
// src/pages/upload.tsx
import { Elysia, t } from "elysia";
import db from "../db/db";
import { WEBROOT, CLAMAV_URL } from "../helpers/env";
import { uploadsDir } from "../index";
import { userService } from "./user";
import sanitize from "sanitize-filename";
import { isAntivirusEnabled } from "../helpers/avToggle";
type ClamAvResultItem = {
name: string;
@ -23,8 +26,22 @@ type ClamAvResponse = {
* Returns { infected: boolean, viruses: string[] } and logs everything.
*/
async function scanFileWithClamAV(file: any, fileName: string) {
// 🔀 Respect toggle + CLAMAV_URL availability
if (!isAntivirusEnabled()) {
console.log(
"[ClamAV] Antivirus disabled (toggle off or CLAMAV_URL unset). Skipping scan for",
fileName,
);
return {
infected: false,
viruses: [] as string[],
};
}
if (!CLAMAV_URL) {
console.error("[ClamAV] CLAMAV_URL is not configured, skipping scan.");
console.error(
"[ClamAV] CLAMAV_URL is not configured, but antivirus was considered enabled. Skipping scan.",
);
return {
infected: false,
viruses: [] as string[],
@ -161,7 +178,12 @@ export const upload = new Elysia()
for (const file of files) {
const originalName = (file as any).name ?? "upload";
const sanitizedFileName = sanitize(originalName) || "file";
console.log("[Upload] Handling file:", originalName, "=> sanitized:", sanitizedFileName);
console.log(
"[Upload] Handling file:",
originalName,
"=> sanitized:",
sanitizedFileName,
);
// 1) Scan with ClamAV REST API (use original name just for logging / AV metadata)
const scan = await scanFileWithClamAV(file, originalName);
@ -216,4 +238,3 @@ export const upload = new Elysia()
auth: true,
},
);