<commit_before><?php namespace App\Console\Commands;

use App\Events\Stats\GalleryImageViewed;
use Ivacuum\Generic\Commands\Command;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use Swoole\Process;

class ImageViewerServer extends Command
{
    protected $signature = 'app:image-viewer-server {host=127.0.0.1} {port=2730}';
    protected $description = 'Image viewer daemon';

    private int $acceptedConnections = 0;
    private bool $started = false;
    private Server $server;

    public function __destruct()
    {
        $this->stop();
    }

    public function handle()
    {
        $this->server = new Server(
            $this->argument('host'),
            $this->argument('port'),
            SWOOLE_BASE,
            SWOOLE_SOCK_TCP
        );

        $this->listeners();
        $this->server->start();
    }

    public function handleRequest(Request $request, Response $response)
    {
        $this->acceptedConnections++;

        if (app()->isLocal()) {
            $this->info($request->server['request_uri']);
        }

        // $referrer = $request->header['referer'] ?? null;

        if (preg_match('/^\/g\/(?<date>\d{6})\/(?<subfolder>[st]\/)?(?<slug>\d+_[\da-zA-Z]{10}\.[a-z]{3,4})$/', $request->server['request_uri'], $matches)) {
            $date = implode('/', str_split($matches['date'], 2));

            event(new GalleryImageViewed("{$matches['date']}/{$matches['slug']}"));

            $response->header('X-Accel-Redirect', "/d/g/{$date}/{$matches['subfolder']}{$matches['slug']}");

            return $response->redirect('/');
        }

        $response->end('Not Found');
    }<commit_msg>Работающий редирект<commit_after><?php namespace App\Console\Commands;

use App\Events\Stats\GalleryImageViewed;
use Ivacuum\Generic\Commands\Command;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use Swoole\Process;

class ImageViewerServer extends Command
{
    protected $signature = 'app:image-viewer-server {host=127.0.0.1} {port=2730}';
    protected $description = 'Image viewer daemon';

    private int $acceptedConnections = 0;
    private bool $started = false;
    private Server $server;

    public function __destruct()
    {
        $this->stop();
    }

    public function handle()
    {
        $this->server = new Server(
            $this->argument('host'),
            $this->argument('port'),
            SWOOLE_BASE,
            SWOOLE_SOCK_TCP
        );

        $this->listeners();
        $this->server->start();
    }

    public function handleRequest(Request $request, Response $response)
    {
        $this->acceptedConnections++;

        if (app()->isLocal()) {
            $this->info($request->server['request_uri']);
        }

        // $referrer = $request->header['referer'] ?? null;

        if (preg_match('/^\/g\/(?<date>\d{6})\/(?<subfolder>[st]\/)?(?<slug>\d+_[\da-zA-Z]{10}\.[a-z]{3,4})$/', $request->server['request_uri'], $matches)) {
            $date = implode('/', str_split($matches['date'], 2));

            event(new GalleryImageViewed("{$matches['date']}/{$matches['slug']}"));

            $response->detach();

            $this->server->send($response->fd, "HTTP/1.1 302 Found\r\nContent-Length: 0\r\nX-Accel-Redirect: /d/g/{$date}/{$matches['subfolder']}{$matches['slug']}\r\n\r\n");

            return;
        }

        $response->status(404);
        $response->end('Not Found');
    }
{'fraction_non_alphanumeric': 0.19020005798782255, 'fraction_numerical': 0.01246738184981154, 'mean_word_length': 2.713670613562971, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3619105', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1087, 'n_words': 176}