Skip to content

Commit

Permalink
chore(builds): upgrade to Laravel 8 (koel#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan authored Nov 14, 2020
1 parent 6130bf7 commit 58c0019
Show file tree
Hide file tree
Showing 75 changed files with 1,964 additions and 1,494 deletions.
3 changes: 3 additions & 0 deletions app/Facades/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Support\Facades\Facade;

/**
* @method static string detectUTFEncoding(string $name)
*/
class Util extends Facade
{
protected static function getFacadeAccessor()
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/API/ObjectStorage/S3/SongController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function put(PutSongRequest $request)
$path = "s3://{$request->bucket}/{$request->key}";

$tags = $request->tags;
$artist = Artist::get(array_get($tags, 'artist'));
$artist = Artist::getOrCreate(array_get($tags, 'artist'));

$compilation = (bool) trim(array_get($tags, 'albumartist'));
$album = Album::get($artist, array_get($tags, 'album'), $compilation);
$album = Album::getOrCreate($artist, array_get($tags, 'album'), $compilation);

if ($cover = array_get($tags, 'cover')) {
$this->mediaMetadataService->writeAlbumCover($album, base64_decode($cover['data']), $cover['extension']);
Expand Down
7 changes: 5 additions & 2 deletions app/Models/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Traits\SupportsDeleteWhereIDsNotIn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand All @@ -30,9 +31,11 @@
* @method static self|null find(int $id)
* @method static Builder where(...$params)
* @method static self first()
* @method static Builder whereArtistIdAndName(int $id, string $name)
*/
class Album extends Model
{
use HasFactory;
use SupportsDeleteWhereIDsNotIn;

const UNKNOWN_ID = 1;
Expand Down Expand Up @@ -63,7 +66,7 @@ public function getIsUnknownAttribute(): bool
* Get an album using some provided information.
* If such is not found, a new album will be created using the information.
*/
public static function get(Artist $artist, string $name, bool $isCompilation = false): self
public static function getOrCreate(Artist $artist, ?string $name = null, bool $isCompilation = false): self
{
// If this is a compilation album, its artist must be "Various Artists"
if ($isCompilation) {
Expand All @@ -72,7 +75,7 @@ public static function get(Artist $artist, string $name, bool $isCompilation = f

return static::firstOrCreate([
'artist_id' => $artist->id,
'name' => $name ?: self::UNKNOWN_NAME,
'name' => trim($name) ?: self::UNKNOWN_NAME,
]);
}

Expand Down
9 changes: 5 additions & 4 deletions app/Models/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Traits\SupportsDeleteWhereIDsNotIn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
Expand All @@ -26,9 +27,11 @@
* @method static self firstOrCreate(array $where, array $params = [])
* @method static Builder where(...$params)
* @method static self first()
* @method static Builder whereName(string $name)
*/
class Artist extends Model
{
use HasFactory;
use SupportsDeleteWhereIDsNotIn;

const UNKNOWN_ID = 1;
Expand Down Expand Up @@ -81,16 +84,14 @@ public function getNameAttribute(string $value): string
* Get an Artist object from their name.
* If such is not found, a new artist will be created.
*/
public static function get(string $name): self
public static function getOrCreate(?string $name = null): self
{
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
if ($encoding = Util::detectUTFEncoding($name)) {
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
}

$name = trim($name) ?: self::UNKNOWN_NAME;

return static::firstOrCreate(compact('name'));
return static::firstOrCreate(['name' => trim($name) ?: self::UNKNOWN_NAME]);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion app/Models/Interaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Models;

use App\Traits\CanFilterByUser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand All @@ -13,11 +15,14 @@
* @property User $user
* @property int $id
*
* @method self firstOrCreate(array $where, array $params = [])
* @method self firstOrCreate(array $where, array $params = [])
* @method static self find(int $id)
* @method static Builder whereSongIdAndUserId(string $songId, string $userId)
*/
class Interaction extends Model
{
use CanFilterByUser;
use HasFactory;

protected $casts = [
'liked' => 'boolean',
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Traits\CanFilterByUser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand All @@ -23,6 +24,7 @@
class Playlist extends Model
{
use CanFilterByUser;
use HasFactory;

protected $hidden = ['user_id', 'created_at', 'updated_at'];
protected $guarded = ['id'];
Expand Down
5 changes: 3 additions & 2 deletions app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -13,10 +14,10 @@
*/
class Setting extends Model
{
protected $primaryKey = 'key';
use HasFactory;

protected $primaryKey = 'key';
public $timestamps = false;

protected $guarded = [];

/**
Expand Down
6 changes: 4 additions & 2 deletions app/Models/Song.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Traits\SupportsDeleteWhereIDsNotIn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand Down Expand Up @@ -38,6 +39,7 @@
*/
class Song extends Model
{
use HasFactory;
use SupportsDeleteWhereIDsNotIn;

protected $guarded = [];
Expand Down Expand Up @@ -156,7 +158,7 @@ public function updateSingle(
$artistName = Artist::UNKNOWN_NAME;
}

$artist = Artist::get($artistName);
$artist = Artist::getOrCreate($artistName);

switch ($compilationState) {
case 1: // ALL, or forcing compilation status to be Yes
Expand All @@ -170,7 +172,7 @@ public function updateSingle(
break;
}

$album = Album::get($artist, $albumName, $isCompilation);
$album = Album::getOrCreate($artist, $albumName, $isCompilation);

$this->artist_id = $artist->id;
$this->album_id = $album->id;
Expand Down
4 changes: 3 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand All @@ -22,8 +23,9 @@
*/
class User extends Authenticatable
{
use Notifiable;
use HasApiTokens;
use HasFactory;
use Notifiable;

/**
* The preferences that we don't want to show to the client.
Expand Down
8 changes: 4 additions & 4 deletions app/Services/FileSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,21 @@ public function sync(array $tags, bool $force = false): int

// If the "artist" tag is specified, use it.
// Otherwise, re-use the existing model value.
$artist = isset($info['artist']) ? Artist::get($info['artist']) : $this->song->album->artist;
$artist = isset($info['artist']) ? Artist::getOrCreate($info['artist']) : $this->song->album->artist;

// If the "album" tag is specified, use it.
// Otherwise, re-use the existing model value.
if (isset($info['album'])) {
$album = $changeCompilationAlbumOnly
? $this->song->album
: Album::get($artist, $info['album'], array_get($info, 'compilation'));
: Album::getOrCreate($artist, $info['album'], array_get($info, 'compilation'));
} else {
$album = $this->song->album;
}
} else {
// The file is newly added.
$artist = Artist::get($info['artist']);
$album = Album::get($artist, $info['album'], array_get($info, 'compilation'));
$artist = Artist::getOrCreate($info['artist']);
$album = Album::getOrCreate($artist, $info['album'], array_get($info, 'compilation'));
}

if (!$album->has_cover) {
Expand Down
11 changes: 4 additions & 7 deletions app/Services/UploadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\SongUploadFailedException;
use App\Models\Setting;
use App\Models\Song;
use function Functional\memoize;
use Illuminate\Http\UploadedFile;

class UploadService
Expand Down Expand Up @@ -45,19 +46,15 @@ public function handleUploadedFile(UploadedFile $file): Song
*/
private function getUploadDirectory(): string
{
static $uploadDirectory;

if (!$uploadDirectory) {
return memoize(static function (): string {
$mediaPath = Setting::get('media_path');

if (!$mediaPath) {
throw new MediaPathNotSetException();
}

$uploadDirectory = $mediaPath.DIRECTORY_SEPARATOR.self::UPLOAD_DIRECTORY.DIRECTORY_SEPARATOR;
}

return $uploadDirectory;
return $mediaPath.DIRECTORY_SEPARATOR.self::UPLOAD_DIRECTORY.DIRECTORY_SEPARATOR;
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct()
/**
* Detects higher UTF encoded strings.
*/
public function detectUTFEncoding(string $str): ?string
public function detectUTFEncoding(?string $str): ?string
{
switch (substr($str, 0, 2)) {
case UTF16_BIG_ENDIAN_BOM:
Expand Down
24 changes: 16 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
"name": "phanan/koel",
"description": "Personal audio streaming service that works.",
"keywords": ["audio", "stream", "mp3"],
"keywords": [
"audio",
"stream",
"mp3"
],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.2.5",
"laravel/framework": "^7.0",
"php": ">=7.3",
"laravel/framework": "^8.0",
"james-heinrich/getid3": "^1.9",
"guzzlehttp/guzzle": "^6.1",
"guzzlehttp/guzzle": "^7.0.1",
"aws/aws-sdk-php-laravel": "^3.1",
"pusher/pusher-php-server": "^4.0",
"predis/predis": "~1.0",
Expand All @@ -21,16 +25,18 @@
"laravel/helpers": "^1.0",
"intervention/image": "^2.5",
"laravel/sanctum": "^2.6",
"doctrine/dbal": "^2.10"
"doctrine/dbal": "^2.10",
"lstrojny/functional-php": "^1.14"
},
"require-dev": {
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^9.0",
"laravel/tinker": "^2.0",
"php-mock/php-mock-mockery": "^1.3",
"friendsofphp/php-cs-fixer": "^2.16"
"friendsofphp/php-cs-fixer": "^2.16",
"dms/phpunit-arraysubset-asserts": "^0.2.1"
},
"suggest": {
"ext-zip": "Allow downloading multiple songs as Zip archives"
Expand All @@ -41,7 +47,9 @@
],
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
"Tests\\": "tests/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers.php"
Expand Down
Loading

0 comments on commit 58c0019

Please sign in to comment.