Skip to content

Commit

Permalink
Merge pull request #10695 from nanaya/replay-file-cleanup
Browse files Browse the repository at this point in the history
Fix missing replay file error and refactor legacy download
  • Loading branch information
notbakaneko authored Nov 1, 2023
2 parents a6a67c1 + ab5cbd3 commit a262a02
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 47 deletions.
24 changes: 2 additions & 22 deletions app/Http/Controllers/ScoresController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use App\Models\Solo\Score as SoloScore;
use App\Transformers\ScoreTransformer;
use App\Transformers\UserCompactTransformer;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class ScoresController extends Controller
{
Expand Down Expand Up @@ -46,13 +45,8 @@ public function download($rulesetOrSoloId, $id = null)
->firstOrFail();
}

try {
$file = $score instanceof SoloScore
? $score->getReplayFile()
: $this->getLegacyReplayFile($score);
} catch (FileNotFoundException $e) {
// missing from storage.
log_error($e);
$file = $score->getReplayFile();
if ($file === null) {
abort(404);
}

Expand Down Expand Up @@ -122,20 +116,6 @@ public function userRankLookup()
return response()->json($score->userRank(['cached' => false]) - 1);
}

private function getLegacyReplayFile(ScoreBest $score): string
{
$replayFile = $score->replayFile();
if ($replayFile === null) {
abort(404);
}
$body = $replayFile->get();

return $replayFile->headerChunk()
.pack('i', strlen($body))
.$body
.$replayFile->endChunk();
}

private function makeReplayFilename(ScoreBest|SoloScore $score): string
{
$prefix = $score instanceof SoloScore
Expand Down
50 changes: 31 additions & 19 deletions app/Libraries/ReplayFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,20 @@

use App\Exceptions\InvariantException;
use App\Models\Beatmap;
use Storage;
use App\Models\Score\Best\Model as ScoreBest;
use Illuminate\Contracts\Filesystem\Filesystem;

class ReplayFile
{
const DEFAULT_VERSION = 20151228;

private $diskName;
private $filename;
private $score;

public function __construct($score)
{
$this->filename = $score->getKey();
$mode = $score->getMode();
$this->diskName = 'replays.'.$mode.'.'.config('osu.score_replays.storage');
$this->score = $score;
}

public function __call($method, $parameters)
public function __construct(private ScoreBest $score)
{
return call_user_func_array([$this->disk(), $method], array_merge([$this->filename], $parameters));
}

public function disk()
public function delete(): void
{
return Storage::disk($this->diskName);
$this->storage()->delete($this->path());
}

/**
Expand All @@ -45,9 +33,16 @@ public function endChunk()
return pack('q', $this->score->score_id);
}

public function getDiskName()
public function get(): ?string
{
return $this->diskName;
$body = $this->storage()->get($this->path());

return $body === null
? null
: $this->headerChunk()
.pack('i', strlen($body))
.$body
.$this->endChunk();
}

public function getVersion()
Expand Down Expand Up @@ -102,4 +97,21 @@ public function headerChunk(): string

return implode('', $components);
}

public function put(string $content): void
{
$this->storage()->put($this->path(), $content);
}

private function path(): string
{
return (string) $this->score->getKey();
}

private function storage(): Filesystem
{
$disk = 'replays.'.$this->score->getMode().'.'.config('osu.score_replays.storage');

return \Storage::disk($disk);
}
}
9 changes: 5 additions & 4 deletions app/Models/Score/Best/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ public function getAttribute($key)

public function replayFile(): ?ReplayFile
{
if ($this->replay) {
return new ReplayFile($this);
}
return $this->replay ? new ReplayFile($this) : null;
}

return null;
public function getReplayFile(): ?string
{
return $this->replayFile()?->get();
}

public function macroForListing()
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Solo/Score.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function getMode(): string
return Beatmap::modeStr($this->ruleset_id);
}

public function getReplayFile(): string
public function getReplayFile(): ?string
{
return Storage::disk(config('osu.score_replays.storage').'-solo-replay')
->get($this->getKey());
Expand Down
2 changes: 1 addition & 1 deletion database/factories/Score/Best/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function withReplay()
return $this->state([
'replay' => true,
])->afterCreating(function ($score) {
$score->replayFile()->disk()->put($score->getKey(), 'this-is-totally-a-legit-replay');
$score->replayFile()->put('this-is-totally-a-legit-replay');
});
}
}

0 comments on commit a262a02

Please sign in to comment.