From 454411b24b602f7dfb9d8a9c840681aaaac109eb Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Sun, 27 Dec 2015 02:36:49 -0800 Subject: [PATCH] Added the ability to the API to include cover art with a track upload. --- app/Commands/EditTrackCommand.php | 4 ++-- app/Commands/UploadTrackCommand.php | 6 ++++++ tests/ApiTest.php | 9 +++++++++ tests/TestCase.php | 19 ++++++++++++++----- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/Commands/EditTrackCommand.php b/app/Commands/EditTrackCommand.php index 420e4cfe..dae10b6d 100644 --- a/app/Commands/EditTrackCommand.php +++ b/app/Commands/EditTrackCommand.php @@ -25,8 +25,8 @@ use Poniverse\Ponyfm\Image; use Poniverse\Ponyfm\Track; use Poniverse\Ponyfm\TrackType; use Poniverse\Ponyfm\User; -use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\DB; +use Auth; +use DB; class EditTrackCommand extends CommandBase { diff --git a/app/Commands/UploadTrackCommand.php b/app/Commands/UploadTrackCommand.php index be33fb3d..550fba6d 100644 --- a/app/Commands/UploadTrackCommand.php +++ b/app/Commands/UploadTrackCommand.php @@ -27,6 +27,7 @@ use Input; use Poniverse\Ponyfm\Album; use Poniverse\Ponyfm\Exceptions\InvalidEncodeOptionsException; use Poniverse\Ponyfm\Genre; +use Poniverse\Ponyfm\Image; use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\Track; use Poniverse\Ponyfm\TrackFile; @@ -149,6 +150,7 @@ class UploadTrackCommand extends CommandBase 'is_explicit' => 'boolean', 'is_downloadable' => 'boolean', 'is_listed' => 'boolean', + 'cover' => 'image|mimes:png,jpeg|min_width:350|min_height:350', 'metadata' => 'json', ]); @@ -176,6 +178,10 @@ class UploadTrackCommand extends CommandBase $track->source = $this->_customTrackSource ?? 'direct_upload'; + if (Input::hasFile('cover')) { + $track->cover_id = Image::upload(Input::file('cover'), $track->user_id)->id; + } + // If json_decode() isn't called here, Laravel will surround the JSON // string with quotes when storing it in the database, which breaks things. $track->metadata = json_decode(Input::get('metadata', null)); diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 55cf38f0..c1448242 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -84,6 +84,8 @@ class ApiTest extends TestCase { 'is_downloadable' => false, 'is_listed' => false, 'metadata' => $track->metadata + ], [ + 'cover' => $this->getTestFileForUpload('ponyfm-transparent-cover-art.png') ]); $this->seeInDatabase('genres', [ @@ -94,8 +96,14 @@ class ApiTest extends TestCase { 'title' => $track->album ]); + $this->seeInDatabase('images', [ + 'id' => 1, + 'uploaded_by' => $this->user->id + ]); + $this->seeInDatabase('tracks', [ 'title' => $track->title, + 'user_id' => $this->user->id, 'track_type_id' => $track->track_type_id, 'released_at' => "2015-01-01 01:01:01", 'description' => $track->description, @@ -104,6 +112,7 @@ class ApiTest extends TestCase { 'is_explicit' => true, 'is_downloadable' => false, 'is_listed' => false, + 'cover_id' => 1, 'metadata' => $track->metadata ]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 8bbca4c4..e276137b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,6 +28,13 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase */ protected $baseUrl = 'http://ponyfm-testing.poni'; + /** + * The Pony.fm user used in tests. + * + * @var User + */ + protected $user = null; + protected static $initializedFiles = false; /** @@ -57,7 +64,8 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase // and add them here with their last-modified date as a Unix // timestamp. $files = [ - 'ponyfm-test.flac' => 1450965707 + 'ponyfm-test.flac' => 1450965707, + 'ponyfm-transparent-cover-art.png' => 1451211579 ]; foreach ($files as $filename => $lastModifiedTimestamp) { @@ -121,15 +129,16 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase * Helper function for testing file uploads to the API. * * @param array $parameters + * @param array $files */ - protected function callUploadWithParameters(array $parameters) { + protected function callUploadWithParameters(array $parameters, array $files = []) { $this->expectsJobs(Poniverse\Ponyfm\Jobs\EncodeTrackFile::class); - $user = factory(User::class)->create(); + $this->user = factory(User::class)->create(); $file = $this->getTestFileForUpload('ponyfm-test.flac'); - $this->actingAs($user) - ->call('POST', '/api/v1/tracks', $parameters, [], ['track' => $file]); + $this->actingAs($this->user) + ->call('POST', '/api/v1/tracks', $parameters, [], array_merge(['track' => $file], $files)); $this->assertResponseStatus(202); }