/** * Scan file in synchron mode. * Note: this method call will block your thread until the file scan finishes. * * @param inputStream input stream to scan * @param fileScanOptions optional file scan options * @param pollingInterval polling time in millis * @param timeout timeout in millis * @return FileScanResult * @throws MetadefenderClientException */ public Responses.FileScanResult ScanFileSync(Stream inputStream, FileScanOptions fileScanOptions, int initialPollingInterval, int maximumPollingInterval, int timeout) { int pollingInterval = initialPollingInterval; string data_id = ScanFile(inputStream, fileScanOptions); var t = Task <Responses.FileScanResult> .Run(() => { Responses.FileScanResult fileScanResult; do { fileScanResult = FetchScanResult(data_id); if (!fileScanResult.IsScanFinished()) { Thread.Sleep(pollingInterval); pollingInterval = Math.Min(pollingInterval * 2, maximumPollingInterval); } }while (!fileScanResult.IsScanFinished()); return(fileScanResult); }); Task.WhenAny(t, Task.Delay(timeout)); if (t.Wait(timeout)) { return(t.Result); } else { throw new TimeoutException(); } }
/** * Scan file in synchron mode. * Note: this method call will block your thread until the file scan finishes. * * @param inputStream input stream to scan * @param fileScanOptions optional file scan options * @param pollingInterval polling time in millis * @param timeout timeout in millis * @return FileScanResult * @throws MetadefenderClientException */ public Responses.FileScanResult ScanFileSync(Stream inputStream, FileScanOptions fileScanOptions, int initialPollingInterval, int maximumPollingInterval, int timeout, out string core_id) { // 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 = this.apiEndPointUrl; int pollingInterval = initialPollingInterval; string data_id = ScanFile(inputStream, fileScanOptions, out CoreId, inputStream.Length); var t = Task <Responses.FileScanResult> .Run(() => { Responses.FileScanResult fileScanResult; do { fileScanResult = FetchScanResult(data_id); if (!fileScanResult.IsScanFinished()) { Thread.Sleep(pollingInterval); pollingInterval = Math.Min(pollingInterval * 2, maximumPollingInterval); } }while (!fileScanResult.IsScanFinished()); return(fileScanResult); }); Task.WhenAny(t, Task.Delay(timeout)); if (t.Wait(timeout)) { core_id = CoreId; return(t.Result); } else { throw new TimeoutException(); } }
/** * Scan is done asynchronously and each scan request is tracked by data id of which result can be retrieved by API Fetch Scan Result. * * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_scan_file.html" target="_blank">REST API doc</a> * * @param inputStream Stream of data (file) to scan. Required * @param fileScanOptions Optional file scan options. Can be NULL. * @return unique data id for this file scan */ public string ScanFile(Stream inputStream, FileScanOptions fileScanOptions, out string coreId, long contentLength = 0) { //if (inputStream == null) //{ // throw new MetadefenderClientException("Stream cannot be null"); //} if ((fileScanOptions != null && fileScanOptions.GetUserAgent() == null) && user_agent != null) { fileScanOptions.SetUserAgent(user_agent); } if (fileScanOptions == null && user_agent != null) { fileScanOptions = new FileScanOptions().SetUserAgent(user_agent); } Dictionary <string, string> headers = (fileScanOptions == null) ? null : fileScanOptions.GetOptions(); cookieContainer = new System.Net.CookieContainer(); HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/file", "POST", inputStream, headers, contentLength, cookieContainer); coreId = apiEndPointUrl; if (response.responseCode == 200) { try { Responses.ScanRequest scanRequest = JsonConvert.DeserializeObject <Responses.ScanRequest>(response.response); if (scanRequest != null) { if (cookieContainer.Count > 0) { foreach (System.Net.Cookie cookie in response.cookieContainer.GetCookies(new Uri(apiEndPointUrl))) { coreId = cookie.Value; } } return(scanRequest.data_id); } else { ThrowRequestError(response); return(null); } } catch (JsonSerializationException) { ThrowRequestError(response); return(null); } } else { ThrowRequestError(response); return(null); } }
/** * Scan is done asynchronously and each scan request is tracked by data id of which result can be retrieved by API Fetch Scan Result. * * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_scan_file.html" target="_blank">REST API doc</a> * * @param inputStream Stream of data (file) to scan. Required * @param fileScanOptions Optional file scan options. Can be NULL. * @return unique data id for this file scan */ public string ScanFile(Stream inputStream, FileScanOptions fileScanOptions) { if (inputStream == null) { throw new MetadefenderClientException("Stream cannot be null"); } if ((fileScanOptions != null && fileScanOptions.GetUserAgent() == null) && user_agent != null) { fileScanOptions.SetUserAgent(user_agent); } if (fileScanOptions == null && user_agent != null) { fileScanOptions = new FileScanOptions().SetUserAgent(user_agent); } Dictionary <string, string> headers = (fileScanOptions == null) ? null : fileScanOptions.GetOptions(); HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/file", "POST", inputStream, headers); if (response.responseCode == 200) { try { Responses.ScanRequest scanRequest = JsonConvert.DeserializeObject <Responses.ScanRequest>(response.response); if (scanRequest != null) { return(scanRequest.data_id); } else { ThrowRequestError(response); return(null); } } catch (JsonSerializationException) { ThrowRequestError(response); return(null); } } else { ThrowRequestError(response); return(null); } }