mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
use url based parser
This commit is contained in:
parent
e61c5fc249
commit
f3d07823df
5 changed files with 82 additions and 5 deletions
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API;
|
||||
|
||||
use Demostf\API\Controllers\TempController;
|
||||
use Demostf\API\Demo\DemoSaver;
|
||||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Demo\HeaderParser;
|
||||
|
|
@ -90,12 +91,16 @@ class Container {
|
|||
return new PlayerProvider($this->connection);
|
||||
}
|
||||
|
||||
public function getRawParser(): RawParser {
|
||||
return new RawParser($this->getParserUrl(), new TempController($this->getApiRoot() . '/temp/'));
|
||||
}
|
||||
|
||||
public function getUploadProvider(): UploadProvider {
|
||||
return new UploadProvider(
|
||||
$this->connection,
|
||||
$this->baseUrl,
|
||||
new HeaderParser(),
|
||||
new Parser(new RawParser($this->parserUrl)),
|
||||
new Parser($this->getRawParser()),
|
||||
$this->getDemoStore(),
|
||||
$this->getUserProvider(),
|
||||
$this->getDemoProvider(),
|
||||
|
|
|
|||
53
src/Controllers/TempController.php
Normal file
53
src/Controllers/TempController.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Demostf\API\Controllers;
|
||||
|
||||
|
||||
class TempController extends BaseController {
|
||||
private $webRoot;
|
||||
|
||||
public function __construct(string $webRoot) {
|
||||
$this->webRoot = $webRoot;
|
||||
}
|
||||
|
||||
public function register(string $key, string $path) : string {
|
||||
apcu_store($key, $path);
|
||||
return $this->webRoot . $key;
|
||||
}
|
||||
|
||||
public function unregister(string $key) {
|
||||
apcu_dec($key);
|
||||
}
|
||||
|
||||
public function serve(string $key) {
|
||||
$path = apcu_fetch($key);
|
||||
if ($path) {
|
||||
$handle = fopen($path, 'r');
|
||||
fpassthru($handle);
|
||||
fclose($handle);
|
||||
} else {
|
||||
\Flight::response()
|
||||
->status(404)
|
||||
->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ $userController = new Controllers\UserController($container->getRequest(), $cont
|
|||
$container->getUserProvider());
|
||||
$infoController = new Controllers\InfoController($container->getRequest(), $container->getResponse(),
|
||||
$container->getInfoProvider());
|
||||
$tempController = new Controllers\TempController($container->getApiRoot() . '/temp/');
|
||||
|
||||
Flight::route('/*', function () {
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
|
|
@ -66,4 +67,6 @@ Flight::route('/auth/handle/@token', [$authController, 'handle']);
|
|||
Flight::route('/auth/login/@token', [$authController, 'login']);
|
||||
Flight::route('/auth/logout/@token', [$authController, 'logout']);
|
||||
|
||||
Flight::route('/temp/@hash', [$tempController, 'serve']);
|
||||
|
||||
Flight::start();
|
||||
|
|
|
|||
|
|
@ -9,10 +9,13 @@ use Flight;
|
|||
/** @var Container $container */
|
||||
$container = require __DIR__ . '/init.php';
|
||||
|
||||
$tempController = new Controllers\TempController($container->getApiRoot() . '/temp/');
|
||||
|
||||
$uploadController = new Controllers\UploadController(
|
||||
$container->getRequest(),
|
||||
$container->getResponse(),
|
||||
$container->getUploadProvider()
|
||||
$container->getUploadProvider(),
|
||||
$tempController
|
||||
);
|
||||
|
||||
Flight::route('/*', function () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue