Skip to content

Instantly share code, notes, and snippets.

@shinkathe
Last active December 1, 2022 13:44
Show Gist options
  • Save shinkathe/e71333081c3a766cb75b8e9ecd1c97d7 to your computer and use it in GitHub Desktop.
Save shinkathe/e71333081c3a766cb75b8e9ecd1c97d7 to your computer and use it in GitHub Desktop.
// ##### RUN AV ON FILE #####
log.LogInformation("Download completed. Connecting to AV server {ClamAVServerUrl}:{ClamAVServerPort}... ", ClamAVServerUrl, ClamAVServerPort);
var clam = new ClamClient(ClamAVServerUrl, ClamAVServerPort);
bool isConnected = await clam.PingAsync();
var version = await clam.GetVersionAsync();
if (!isConnected) throw new Exception("AV server connection could not be established.");
log.LogInformation("Connection ok: {IsConnected}. AV server reports version: {ServerVersion}", isConnected, version);
var scanResult = await clam.SendAndScanFileAsync(ms.ToArray());
switch (scanResult.Result)
{
case ClamScanResults.Clean:
log.LogInformation("The file is clean!");
// You could save the file to storage here, or send event grid events for a success or perform HTTP requests accordingly
break;
case ClamScanResults.VirusDetected:
log.LogCritical("Virus found. Deleting file.");
await blobClient.DeleteIfExistsAsync(Azure.Storage.Blobs.Models.DeleteSnapshotsOption.IncludeSnapshots);
// You might also notify other systems here about the faulty file
break;
case ClamScanResults.Error:
default:
log.LogInformation("File scan error occurred. {ScanResult}", scanResult.RawResult);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment