Skip to content

Commit

Permalink
Big revamp for artist and album info
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Aug 19, 2018
1 parent cedb9f9 commit 5fbec01
Show file tree
Hide file tree
Showing 31 changed files with 608 additions and 308 deletions.
2 changes: 0 additions & 2 deletions app/Console/Commands/SyncMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class SyncMedia extends Command

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Expand Down
36 changes: 36 additions & 0 deletions app/Events/AlbumInformationFetched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Events;

use App\Models\Album;
use Illuminate\Queue\SerializesModels;

class AlbumInformationFetched extends Event
{
use SerializesModels;

private $album;
private $information;

public function __construct(Album $album, array $information)
{
$this->album = $album;
$this->information = $information;
}

/**
* @return Album
*/
public function getAlbum()
{
return $this->album;
}

/**
* @return array
*/
public function getInformation()
{
return $this->information;
}
}
36 changes: 36 additions & 0 deletions app/Events/ArtistInformationFetched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Events;

use App\Models\Artist;
use Illuminate\Queue\SerializesModels;

class ArtistInformationFetched
{
use SerializesModels;

private $artist;
private $information;

public function __construct(Artist $artist, array $information)
{
$this->artist = $artist;
$this->information = $information;
}

/**
* @return Artist
*/
public function getArtist()
{
return $this->artist;
}

/**
* @return array
*/
public function getInformation()
{
return $this->information;
}
}
10 changes: 9 additions & 1 deletion app/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@

abstract class Event
{
//
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
16 changes: 0 additions & 16 deletions app/Events/LibraryChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,4 @@

class LibraryChanged extends Event
{
/**
* Create a new event instance.
*/
public function __construct()
{
}

/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
10 changes: 0 additions & 10 deletions app/Events/SongLikeToggled.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,4 @@ public function __construct(Interaction $interaction, User $user = null)
$this->interaction = $interaction;
$this->user = $user ?: auth()->user();
}

/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
10 changes: 0 additions & 10 deletions app/Events/SongStartedPlaying.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,4 @@ public function __construct(Song $song, User $user)
$this->song = $song;
$this->user = $user;
}

/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
10 changes: 9 additions & 1 deletion app/Http/Controllers/API/ObjectStorage/S3/SongController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Services\MediaMetadataService;
use Exception;
use Illuminate\Http\JsonResponse;
use Media;

class SongController extends Controller
{
private $mediaMetadataService;

public function __construct(MediaMetadataService $mediaMetadataService)
{
$this->mediaMetadataService = $mediaMetadataService;
}

/**
* Store a new song or update an existing one with data from AWS.
*
Expand All @@ -32,7 +40,7 @@ public function put(PutSongRequest $request)
$album = Album::get($artist, array_get($tags, 'album'), $compilation);

if ($cover = array_get($tags, 'cover')) {
$album->writeCoverFile(base64_decode($cover['data']), $cover['extension']);
$this->mediaMetadataService->writeAlbumCover($album, base64_decode($cover['data']), $cover['extension']);
}

$song = Song::updateOrCreate(['id' => Media::getHash($path)], [
Expand Down
7 changes: 0 additions & 7 deletions app/Listeners/ClearMediaCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

class ClearMediaCache
{
/**
* Create the event listener.
*/
public function __construct()
{
}

/**
* Fired every time a LibraryChanged event is triggered.
* Clears the media cache.
Expand Down
29 changes: 29 additions & 0 deletions app/Listeners/DownloadAlbumCover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Listeners;

use App\Events\AlbumInformationFetched;
use App\Services\MediaMetadataService;

class DownloadAlbumCover
{
private $mediaMetadataService;

public function __construct(MediaMetadataService $mediaMetadataService)
{
$this->mediaMetadataService = $mediaMetadataService;
}

public function handle(AlbumInformationFetched $event)
{
$info = $event->getInformation();
$album = $event->getAlbum();

$image = array_get($info, 'image');

// If our current album has no cover, and Last.fm has one, why don't we steal it?
if (!$album->has_cover && is_string($image) && ini_get('allow_url_fopen')) {
$this->mediaMetadataService->downloadAlbumCover($album, $image);
}
}
}
29 changes: 29 additions & 0 deletions app/Listeners/DownloadArtistImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Listeners;

use App\Events\ArtistInformationFetched;
use App\Services\MediaMetadataService;

class DownloadArtistImage
{
private $mediaMetadataService;

public function __construct(MediaMetadataService $mediaMetadataService)
{
$this->mediaMetadataService = $mediaMetadataService;
}

public function handle(ArtistInformationFetched $event)
{
$info = $event->getInformation();
$artist = $event->getArtist();

$image = array_get($info, 'image');

// If our current album has no cover, and Last.fm has one, why don't we steal it?
if (!$artist->has_image && is_string($image) && ini_get('allow_url_fopen')) {
$this->mediaMetadataService->downloadArtistImage($artist, $image);
}
}
}
67 changes: 0 additions & 67 deletions app/Models/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,73 +84,6 @@ public static function get(Artist $artist, $name, $isCompilation = false)
]);
}

/**
* Generate a cover from provided data.
*
* @param array $cover The cover data in array format, extracted by getID3.
* For example:
* [
* 'data' => '<binary data>',
* 'image_mime' => 'image/png',
* 'image_width' => 512,
* 'image_height' => 512,
* 'imagetype' => 'PNG', // not always present
* 'picturetype' => 'Other',
* 'description' => '',
* 'datalength' => 7627,
* ]
*/
public function generateCover(array $cover)
{
$extension = explode('/', $cover['image_mime']);
$extension = empty($extension[1]) ? 'png' : $extension[1];

$this->writeCoverFile($cover['data'], $extension);
}

/**
* Write a cover image file with binary data and update the Album with the new cover file.
*
* @param string $binaryData
* @param string $extension The file extension
* @param string $destination The destination path. Automatically generated if empty.
*/
public function writeCoverFile($binaryData, $extension, $destination = '')
{
$extension = trim(strtolower($extension), '. ');
$destination = $destination ?: $this->generateRandomCoverPath($extension);
file_put_contents($destination, $binaryData);

$this->update(['cover' => basename($destination)]);
}

/**
* Copy a cover file from an existing image on the system.
*
* @param string $source The original image's full path.
* @param string $destination The destination path. Automatically generated if empty.
*/
public function copyCoverFile($source, $destination = '')
{
$extension = pathinfo($source, PATHINFO_EXTENSION);
$destination = $destination ?: $this->generateRandomCoverPath($extension);
copy($source, $destination);

$this->update(['cover' => basename($destination)]);
}

/**
* Generate a random path for the cover image.
*
* @param string $extension The extension of the cover (without dot)
*
* @return string
*/
private function generateRandomCoverPath($extension)
{
return app()->publicPath().'/public/img/covers/'.uniqid('', true).".$extension";
}

/**
* Set the album cover.
*
Expand Down
28 changes: 0 additions & 28 deletions app/Models/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,6 @@ public static function get($name)
return self::firstOrCreate(compact('name'), compact('name'));
}

/**
* Write an artist image file with binary data and update the Artist with the new cover file.
*
* @param string $binaryData
* @param string $extension The file extension
* @param string $destination The destination path. Automatically generated if empty.
*/
public function writeImageFile($binaryData, $extension, $destination = '')
{
$extension = trim(strtolower($extension), '. ');
$destination = $destination ?: $this->generateRandomImagePath($extension);
file_put_contents($destination, $binaryData);

$this->update(['image' => basename($destination)]);
}

/**
* Generate a random path for the artist's image.
*
* @param string $extension The extension of the cover (without dot)
*
* @return string
*/
private function generateRandomImagePath($extension)
{
return app()->publicPath().'/public/img/artists/'.uniqid('', true).".$extension";
}

/**
* Turn the image name into its absolute URL.
*
Expand Down
Loading

0 comments on commit 5fbec01

Please sign in to comment.