1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-04 02:14:06 +02:00

use url based parser

This commit is contained in:
Robin Appelman 2018-09-13 23:32:27 +02:00
commit f3d07823df
5 changed files with 82 additions and 5 deletions

View file

@ -4,8 +4,10 @@ declare(strict_types=1);
namespace Demostf\API\Demo;
use Demostf\API\Controllers\TempController;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use function GuzzleHttp\Psr7\stream_for;
/**
* Wrapper around demo.js parser.
@ -16,27 +18,38 @@ class RawParser {
/** @var string */
private $parserUrl;
public function __construct(string $parserUrl) {
private $tempController;
public function __construct(string $parserUrl, TempController $tempController) {
$this->parserUrl = $parserUrl;
$this->tempController = $tempController;
}
public function parse(string $path): ?array {
$key = md5($path);
$url = $this->tempController->register($key, $path);
try {
$client = new Client();
echo($url);
$response = $client->post($this->parserUrl, [
'body' => fopen($path, 'r'),
'body' => stream_for($url),
'headers' => [
'Content-Type' => 'application/octet-stream'
]
]);
$result = json_decode($response->getBody()->getContents(), true);
$this->tempController->unregister($key);
if (null === $result) {
throw new \Exception('Failed to parse demo, unexpected result from parser');
} else {
return $result;
}
} catch (RequestException $e) {
$this->tempController->unregister($key);
if (strpos($e->getMessage(), 'cURL error 52') !== false) {
throw new \Exception('Failed to parse demo, can\'t reach demo parser');
}
throw new \Exception('Failed to parse demo, ' . $e->getMessage());
throw new \Exception('Failed to parse demo, ' . $e->getMessage() . ' ' . $url);
}
}
}