<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers;

use App\Models\Score\Best\Model as ScoreBest;
use App\Transformers\UserCompactTransformer;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class ScoresController extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->middleware('auth', ['except' => [
            'show',
            'userRankLookup',
        ]]);

        $this->middleware('require-scopes:public');
    }

    public function download($mode, $id)
    {
        // don't limit downloading replays of restricted users for review purpose
        $score = ScoreBest::getClassByString($mode)
            ::where('score_id', $id)
            ->where('replay', true)
            ->firstOrFail();

        if (!is_api_request() && !from_app_url()) {
            return ujs_redirect(route('scores.show', ['score' => $score->getKey(), 'mode' => $mode]));
        }

        $replayFile = $score->replayFile();
        if ($replayFile === null) {
            abort(404);
        }

        try {
            $filename = "replay-{$mode}_{$score->beatmap_id}_{$score->getKey()}.osr";
            $body = $replayFile->get();
        } catch (FileNotFoundException $e) {
            // missing from storage.
            log_error($e);
            abort(404);
        }

        $file = [
            $replayFile->headerChunk(),
            pack('i', strlen($body)),
            $body,
            $replayFile->endChunk(),
        ];

        return response()->streamDownload(function () use ($file) {
            foreach ($file as $f) {
                echo $f;
            }
        }, $filename, ['Content-Type' => 'application/x-osu-replay']);
    }

    public function show($mode, $id)
    {
        $score = ScoreBest::getClassByString($mode)
            ::whereHas('beatmap.beatmapset')
            ->visibleUsers()
            ->findOrFail($id);

        $userIncludes = array_map(function ($include) {
            return "user.{$include}";
        }, UserCompactTransformer::CARD_INCLUDES);

        $scoreJson = json_item($score, 'Score', array_merge([
            'beatmap.max_combo',
            'beatmapset',
            'rank_global',
        ], $userIncludes));

        if (is_json_request()) {
            return $scoreJson;
        }

        return ext_view('scores.show', compact('score', 'scoreJson'));
    }

    public function userRankLookup()
    {
        $params = get_params(request()->all(), null, [
            'beatmapId:int',
            'score:int',
            'rulesetId:int',
        ]);

        foreach (['beatmapId', 'score', 'rulesetId'] as $key) {
            if (!isset($params[$key])) {
                abort(422, "required parameter '{$key}' is missing");
            }
        }

        $score = ScoreBest
            ::getClass($params['rulesetId'])
            ::where([
                'beatmap_id' => $params['beatmapId'],
                'hidden' => false,
                'score' => $params['score'],
            ])->firstOrFail();

        return response()->json($score->userRank(['cached' => false]) - 1);
    }
}
<commit_msg>Just concat right away<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers;

use App\Models\Score\Best\Model as ScoreBest;
use App\Transformers\UserCompactTransformer;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class ScoresController extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->middleware('auth', ['except' => [
            'show',
            'userRankLookup',
        ]]);

        $this->middleware('require-scopes:public');
    }

    public function download($mode, $id)
    {
        // don't limit downloading replays of restricted users for review purpose
        $score = ScoreBest::getClassByString($mode)
            ::where('score_id', $id)
            ->where('replay', true)
            ->firstOrFail();

        if (!is_api_request() && !from_app_url()) {
            return ujs_redirect(route('scores.show', ['score' => $score->getKey(), 'mode' => $mode]));
        }

        $replayFile = $score->replayFile();
        if ($replayFile === null) {
            abort(404);
        }

        try {
            $filename = "replay-{$mode}_{$score->beatmap_id}_{$score->getKey()}.osr";
            $body = $replayFile->get();
        } catch (FileNotFoundException $e) {
            // missing from storage.
            log_error($e);
            abort(404);
        }

        $file = implode([
            $replayFile->headerChunk(),
            pack('i', strlen($body)),
            $body,
            $replayFile->endChunk(),
        ]);

        return response()->streamDownload(fn () => echo $file, $filename, ['Content-Type' => 'application/x-osu-replay']);
    }

    public function show($mode, $id)
    {
        $score = ScoreBest::getClassByString($mode)
            ::whereHas('beatmap.beatmapset')
            ->visibleUsers()
            ->findOrFail($id);

        $userIncludes = array_map(function ($include) {
            return "user.{$include}";
        }, UserCompactTransformer::CARD_INCLUDES);

        $scoreJson = json_item($score, 'Score', array_merge([
            'beatmap.max_combo',
            'beatmapset',
            'rank_global',
        ], $userIncludes));

        if (is_json_request()) {
            return $scoreJson;
        }

        return ext_view('scores.show', compact('score', 'scoreJson'));
    }

    public function userRankLookup()
    {
        $params = get_params(request()->all(), null, [
            'beatmapId:int',
            'score:int',
            'rulesetId:int',
        ]);

        foreach (['beatmapId', 'score', 'rulesetId'] as $key) {
            if (!isset($params[$key])) {
                abort(422, "required parameter '{$key}' is missing");
            }
        }

        $score = ScoreBest
            ::getClass($params['rulesetId'])
            ::where([
                'beatmap_id' => $params['beatmapId'],
                'hidden' => false,
                'score' => $params['score'],
            ])->firstOrFail();

        return response()->json($score->userRank(['cached' => false]) - 1);
    }
}

{'fraction_non_alphanumeric': 0.15599817545993613, 'fraction_numerical': 0.0036490801277178045, 'mean_word_length': 2.0105263157894737, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1998594', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1856, 'n_words': 374}