Skip to content

Commit

Permalink
fixed song cache not updating after uploading a new version
Browse files Browse the repository at this point in the history
made SongComposer::createOrUpdate protected
added SongComposer::create
fixed behaviour of SongComposer::update
SongComposer::compose now automatically updates song cache
  • Loading branch information
Byorun committed Jul 12, 2018
1 parent 1b68a81 commit f56b2c2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/BeatsaverImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function importSongs()
$file = realpath(config('beatsaver.legacy.songPath') . '/' . $song->id . '.zip');
if ($file) {
// $this->line('Importing: ID ' . $song->id . ' PATH ' . $file);
$status = $composer->createOrUpdate([
$status = $composer->create([
'userId' => $song->ownerid,
'songId' => $song->id,
'name' => html_entity_decode($song->beatname, ENT_QUOTES | ENT_HTML5),
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/BeatSaverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function vote($key, VoteRequest $request, SongComposer $composer)
* @param $key
* @param SongComposer $composer
*
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @return \Illuminate\Http\RedirectResponse
*/
public function download($key, SongComposer $composer)
{
Expand Down Expand Up @@ -169,7 +169,7 @@ public function uploadSubmit(UploadRequest $request, SongComposer $composer)
'description' => $request->input('description'),
];

$song = $composer->createOrUpdate($metadata, $process);
$song = $composer->create($metadata, $process);

if ($song['status'] != $composer::SONG_CREATED) {
Log::debug($song['status']);
Expand Down Expand Up @@ -265,7 +265,7 @@ public function songEditSubmit($id, UpdateSongRequest $request, SongComposer $co
$metadata['updateFile'] = $process;
$metadata['songId'] = $song->id;

$info = $composer->createOrUpdate($metadata, $process);
$info = $composer->update($song, $metadata);

if ($info['status'] == $composer::SONG_CREATED || $info['status'] == $composer::SONG_UPDATED) {
return redirect()->route('browse.detail', ['key' => $info['song']['key']]);
Expand Down
70 changes: 34 additions & 36 deletions app/SongComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ class SongComposer
*
* @param string $key
* @param bool $apiFormat
* @param bool $updateCache
* @param bool $noCache
*
* @return array
*/
public function get(string $key, $apiFormat = false, $updateCache = false): array
public function get(string $key, $apiFormat = false, $noCache = false): array
{
if ($updateCache) {
if ($noCache) {
Log::debug('Force Cache Update: ' . $key);
$song = $this->compose($key);
if ($song) {
$this->updateCache($song);
if ($apiFormat) {
$song = $this->convertSongToApiFormat($song);
}
Expand Down Expand Up @@ -73,7 +72,6 @@ public function get(string $key, $apiFormat = false, $updateCache = false): arra
Log::debug('cache empty ' . $key . ' try compose');
if ($song = $this->compose($key)) {
if ($song) {
$this->updateCache($song);
if ($apiFormat) {
$song = $this->convertSongToApiFormat($song);
}
Expand All @@ -93,7 +91,7 @@ public function get(string $key, $apiFormat = false, $updateCache = false): arra
*
* @return array
*/
public function createOrUpdate(array $metadata, string $file): array
protected function createOrUpdate(array $metadata, string $file): array
{
//check if song fingerprint already exists
if (!empty($file)) {
Expand Down Expand Up @@ -145,7 +143,7 @@ public function createOrUpdate(array $metadata, string $file): array
if (empty($file)) {
return [
'status' => static::SONG_UPDATED,
'song' => $this->get($song->id, false, true)
'key' => $song->id,
];
}

Expand Down Expand Up @@ -176,47 +174,45 @@ public function createOrUpdate(array $metadata, string $file): array

return [
'status' => static::SONG_CREATED,
'song' => [
'id' => $song->id,
'key' => $song->id . '-' . $songDetails->id,
'name' => $song->name,
'description' => $song->description,
'uploader' => $song->uploader->name,
'uploaderId' => $song->uploader->id,
'songName' => $songDetails->song_name,
'songSubName' => $songDetails->song_sub_name,
'authorName' => $songDetails->author_name,
'bpm' => $songDetails->bpm,
'difficulties' => $songData['difficultyLevels'],
'downloadCount' => 0,
'playedCount' => 0,
'upVotes' => 0,
'upVotesTotal' => 0,
'downVotes' => 0,
'downVotesTotal' => 0,
'version' => $song->details->count(), //@todo fix version if $detailId is specified
'createdAt' => $songDetails->created_at,
'linkUrl' => route('browse.detail', ['key' => $song->id . '-' . $songDetails->id]),
'downloadUrl' => route('download', ['key' => $song->id . '-' . $songDetails->id]),
'coverUrl' => asset("storage/songs/{$song->id}/{$song->id}-{$songDetails->id}.$songDetails->cover"),
'hashMd5' => $songDetails->hash_md5,
'hashSha1' => $songDetails->hash_sha1,
]
'key' => $song->id . '-' . $songDetails->id,
];
}

/**
* @param array $metadata
* @param string $file
*
* @return array
*/
public function create(array $metadata, string $file): array
{
$songData = $this->createOrUpdate($metadata, $file);
if ($songData['status'] == static::SONG_CREATED) {
$songData['song'] = $this->compose($songData['key']);
}

return $songData;
}

/**
* @param Song $song
* @param array $metadata
*
* @return array
*/
public function update(Song $song, array $metadata)
public function update(Song $song, array $metadata): array
{
if ($song) {
// update song version if we have a new song archive
if (!empty($metadata['updateFile'])) {
Log::debug('found update file');
return $this->createOrUpdate($metadata, $metadata['updateFile']);

$songData = $this->createOrUpdate($metadata, $metadata['updateFile']);
if ($songData['status'] == static::SONG_CREATED) {
$songData['status'] = static::SONG_UPDATED;
$songData['song'] = $this->compose($songData['key']);
}
return $songData;
}

Log::debug('only update meta data');
Expand All @@ -225,7 +221,8 @@ public function update(Song $song, array $metadata)
$song->description = $metadata['description'];
$song->save();

return $this->get($song->id);
$songData = $this->compose($song->id);
return $songData;
}

Log::debug('no song');
Expand Down Expand Up @@ -398,6 +395,7 @@ protected function compose(string $key): array
];
}

$this->updateCache($songData);

return $songData;
}
Expand Down

0 comments on commit f56b2c2

Please sign in to comment.