private static void ScanFile(string apiUrl, string file, string rule = "") { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(apiUrl); try { // The Core ID is usefull when using external LB // It actually points the cookie value that identify the core server the LB sent the file to string CoreId = apiUrl; Stream inputStream = File.Open(file, FileMode.Open); FileScanOptions fso = new FileScanOptions(); fso.SetFileName(GetFileNameFromPath(file)); if (!string.IsNullOrEmpty(rule)) { fso.SetRule(rule); } string dataId = metadefenderCoreClient.ScanFile(inputStream, fso, out CoreId, inputStream.Length); Console.WriteLine("File scan started. The data id is: " + dataId); if (CoreId.ToLower() != apiUrl.ToLower()) { Console.WriteLine("Core Id: {0}", CoreId); } } catch (MetadefenderClientException e) { Console.WriteLine("Error during file scan: " + e.GetDetailedMessage()); } catch (FileNotFoundException e) { Console.WriteLine("File not found: " + file + " Exception: " + e.Message); } }
private static void ScanFileSync(string apiUrl, string file, string rule = "") { int TaskTimeout = 600 * 1000; // 10 minutes MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(apiUrl); // The Core ID is usefull when using external LB // It actually points the cookie value that identify the core server the LB sent the file to string CoreId = apiUrl; try { Stream inputStream = File.Open(file, FileMode.Open); FileScanOptions fso = new FileScanOptions(); fso.SetFileName(GetFileNameFromPath(file)); if (!string.IsNullOrEmpty(rule)) { fso.SetRule(rule); } FileScanResult result = metadefenderCoreClient.ScanFileSync(inputStream, fso, 200, 1000 * 30, TaskTimeout, out CoreId); Console.WriteLine("File scan finished with result: " + result.process_info.result); if (CoreId.ToLower() != apiUrl.ToLower()) { Console.WriteLine("Core Id: {0}", CoreId); } if (result.process_info.post_processing != null) { if (!string.IsNullOrEmpty(result.process_info.post_processing.actions_ran)) { Console.WriteLine("post processing: " + result.process_info.post_processing.actions_ran); } if (result.process_info.post_processing.actions_ran.ToLower().Contains("sanitized")) { Console.WriteLine("\nFile was sanitized. Where would you like to save the sanitized file?"); Console.Write("Enter folder path: "); string destinationPath = Console.ReadLine(); if (!Directory.Exists(destinationPath)) { throw new Exception(string.Format("Destination folder {0} does not exist!", destinationPath)); } string cdrErrorMessage = null; Stream sanitizedStream = metadefenderCoreClient.DownloadSanitizedFile(result.data_id, out cdrErrorMessage); if (string.IsNullOrEmpty(cdrErrorMessage)) { string sanitizedFilename = result.process_info.post_processing.converted_destination; // save the file on disk using (var fileStream = File.Create(Path.Combine(destinationPath, sanitizedFilename))) { sanitizedStream.Seek(0, SeekOrigin.Begin); sanitizedStream.CopyTo(fileStream, 81920); } } else { Console.WriteLine("Failed to download sanitized file! Reason: {0}", cdrErrorMessage); } } } } catch (MetadefenderClientException e) { Console.WriteLine("Error during file scan: " + e.GetDetailedMessage()); } catch (FileNotFoundException e) { Console.WriteLine("File not found: " + file + " Exception: " + e.Message); } catch (Exception e) { Console.WriteLine("General error: " + file + " Exception: " + e.Message); } }