ZH
apps/winzik / src / Core / Market.php · commit 6576d6f
<?phpnamespace App\Core;use JumPHP\Async\Task;use JumPHP\Http\Http;use JumPHP\Http\HttpResponse;/** * The marketplace of WinZik: the skins and the lyrics its users share on jumphp.net (GET / POST * /api/apps/winzik/shared/<kind>; public at once). Kinds: `skin` (a .zip, SkinPack) and `lyrics` (a .LYR). * project.yaml `market_url` says where (JUMPHP_MARKET_URL tries another one; '' = no marketplace, the tests). */final class Market{    /** a skin or a lyrics file of the market: never kept in memory past this */    public const MAX_DOWNLOAD = 20 << 20;    public const DEFAULT_URL = 'https://jumphp.net/api/apps/winzik/shared';    public function __construct(private Http $http, private string $url)    {    }    public function enabled(): bool    {        return $this->url !== '';    }    /** Settles with the list of a kind (newest first): [{id, name, title, artist, author, ext, size, at, downloads}]. */    public function list(string $kind, string $query = ''): Task    {        $result = new Task();        $this->json('GET', '/' . $kind . ($query !== '' ? '?q=' . rawurlencode($query) : ''))->then(            fn (array $data) => $result->settle(array_values(array_filter((array)($data['items'] ?? []), 'is_array'))),            fn (\Throwable $e) => $result->settle(null, $e),        );        return $result;    }    /** Settles with the bytes of a file. */    public function download(string $kind, string $id): Task    {        $result = new Task();        if (!$this->enabled()) {            $result->settle(null, new \RuntimeException('pas de marketplace'));            return $result;        }        $this->http->request('GET', $this->url . '/' . $kind . '/' . rawurlencode($id), null, [], 60)->then(            function (HttpResponse $r) use ($result): void {                if (!$r->ok()) {                    $result->settle(null, new \RuntimeException('HTTP ' . $r->status));                } elseif (strlen($r->body) > self::MAX_DOWNLOAD) {                    $result->settle(null, new \RuntimeException('fichier trop gros (plus de ' . (self::MAX_DOWNLOAD >> 20) . ' Mo)'));                } else {                    $result->settle($r->body);                }            },            fn (\Throwable $e) => $result->settle(null, $e),        );        return $result;    }    /**     * Sends a file: settles with its entry.     * @param array{name: string, title?: string, artist?: string, author?: string, ext: string} $meta     */    public function upload(string $kind, array $meta, string $bytes): Task    {        return $this->json('POST', '/' . $kind, $meta + ['data' => base64_encode($bytes)]);    }    /** @param array<string, mixed>|null $body */    private function json(string $method, string $path, ?array $body = null): Task    {        $result = new Task();        if (!$this->enabled()) {            $result->settle(null, new \RuntimeException('pas de marketplace'));            return $result;        }        $this->http->request($method, $this->url . $path, $body, ['Accept' => 'application/json'], 60)->then(            function (HttpResponse $r) use ($result): void {                try {                    $data = $r->json();                    if (!$r->ok()) {                        throw new \RuntimeException(is_array($data) && isset($data['error']) ? (string)$data['error'] : 'HTTP ' . $r->status);                    }                    $result->settle(is_array($data) ? $data : []);                } catch (\Throwable $e) {                    $result->settle(null, $e);                }            },            fn (\Throwable $e) => $result->settle(null, $e),        );        return $result;    }}