MCPcopy Create free account
hub / github.com/GDATASoftwareAG/vaas / forFileAsync

Method forFileAsync

php/src/vaas/Vaas.php:139–189  ·  view source on GitHub ↗

* Get the verdict for a File * @param string $path Path to the file * @param ForFileOptions|null $options Options for the request * @param Cancellation|null $cancellation Cancellation token * @return Future A future that resolves to a VaasVerdict * @throws VaasClientException The request is malformed or cannot be completed. Recommended actions: Don't repeat the request. Lo

(string $path, ?ForFileOptions $options = null, ?Cancellation $cancellation = null)

Source from the content-addressed store, hash-verified

137 * @throws VaasServerException The server encountered an internal error. Recommended actions: You may retry the request after a certain delay. If the problem persists contact G DATA.
138 */
139 public function forFileAsync(string $path, ?ForFileOptions $options = null, ?Cancellation $cancellation = null): Future
140 {
141 return async(function () use ($path, $options, $cancellation) {
142 $this->logger->debug("Requesting verdict for file: $path");
143 $filesystem = filesystem($options->filesystemDriver);
144
145 if (!$filesystem->exists($path)) {
146 $this->logger->error("File does not exist: $path");
147 throw new VaasClientException('File does not exist');
148 }
149
150 $options = $options ?? ForFileOptions::fromVaasOptions($this->options);
151
152 if ($options->useCache || $options->useHashLookup) {
153 $forSha256Options = new ForSha256Options(
154 $options->useCache, $options->useHashLookup, $options->vaasRequestId);
155 $sha256 = Sha256::TryFromFile($path)->await();
156 $this->logger->debug("Check if file $path is already known by its SHA256: $sha256");
157 try {
158 $response = $this->forSha256Async($sha256, $forSha256Options, $cancellation)->await();
159 $isVerdictWithoutDetection = ($response->verdict === 'Malicious' || $response->verdict === 'Pup') && !empty($response->detection);
160 if ($response->verdict !== 'Unknown' && !empty($response->fileType) && !empty($response->mimeType) && !$isVerdictWithoutDetection) {
161 $this->logger->debug("File $path is already known from cache or G DATA cloud by its SHA256: $sha256");
162 return $response;
163 }
164 }
165 catch (Exception $ex) {
166 $this->logger->debug("Something went wrong while checking the SHA256 of file $path: " . $ex->getMessage());
167 }
168 }
169
170 try {
171 $stream = $filesystem->openFile($path, 'r');
172 $cancellation?->subscribe(function () use ($stream) { $stream->close(); });
173 $forStreamOptions = new ForStreamOptions($options->useHashLookup, $options->timeout, $options->vaasRequestId);
174 $this->logger->debug("Requesting verdict for $path as file stream");
175 return $this->forStreamAsync($stream, filesize($path), $forStreamOptions)->await();
176 } catch (FilesystemException $ex) {
177 $this->logger->error("Error opening file: $path");
178 throw new VaasClientException('Error opening file (' . $path . '): ' . $ex->getMessage());
179 } catch (Exception $ex){
180 $this->logger->error("Error requesting verdict for file '$path': " . $ex->getMessage());
181 throw $ex;
182 }
183 finally {
184 if (isset($stream)) {
185 $stream->close();
186 }
187 }
188 });
189 }
190
191 /**
192 * Get the verdict for a stream

Callers

nothing calls this directly

Calls 6

forSha256AsyncMethod · 0.95
forStreamAsyncMethod · 0.95
TryFromFileMethod · 0.80
openFileMethod · 0.80
fromVaasOptionsMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected