Scanning uploaded files for viruses
private async Task<UploadedDocument> ScanFile(HttpPostedFileBase fileToScan, string modelProperty)
{
WebServiceClient MtoaApi = CreateMtoaApi();
var target = new MemoryStream();
var fileBytes = target.ToArray();
fileToScan.InputStream.CopyTo(target);
string fileExtension = Path.GetExtension(fileToScan.FileName);
bool isInfected = true;
try
{
isInfected = await MtoaApi.VirusScanApi.IsInfected(fileToScan.FileName, fileBytes);
}
catch (Exception ex)
{
throw ex;
}
// Add error when threat exists
if (isInfected)
{
ModelState.AddModelError(modelProperty, ValidationErrors.FileScanError + $" ({fileToScan.FileName})");
return null;
}
return new UploadedDocument {
Name = fileToScan.FileName,
ContentType = fileToScan.ContentType,
Data = fileBytes,
FileNameWithPath = fileToScan.FileName,
FileSize = fileBytes.Length
};
}