* Get the verdict for a stream * @param ReadableStream $stream The stream to check * @param int $fileSize The size of the file in bytes * @param ForStreamOptions|null $options Options for the request * @param Cancellation|null $cancellation Cancellation token * @return Future A future that resolves to a VaasVerdict * @throws HttpException If the request fails * @
(ReadableStream $stream, int $fileSize, ?ForStreamOptions $options = null, ?Cancellation $cancellation = null)
| 201 | * @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. |
| 202 | */ |
| 203 | public function forStreamAsync(ReadableStream $stream, int $fileSize, ?ForStreamOptions $options = null, ?Cancellation $cancellation = null): Future |
| 204 | { |
| 205 | return async(function () use ($stream, $fileSize, $options, $cancellation) { |
| 206 | try { |
| 207 | $this->logger->debug("Requesting verdict for stream"); |
| 208 | |
| 209 | if (!$stream->isReadable() || $stream->isClosed()) { |
| 210 | $this->logger->error("Stream is not readable"); |
| 211 | throw new VaasClientException('Stream is not readable'); |
| 212 | } |
| 213 | |
| 214 | $options = $options ?? ForStreamOptions::fromVaasOptions($this->options); |
| 215 | |
| 216 | $url = sprintf('%s/files?useHashLookup=%s', $this->options->vaasUrl, json_encode($options->useHashLookup)); |
| 217 | |
| 218 | $request = new Request($url, 'POST'); |
| 219 | |
| 220 | $request->setBody(StreamedContent::fromStream($stream, $fileSize)); |
| 221 | $this->enrichRequestAsync($request, $options->timeout, $options->vaasRequestId)->await(); |
| 222 | $this->logger->debug("Send request for file stream: " . self::logUri($request)); |
| 223 | $response = $this->httpClient->request($request, $cancellation); |
| 224 | } finally { |
| 225 | $stream->close(); |
| 226 | } |
| 227 | switch ($response->getStatus()) { |
| 228 | case 201: |
| 229 | try { |
| 230 | $fileAnalysisStarted = json_decode($response->getBody()->buffer(), true); |
| 231 | } catch (Exception $ex) { |
| 232 | $this->logger->error("Error parsing response for stream: " . $ex->getMessage()); |
| 233 | throw new VaasServerException('Error parsing response stream', $ex->getCode(), $ex); |
| 234 | } |
| 235 | $this->logger->debug("File uploaded successfully and analysis started"); |
| 236 | break; |
| 237 | case 400: |
| 238 | $this->logger->error("Bad request for stream"); |
| 239 | throw new VaasClientException("Bad request. The header content-length is missing or the content-type is not \"application/octet-stream\"."); |
| 240 | case 401: |
| 241 | $this->logger->error("Unauthorized request for stream"); |
| 242 | throw new VaasAuthenticationException("Unauthorized. Check your credentials."); |
| 243 | case 403: |
| 244 | $this->logger->error("Forbidden request for stream"); |
| 245 | throw new VaasClientException("Forbidden. You are not allowed to use this endpoint."); |
| 246 | default: |
| 247 | $this->logger->error("Error requesting verdict for stream"); |
| 248 | throw $this->parseVaasError($response); |
| 249 | } |
| 250 | |
| 251 | $forSha256Options = new ForSha256Options(true, $options->useHashLookup, $options->vaasRequestId); |
| 252 | |
| 253 | if (!isset($fileAnalysisStarted['sha256'])) { |
| 254 | $this->logger->error("Unexpected response from the server for stream"); |
| 255 | throw new VaasServerException('Unexpected response from the server'); |
| 256 | } |
| 257 | $sha256 = Sha256::TryFromString($fileAnalysisStarted['sha256'])->await(); |
| 258 | |
| 259 | $this->logger->debug("Requesting verdict for uploaded file with SHA256: $sha256"); |
| 260 | return $this->forSha256Async($sha256, $forSha256Options)->await(); |
no test coverage detected