forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoresController.php
159 lines (130 loc) · 5.32 KB
/
ScoresController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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\Enums\Ruleset;
use App\Models\Score\Best\Model as ScoreBest;
use App\Models\ScoreReplayStats;
use App\Models\Solo\Score as SoloScore;
use App\Transformers\ScoreTransformer;
use App\Transformers\UserCompactTransformer;
use Illuminate\Auth\AuthenticationException;
class ScoresController extends Controller
{
const REPLAY_DOWNLOAD_COUNT_INTERVAL = 86400; // 1 day
public function __construct()
{
parent::__construct();
$this->middleware('auth', ['except' => [
'show',
'download',
]]);
$this->middleware('require-scopes:public');
}
private static function parseIdOrFail(string $id): int
{
if (ctype_digit($id)) {
$ret = (int) $id;
if ($ret > 0) {
return $ret;
}
}
abort(404, osu_trans('errors.scores.invalid_id'));
}
public function download($rulesetOrSoloId, $id = null)
{
$currentUser = \Auth::user();
if (!is_api_request() && $currentUser === null) {
throw new AuthenticationException('User is not logged in.');
}
$shouldRedirect = !is_api_request() && !from_app_url();
if ($id === null) {
if ($shouldRedirect) {
return ujs_redirect(route('scores.show', ['rulesetOrScore' => $rulesetOrSoloId]));
}
$soloScore = SoloScore::where('has_replay', true)->findOrFail($rulesetOrSoloId);
$score = $soloScore->legacyScore() ?? $soloScore;
} else {
if ($shouldRedirect) {
return ujs_redirect(route('scores.show', ['rulesetOrScore' => $rulesetOrSoloId, 'score' => $id]));
}
// don't limit downloading replays of restricted users for review purpose
$score = ScoreBest::getClass($rulesetOrSoloId)
::where('score_id', $id)
->where('replay', true)
->firstOrFail();
$soloScore = SoloScore::firstWhere(['legacy_score_id' => $score->getKey(), 'ruleset_id' => $score->ruleset_id]);
}
$file = $score->getReplayFile();
if ($file === null) {
abort(404);
}
if (
$currentUser !== null
&& !$currentUser->isRestricted()
&& $currentUser->getKey() !== $score->user_id
&& ($currentUser->token()?->client->password_client ?? false)
) {
$countLock = \Cache::lock(
"view:score_replay:{$score->getKey()}:{$currentUser->getKey()}",
static::REPLAY_DOWNLOAD_COUNT_INTERVAL,
);
if ($countLock->get()) {
$score->user->statistics($score->getMode(), true)->increment('replay_popularity');
$currentMonth = format_month_column(new \DateTime());
$score->user->replaysWatchedCounts()
->firstOrCreate(['year_month' => $currentMonth], ['count' => 0])
->incrementInstance('count');
if ($score instanceof ScoreBest) {
$score->replayViewCount()
->firstOrCreate([], ['play_count' => 0])
->incrementInstance('play_count');
}
if ($soloScore !== null) {
ScoreReplayStats
::createOrFirst(['score_id' => $soloScore->getKey()], ['user_id' => $soloScore->user_id])
->incrementInstance('watch_count');
}
}
}
static $responseHeaders = [
'Content-Type' => 'application/x-osu-replay',
];
return response()->streamDownload(function () use ($file) {
echo $file;
}, $this->makeReplayFilename($score), $responseHeaders);
}
public function show($rulesetOrSoloId, $legacyId = null)
{
if ($legacyId === null) {
$scoreQuery = SoloScore::whereKey(static::parseIdOrFail($rulesetOrSoloId));
} else {
$scoreQuery = SoloScore::where([
'ruleset_id' => Ruleset::tryFromName($rulesetOrSoloId) ?? abort(404, 'unknown ruleset name'),
'legacy_score_id' => static::parseIdOrFail($legacyId),
]);
}
$score = $scoreQuery->whereHas('beatmap.beatmapset')->visibleUsers()->firstOrFail();
$userIncludes = array_map(function ($include) {
return "user.{$include}";
}, UserCompactTransformer::CARD_INCLUDES);
$scoreJson = json_item($score, new ScoreTransformer(), array_merge([
'beatmap.max_combo',
'beatmap.user',
'beatmap.owners',
'beatmapset',
'rank_global',
], $userIncludes));
if (is_json_request()) {
return $scoreJson;
}
return ext_view('scores.show', compact('score', 'scoreJson'));
}
private function makeReplayFilename(ScoreBest|SoloScore $score): string
{
$prefix = $score instanceof SoloScore
? 'solo-replay'
: 'replay';
return "{$prefix}-{$score->getMode()}_{$score->beatmap_id}_{$score->getKey()}.osr";
}
}