2015-08-31 16:19:23 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
|
|
|
* Copyright (C) 2015 Peter Deltchev
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-10-24 03:22:14 +02:00
|
|
|
namespace Poniverse\Ponyfm\Commands;
|
2015-08-31 16:19:23 +02:00
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
use Carbon\Carbon;
|
2015-12-18 09:56:13 +01:00
|
|
|
use Config;
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2015-12-26 12:40:47 +01:00
|
|
|
use Input;
|
|
|
|
use Poniverse\Ponyfm\Album;
|
2015-12-18 09:56:13 +01:00
|
|
|
use Poniverse\Ponyfm\Exceptions\InvalidEncodeOptionsException;
|
2015-12-26 12:40:47 +01:00
|
|
|
use Poniverse\Ponyfm\Genre;
|
2015-12-27 11:36:49 +01:00
|
|
|
use Poniverse\Ponyfm\Image;
|
2015-12-18 09:56:13 +01:00
|
|
|
use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
|
2015-10-24 03:22:14 +02:00
|
|
|
use Poniverse\Ponyfm\Track;
|
|
|
|
use Poniverse\Ponyfm\TrackFile;
|
2015-09-10 13:15:19 +02:00
|
|
|
use AudioCache;
|
|
|
|
use File;
|
|
|
|
use Illuminate\Support\Str;
|
2015-12-26 12:40:47 +01:00
|
|
|
use Poniverse\Ponyfm\TrackType;
|
2015-08-31 16:19:23 +02:00
|
|
|
|
|
|
|
class UploadTrackCommand extends CommandBase
|
|
|
|
{
|
2015-12-18 09:56:13 +01:00
|
|
|
use DispatchesJobs;
|
|
|
|
|
|
|
|
|
2015-09-10 13:15:19 +02:00
|
|
|
private $_allowLossy;
|
|
|
|
private $_allowShortTrack;
|
2015-12-26 12:40:47 +01:00
|
|
|
private $_customTrackSource;
|
|
|
|
private $_autoPublishByDefault;
|
|
|
|
|
2015-09-10 13:15:19 +02:00
|
|
|
private $_losslessFormats = [
|
|
|
|
'flac',
|
|
|
|
'pcm_s16le ([1][0][0][0] / 0x0001)',
|
|
|
|
'pcm_s16be',
|
|
|
|
'adpcm_ms ([2][0][0][0] / 0x0002)',
|
|
|
|
'pcm_s24le ([1][0][0][0] / 0x0001)',
|
|
|
|
'pcm_s24be',
|
|
|
|
'pcm_f32le ([3][0][0][0] / 0x0003)',
|
|
|
|
'pcm_f32be (fl32 / 0x32336C66)'
|
|
|
|
];
|
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
public function __construct($allowLossy = false, $allowShortTrack = false, $customTrackSource = null, $autoPublishByDefault = false)
|
2015-09-10 13:15:19 +02:00
|
|
|
{
|
|
|
|
$this->_allowLossy = $allowLossy;
|
|
|
|
$this->_allowShortTrack = $allowShortTrack;
|
2015-12-26 12:40:47 +01:00
|
|
|
$this->_customTrackSource = $customTrackSource;
|
|
|
|
$this->_autoPublishByDefault = $autoPublishByDefault;
|
2015-09-10 13:15:19 +02:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return \Auth::user() != null;
|
|
|
|
}
|
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
protected function getGenreId(string $genreName) {
|
|
|
|
return Genre::firstOrCreate(['name' => $genreName, 'slug' => Str::slug($genreName)])->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAlbumId(int $artistId, $albumName) {
|
|
|
|
if (null !== $albumName) {
|
|
|
|
$album = Album::firstOrNew([
|
|
|
|
'user_id' => $artistId,
|
|
|
|
'title' => $albumName
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (null === $album->id) {
|
|
|
|
$album->description = '';
|
|
|
|
$album->track_count = 0;
|
|
|
|
$album->save();
|
|
|
|
return $album->id;
|
|
|
|
} else {
|
|
|
|
return $album->id;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
* @return CommandResponse
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
$user = \Auth::user();
|
2015-11-09 20:35:30 +01:00
|
|
|
$trackFile = \Input::file('track', null);
|
|
|
|
|
|
|
|
if (null === $trackFile) {
|
|
|
|
return CommandResponse::fail(['track' => ['You must upload an audio file!']]);
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
$audio = \AudioCache::get($trackFile->getPathname());
|
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
|
|
|
|
$track = new Track();
|
|
|
|
$track->user_id = $user->id;
|
2015-12-26 12:40:47 +01:00
|
|
|
$track->title = Input::get('title', pathinfo($trackFile->getClientOriginalName(), PATHINFO_FILENAME));
|
2015-12-18 09:56:13 +01:00
|
|
|
$track->duration = $audio->getDuration();
|
2015-12-26 12:40:47 +01:00
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
|
|
|
|
$track->save();
|
|
|
|
$track->ensureDirectoryExists();
|
|
|
|
|
2015-12-25 03:08:49 +01:00
|
|
|
if (!is_dir(Config::get('ponyfm.files_directory') . '/queued-tracks')) {
|
|
|
|
mkdir(Config::get('ponyfm.files_directory') . '/queued-tracks', 0755, true);
|
|
|
|
}
|
2015-12-18 09:56:13 +01:00
|
|
|
$trackFile = $trackFile->move(Config::get('ponyfm.files_directory').'/queued-tracks', $track->id);
|
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
$input = Input::all();
|
|
|
|
$input['track'] = $trackFile;
|
2015-12-18 09:56:13 +01:00
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
$validator = \Validator::make($input, [
|
2015-08-31 16:19:23 +02:00
|
|
|
'track' =>
|
|
|
|
'required|'
|
2015-09-10 13:15:19 +02:00
|
|
|
. ($this->_allowLossy ? '' : 'audio_format:'. implode(',', $this->_losslessFormats).'|')
|
|
|
|
. ($this->_allowShortTrack ? '' : 'min_duration:30|')
|
2015-12-26 12:40:47 +01:00
|
|
|
. 'audio_channels:1,2',
|
|
|
|
|
|
|
|
'auto_publish' => 'boolean',
|
|
|
|
'title' => 'string',
|
|
|
|
'track_type_id' => 'exists:track_types,id',
|
|
|
|
'genre' => 'string',
|
|
|
|
'album' => 'string',
|
|
|
|
'track_number' => 'integer',
|
|
|
|
'released_at' => 'date_format:'.Carbon::ISO8601,
|
|
|
|
'description' => 'string',
|
|
|
|
'lyrics' => 'string',
|
|
|
|
'is_vocal' => 'boolean',
|
|
|
|
'is_explicit' => 'boolean',
|
|
|
|
'is_downloadable' => 'boolean',
|
|
|
|
'is_listed' => 'boolean',
|
2015-12-27 11:36:49 +01:00
|
|
|
'cover' => 'image|mimes:png,jpeg|min_width:350|min_height:350',
|
2015-12-26 12:40:47 +01:00
|
|
|
'metadata' => 'json',
|
2015-08-31 16:19:23 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2015-12-18 09:56:13 +01:00
|
|
|
$track->delete();
|
2015-08-31 16:19:23 +02:00
|
|
|
return CommandResponse::fail($validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
// Process optional track fields
|
|
|
|
$autoPublish = (bool) Input::get('auto_publish', $this->_autoPublishByDefault);
|
|
|
|
|
|
|
|
$track->title = Input::get('title', $track->title);
|
|
|
|
$track->track_type_id = Input::get('track_type_id', TrackType::UNCLASSIFIED_TRACK);
|
|
|
|
$track->genre_id = $this->getGenreId(Input::get('genre', 'Unknown'));
|
|
|
|
$track->album_id = $this->getAlbumId($user->id, Input::get('album', null));
|
|
|
|
$track->track_number = $track->album_id !== null ? (int) Input::get('track_number', null) : null;
|
|
|
|
$track->released_at = Input::has('released_at') ? Carbon::createFromFormat(Carbon::ISO8601, Input::get('released_at')) : null;
|
|
|
|
$track->description = Input::get('description', '');
|
|
|
|
$track->lyrics = Input::get('lyrics', '');
|
|
|
|
$track->is_vocal = (bool) Input::get('is_vocal');
|
|
|
|
$track->is_explicit = (bool) Input::get('is_explicit');
|
|
|
|
$track->is_downloadable = (bool) Input::get('is_downloadable');
|
|
|
|
$track->is_listed = (bool) Input::get('is_listed', true);
|
|
|
|
|
|
|
|
$track->source = $this->_customTrackSource ?? 'direct_upload';
|
|
|
|
|
2015-12-27 11:36:49 +01:00
|
|
|
if (Input::hasFile('cover')) {
|
|
|
|
$track->cover_id = Image::upload(Input::file('cover'), $track->user_id)->id;
|
|
|
|
}
|
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
// 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));
|
|
|
|
|
|
|
|
$track->save();
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
try {
|
|
|
|
$source = $trackFile->getPathname();
|
|
|
|
|
2015-09-10 13:15:19 +02:00
|
|
|
// Lossy uploads need to be identified and set as the master file
|
|
|
|
// without being re-encoded.
|
|
|
|
$audioObject = AudioCache::get($source);
|
|
|
|
$isLossyUpload = !in_array($audioObject->getAudioCodec(), $this->_losslessFormats);
|
|
|
|
|
|
|
|
if ($isLossyUpload) {
|
|
|
|
if ($audioObject->getAudioCodec() === 'mp3') {
|
|
|
|
$masterFormat = 'MP3';
|
|
|
|
|
|
|
|
} else if (Str::startsWith($audioObject->getAudioCodec(), 'aac')) {
|
|
|
|
$masterFormat = 'AAC';
|
|
|
|
|
|
|
|
} else if ($audioObject->getAudioCodec() === 'vorbis') {
|
|
|
|
$masterFormat = 'OGG Vorbis';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$validator->messages()->add('track', 'The track does not contain audio in a known lossy format.');
|
2015-12-18 09:56:13 +01:00
|
|
|
$track->delete();
|
2015-09-10 13:15:19 +02:00
|
|
|
return CommandResponse::fail($validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
$trackFile = new TrackFile();
|
|
|
|
$trackFile->is_master = true;
|
|
|
|
$trackFile->format = $masterFormat;
|
|
|
|
$trackFile->track_id = $track->id;
|
|
|
|
$trackFile->save();
|
|
|
|
|
|
|
|
// Lossy masters are copied into the datastore - no re-encoding involved.
|
|
|
|
File::copy($source, $trackFile->getFile());
|
|
|
|
}
|
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
|
|
|
|
$trackFiles = [];
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
foreach (Track::$Formats as $name => $format) {
|
2015-09-10 13:15:19 +02:00
|
|
|
// Don't bother with lossless transcodes of lossy uploads, and
|
|
|
|
// don't re-encode the lossy master.
|
|
|
|
if ($isLossyUpload && ($format['is_lossless'] || $name === $masterFormat)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
$trackFile = new TrackFile();
|
|
|
|
$trackFile->is_master = $name === 'FLAC' ? true : false;
|
|
|
|
$trackFile->format = $name;
|
2015-12-18 09:56:13 +01:00
|
|
|
$trackFile->status = TrackFile::STATUS_PROCESSING;
|
2015-11-10 07:29:02 +01:00
|
|
|
|
2015-11-01 17:49:28 +01:00
|
|
|
if (in_array($name, Track::$CacheableFormats) && $trackFile->is_master == false) {
|
2015-10-27 17:21:03 +01:00
|
|
|
$trackFile->is_cacheable = true;
|
|
|
|
} else {
|
|
|
|
$trackFile->is_cacheable = false;
|
|
|
|
}
|
2015-08-31 16:19:23 +02:00
|
|
|
$track->trackFiles()->save($trackFile);
|
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
// All TrackFile records we need are synchronously created
|
|
|
|
// before kicking off the encode jobs in order to avoid a race
|
|
|
|
// condition with the "temporary" source file getting deleted.
|
|
|
|
$trackFiles[] = $trackFile;
|
|
|
|
}
|
2015-10-29 17:26:01 +01:00
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
try {
|
|
|
|
foreach($trackFiles as $trackFile) {
|
2015-12-26 12:40:47 +01:00
|
|
|
$this->dispatch(new EncodeTrackFile($trackFile, false, true, $autoPublish));
|
2015-10-27 17:21:03 +01:00
|
|
|
}
|
2015-08-31 16:19:23 +02:00
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
} catch (InvalidEncodeOptionsException $e) {
|
|
|
|
$track->delete();
|
|
|
|
return CommandResponse::fail(['track' => [$e->getMessage()]]);
|
|
|
|
}
|
2015-08-31 16:19:23 +02:00
|
|
|
|
2015-10-27 18:21:14 +01:00
|
|
|
} catch (\Exception $e) {
|
2015-08-31 16:19:23 +02:00
|
|
|
$track->delete();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandResponse::succeed([
|
|
|
|
'id' => $track->id,
|
2015-12-26 12:40:47 +01:00
|
|
|
'name' => $track->name,
|
|
|
|
'title' => $track->title,
|
|
|
|
'slug' => $track->slug,
|
|
|
|
'autoPublish' => $autoPublish,
|
2015-08-31 16:19:23 +02:00
|
|
|
]);
|
|
|
|
}
|
2015-09-13 07:21:17 +02:00
|
|
|
}
|