mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 09:54:17 +02:00
explicit upload errors
This commit is contained in:
parent
2055526cb0
commit
8c80182ab1
3 changed files with 7 additions and 59 deletions
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Controllers;
|
||||
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Demostf\API\Providers\UploadProvider;
|
||||
use Exception;
|
||||
use Flight;
|
||||
|
|
@ -31,18 +32,6 @@ class UploadController extends BaseController {
|
|||
}
|
||||
$demoFile = $demo['tmp_name'];
|
||||
|
||||
try {
|
||||
$result = $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile);
|
||||
if ('Invalid key' === $result) {
|
||||
Flight::response()->status(401)->write($result)->send();
|
||||
} else {
|
||||
echo $result;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Flight::response()
|
||||
->status(500)
|
||||
->write($e->getMessage())
|
||||
->send();
|
||||
}
|
||||
echo $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use Demostf\API\Demo\DemoStore;
|
|||
use Demostf\API\Demo\Header;
|
||||
use Demostf\API\Demo\HeaderParser;
|
||||
use Demostf\API\Demo\Parser;
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use RandomLib\Generator;
|
||||
|
||||
|
|
@ -57,7 +58,7 @@ class UploadProvider extends BaseProvider {
|
|||
public function upload(string $key, string $red, string $blu, string $name, string $demoFile): string {
|
||||
$user = $this->userProvider->byKey($key);
|
||||
if (!$user || ('' !== $this->uploadKey && $this->uploadKey !== $key)) {
|
||||
return 'Invalid key';
|
||||
throw new InvalidKeyException('Invalid key');
|
||||
}
|
||||
|
||||
if (!mb_check_encoding($red, 'UTF-8')) {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ use Demostf\API\Demo\Header;
|
|||
use Demostf\API\Demo\HeaderParser;
|
||||
use Demostf\API\Demo\Parser;
|
||||
use Demostf\API\Demo\RawParser;
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
use Demostf\API\Providers\KillProvider;
|
||||
|
|
@ -214,10 +215,8 @@ class UploadProviderTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testUploadInvalidKey() {
|
||||
$this->assertEquals(
|
||||
'Invalid key',
|
||||
$this->uploadProvider->upload('dummy', 'RED', 'BLU', 'dummy', 'dummy')
|
||||
);
|
||||
$this->expectException(InvalidKeyException::class);
|
||||
$this->uploadProvider->upload('dummy', 'RED', 'BLU', 'dummy', 'dummy');
|
||||
}
|
||||
|
||||
public function testUploadNonDemo() {
|
||||
|
|
@ -316,45 +315,4 @@ class UploadProviderTest extends TestCase {
|
|||
$this->assertEquals(0, $demo->getBlueScore());
|
||||
$this->assertEquals(3, $demo->getRedScore());
|
||||
}
|
||||
|
||||
public function testUploadKey() {
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Not an HL2 demo');
|
||||
|
||||
$uploadProvider = new UploadProvider(
|
||||
$this->getDatabaseConnection(),
|
||||
'http://example.com',
|
||||
$this->headerParser,
|
||||
$this->parser,
|
||||
$this->demoStore,
|
||||
$this->userProvider,
|
||||
$this->demoProvider,
|
||||
$this->demoSaver,
|
||||
'foo'
|
||||
);
|
||||
|
||||
$steamId = $this->getSteamId('123', 'a');
|
||||
$token = $this->userProvider->store($steamId);
|
||||
|
||||
$this->assertEquals(
|
||||
'Invalid key',
|
||||
$uploadProvider->upload($token, 'RED', 'BLU', 'dummy', 'dummy')
|
||||
);
|
||||
|
||||
$uploadProvider = new UploadProvider(
|
||||
$this->getDatabaseConnection(),
|
||||
'http://example.com',
|
||||
$this->headerParser,
|
||||
$this->parser,
|
||||
$this->demoStore,
|
||||
$this->userProvider,
|
||||
$this->demoProvider,
|
||||
$this->demoSaver,
|
||||
$token
|
||||
);
|
||||
|
||||
file_put_contents($this->tmpDir . '/foo.dem', 'asd');
|
||||
|
||||
$uploadProvider->upload($token, 'RED', 'BLU', 'dummy', $this->tmpDir . '/foo.dem');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue