Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add download mirror fallback #11435

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app/Http/Controllers/BeatmapsetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,24 @@ public function download($id)

priv_check('BeatmapsetDownload', $beatmapset)->ensureCan();

$recentlyDownloaded = BeatmapDownload::where('user_id', Auth::user()->user_id)
$user = Auth::user();
$userId = $user->getKey();
$recentlyDownloaded = BeatmapDownload::where('user_id', $userId)
->where('timestamp', '>', Carbon::now()->subHours()->getTimestamp())
->count();

if ($recentlyDownloaded > Auth::user()->beatmapsetDownloadAllowance()) {
if ($recentlyDownloaded > $user->beatmapsetDownloadAllowance()) {
abort(429, osu_trans('beatmapsets.download.limit_exceeded'));
}

$noVideo = get_bool(Request::input('noVideo', false));
$mirror = BeatmapMirror::getRandomForRegion(request_country(request()));
$mirror = BeatmapMirror::getRandomForRegion(request_country())
?? BeatmapMirror::getDefault()
?? abort(503, osu_trans('beatmapsets.download.no_mirrors'));

BeatmapDownload::create([
'user_id' => Auth::user()->user_id,
'timestamp' => Carbon::now()->getTimestamp(),
'user_id' => $userId,
'timestamp' => time(),
'beatmapset_id' => $beatmapset->beatmapset_id,
'fulfilled' => 1,
'mirror_id' => $mirror->mirror_id,
Expand Down
12 changes: 10 additions & 2 deletions app/Models/BeatmapMirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace App\Models;

use Auth;
use Illuminate\Database\Eloquent\Builder;

/**
* @property string $base_url
Expand All @@ -29,6 +29,7 @@ class BeatmapMirror extends Model
public $timestamps = false;

protected $hidden = ['secret_key'];
protected array $macros = ['getDefault'];

const MIN_VERSION_TO_USE = 2;

Expand Down Expand Up @@ -65,6 +66,14 @@ public static function getRandomForRegion($region = null)
return $regionalMirror ?? self::getRandom();
}

public function macroGetDefault(): callable
{
return fn (Builder $query): ?static => $query
->where('version', '>=', static::MIN_VERSION_TO_USE)
->where('is_master', true)
->first();
}

public function generateURL(Beatmapset $beatmapset, $skipVideo = false)
{
if ($beatmapset->download_disabled) {
Expand All @@ -81,7 +90,6 @@ public function generateURL(Beatmapset $beatmapset, $skipVideo = false)
$serveFilename = str_replace(['"', '?'], ['', ''], $serveFilename);

$time = time();
$userId = Auth::check() ? Auth::user()->user_id : 0;
$checksum = md5("{$beatmapset->beatmapset_id}{$diskFilename}{$serveFilename}{$time}{$noVideo}{$this->secret_key}");

$url = "{$this->base_url}d/{$beatmapset->beatmapset_id}?fs=".rawurlencode($serveFilename).'&fd='.rawurlencode($diskFilename)."&ts=$time&cs=$checksum&nv=$noVideo";
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/beatmapsets.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

'download' => [
'limit_exceeded' => 'Slow down, play more.',
'no_mirrors' => 'No download servers available.',
],

'featured_artist_badge' => [
Expand Down