/** * Scan a file via VirusTotal * @param string $filename The filename to scan, provide an absolute filename * @param boolean $rescan Wether or not to force a rescan * @return array Thee information returned by VirusTotal. */ public static function scanFile($filename, $rescan = false) { // The api key must be set! $apikey = config("virus-total-api.api_key"); if (!$apikey) { throw new \Exception("Please setup your VirusTotal api key to use the api.", 1); } // Prepare the file scanner $filescan = new \VirusTotal\File($apikey); // Try/Catch to get rate limit exceptions thrown by the api wrapper try { // Force rescan if needed. if ($rescan) { $scanResults = $filescan->rescan($filename); } else { $scanResults = $filescan->getReport($filename); if ($scanResults['response_code'] == 0) { $scanResults = $filescan->scan($filename); } } } catch (\Exception $e) { if ($e instanceof \VirusTotal\Exceptions\RateLimitException) { return ['success' => false, 'error' => 'rate limit exceeded']; } else { throw new \Exception("Please setup a valid VirusTotal api key to use the api.", 1); } } return array_merge(['success' => true], $scanResults); }
/** * Scans file for viruses. * * @param $data * @internal param FileScanRequest $request * @return array|bool|float|int|string */ private function fileScan($data) { $file = $data->file('file'); //$file_to_scan = $file->getRealPath(); //$file_size_mb = filesize($file_to_scan) / 1024 / 1024; $scan = new \VirusTotal\File(getenv('VIRUSTOTAL_API_KEY')); $resp = $scan->scan($file->getRealPath()); //$resp = $scan->rescan($file->getRealPath()); if (isset($resp['resource'])) { $resp['Resource']['result'] = $scan->getReport($resp['resource']); } return $resp; }