mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
add api to set demo url
This commit is contained in:
parent
b588c8ce7e
commit
64b0aff075
6 changed files with 87 additions and 4 deletions
|
|
@ -27,6 +27,7 @@ class Container {
|
||||||
private $storeRoot;
|
private $storeRoot;
|
||||||
private $storeUrl;
|
private $storeUrl;
|
||||||
private $apiRoot;
|
private $apiRoot;
|
||||||
|
private $editKey;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Connection $connection,
|
Connection $connection,
|
||||||
|
|
@ -35,7 +36,8 @@ class Container {
|
||||||
string $parserUrl,
|
string $parserUrl,
|
||||||
string $storeRoot,
|
string $storeRoot,
|
||||||
string $storeUrl,
|
string $storeUrl,
|
||||||
string $apiRoot
|
string $apiRoot,
|
||||||
|
string $editKey
|
||||||
) {
|
) {
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
$this->generator = $generator;
|
$this->generator = $generator;
|
||||||
|
|
@ -44,6 +46,7 @@ class Container {
|
||||||
$this->storeRoot = $storeRoot;
|
$this->storeRoot = $storeRoot;
|
||||||
$this->storeUrl = $storeUrl;
|
$this->storeUrl = $storeUrl;
|
||||||
$this->apiRoot = $apiRoot;
|
$this->apiRoot = $apiRoot;
|
||||||
|
$this->editKey = $editKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAuthProvider(): AuthProvider {
|
public function getAuthProvider(): AuthProvider {
|
||||||
|
|
@ -116,4 +119,8 @@ class Container {
|
||||||
public function getApiRoot(): string {
|
public function getApiRoot(): string {
|
||||||
return $this->apiRoot;
|
return $this->apiRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getEditKey(): string {
|
||||||
|
return $this->editKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,13 @@ class DemoController extends BaseController {
|
||||||
|
|
||||||
private $demoListProvider;
|
private $demoListProvider;
|
||||||
|
|
||||||
public function __construct(DemoProvider $demoProvider, ChatProvider $chatProvider, DemoListProvider $demoListProvider) {
|
private $editKey;
|
||||||
|
|
||||||
|
public function __construct(DemoProvider $demoProvider, ChatProvider $chatProvider, DemoListProvider $demoListProvider, string $editKey) {
|
||||||
$this->demoProvider = $demoProvider;
|
$this->demoProvider = $demoProvider;
|
||||||
$this->chatProvider = $chatProvider;
|
$this->chatProvider = $chatProvider;
|
||||||
$this->demoListProvider = $demoListProvider;
|
$this->demoListProvider = $demoListProvider;
|
||||||
|
$this->editKey = $editKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -77,4 +80,23 @@ class DemoController extends BaseController {
|
||||||
public function chat($demoId) {
|
public function chat($demoId) {
|
||||||
\Flight::json($this->chatProvider->getChat($demoId));
|
\Flight::json($this->chatProvider->getChat($demoId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setDemoUrl($id) {
|
||||||
|
$hash = $this->query('hash', '');
|
||||||
|
$backend = $this->query('backend', '');
|
||||||
|
$path = $this->query('path', '');
|
||||||
|
$url = $this->query('url', '');
|
||||||
|
$editKey = $this->query('key', '');
|
||||||
|
if ($editKey !== $this->editKey) {
|
||||||
|
throw new \InvalidArgumentException('Invalid key');
|
||||||
|
}
|
||||||
|
|
||||||
|
$demo = $this->demoProvider->get($id);
|
||||||
|
$existingHash = $demo->getHash();
|
||||||
|
if ($existingHash === '' || $existingHash === $hash) {
|
||||||
|
$this->demoProvider->setDemoUrl($id, $backend, $url, $path);
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException('Invalid demo hash');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,4 +81,14 @@ class DemoProvider extends BaseProvider {
|
||||||
->execute();
|
->execute();
|
||||||
return (int)$this->connection->lastInsertId();
|
return (int)$this->connection->lastInsertId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setDemoUrl(int $id, string $backend, string $url, string $path) {
|
||||||
|
$query = $this->getQueryBuilder();
|
||||||
|
$query->update('demos')
|
||||||
|
->set('backend', $query->createNamedParameter($backend))
|
||||||
|
->set('url', $query->createNamedParameter($url))
|
||||||
|
->set('path', $query->createNamedParameter($path))
|
||||||
|
->where($query->expr()->eq('id', $query->createNamedParameter($id, \PDO::PARAM_INT)))
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,8 @@ $container = require __DIR__ . '/init.php';
|
||||||
$demoController = new Controllers\DemoController(
|
$demoController = new Controllers\DemoController(
|
||||||
$container->getDemoProvider(),
|
$container->getDemoProvider(),
|
||||||
$container->getChatProvider(),
|
$container->getChatProvider(),
|
||||||
$container->getDemoListProvider()
|
$container->getDemoListProvider(),
|
||||||
|
$container->getEditKey()
|
||||||
);
|
);
|
||||||
$authController = new Controllers\AuthController(
|
$authController = new Controllers\AuthController(
|
||||||
$container->getUserProvider(),
|
$container->getUserProvider(),
|
||||||
|
|
@ -43,6 +44,7 @@ Flight::route('/demos/@id', [$demoController, 'get']);
|
||||||
Flight::route('/demos/@id/chat', [$demoController, 'chat']);
|
Flight::route('/demos/@id/chat', [$demoController, 'chat']);
|
||||||
Flight::route('/profiles/@steamid', [$demoController, 'listProfile']);
|
Flight::route('/profiles/@steamid', [$demoController, 'listProfile']);
|
||||||
Flight::route('/uploads/@steamid', [$demoController, 'listUploads']);
|
Flight::route('/uploads/@steamid', [$demoController, 'listUploads']);
|
||||||
|
Flight::route('/demos/@id/url', [$demoController, 'setDemoUrl']);
|
||||||
|
|
||||||
Flight::route('/users/search', [$userController, 'search']);
|
Flight::route('/users/search', [$userController, 'search']);
|
||||||
Flight::route('/users/@steamid', [$userController, 'get']);
|
Flight::route('/users/@steamid', [$userController, 'get']);
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ $storeRoot = getenv('DEMO_ROOT') ?: '';
|
||||||
$storeHost = getenv('DEMO_HOST') ?: '';
|
$storeHost = getenv('DEMO_HOST') ?: '';
|
||||||
$parserUrl = getenv('PARSER_URL') ?: '';
|
$parserUrl = getenv('PARSER_URL') ?: '';
|
||||||
$appRoot = getenv('APP_ROOT') ?: '';
|
$appRoot = getenv('APP_ROOT') ?: '';
|
||||||
|
$editKey = getenv('EDIT_SECRET') ?: '';
|
||||||
|
|
||||||
$factory = new \RandomLib\Factory;
|
$factory = new \RandomLib\Factory;
|
||||||
$generator = $factory->getMediumStrengthGenerator();
|
$generator = $factory->getMediumStrengthGenerator();
|
||||||
|
|
@ -36,7 +37,8 @@ $container = new Container(
|
||||||
$parserUrl,
|
$parserUrl,
|
||||||
$storeRoot,
|
$storeRoot,
|
||||||
$storeHost,
|
$storeHost,
|
||||||
$appRoot
|
$appRoot,
|
||||||
|
$editKey
|
||||||
);
|
);
|
||||||
|
|
||||||
return $container;
|
return $container;
|
||||||
|
|
|
||||||
|
|
@ -156,4 +156,44 @@ class DemoProviderTest extends TestCase {
|
||||||
$kill = new Kill(0, $demoId, $attackerId, $assisterId, $victimId, $weapon);
|
$kill = new Kill(0, $demoId, $attackerId, $assisterId, $victimId, $weapon);
|
||||||
return $this->killProvider->store($kill);
|
return $this->killProvider->store($kill);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetDemoUrl() {
|
||||||
|
$uploaderSteamId = $this->getSteamId('12345', 'test');
|
||||||
|
$this->userProvider->store($uploaderSteamId);
|
||||||
|
|
||||||
|
$uploader = $this->userProvider->get($uploaderSteamId->getSteamId64());
|
||||||
|
|
||||||
|
$demo = new Demo(
|
||||||
|
0,
|
||||||
|
'http://example.com',
|
||||||
|
'name',
|
||||||
|
'server',
|
||||||
|
12,
|
||||||
|
'nick',
|
||||||
|
'map',
|
||||||
|
new \DateTime(),
|
||||||
|
'RED',
|
||||||
|
'BLUE',
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
18,
|
||||||
|
$uploader->getId(),
|
||||||
|
'hash',
|
||||||
|
'dummy',
|
||||||
|
'path'
|
||||||
|
);
|
||||||
|
|
||||||
|
$id = $this->provider->storeDemo($demo, 'dummy', 'path');
|
||||||
|
$id2 = $this->provider->storeDemo($demo, 'dummy', 'path');
|
||||||
|
|
||||||
|
$this->provider->setDemoUrl($id, 'foobackend', 'http://foo.example.com', 'bar');
|
||||||
|
|
||||||
|
$storedDemo = $this->provider->get($id);
|
||||||
|
$this->assertEquals('http://foo.example.com', $storedDemo->getUrl());
|
||||||
|
$this->assertEquals('foobackend', $storedDemo->getBackend());
|
||||||
|
$this->assertEquals('bar', $storedDemo->getPath());
|
||||||
|
|
||||||
|
$storedDemo2 = $this->provider->get($id2);
|
||||||
|
$this->assertEquals('http://example.com', $storedDemo2->getUrl());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue