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-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;
|
2015-08-31 16:19:23 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2015-09-10 13:15:19 +02:00
|
|
|
use Illuminate\Support\Str;
|
2015-10-27 17:21:03 +01:00
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
|
|
use Symfony\Component\Process\Process;
|
2015-08-31 16:19:23 +02:00
|
|
|
|
|
|
|
class UploadTrackCommand extends CommandBase
|
|
|
|
{
|
2015-09-10 13:15:19 +02:00
|
|
|
private $_allowLossy;
|
|
|
|
private $_allowShortTrack;
|
|
|
|
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)'
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct($allowLossy = false, $allowShortTrack = false)
|
|
|
|
{
|
|
|
|
$this->_allowLossy = $allowLossy;
|
|
|
|
$this->_allowShortTrack = $allowShortTrack;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return \Auth::user() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
* @return CommandResponse
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
$user = \Auth::user();
|
|
|
|
$trackFile = \Input::file('track');
|
|
|
|
$audio = \AudioCache::get($trackFile->getPathname());
|
|
|
|
|
|
|
|
$validator = \Validator::make(['track' => $trackFile], [
|
|
|
|
'track' =>
|
|
|
|
'required|'
|
2015-09-10 13:15:19 +02:00
|
|
|
. ($this->_allowLossy ? '' : 'audio_format:'. implode(',', $this->_losslessFormats).'|')
|
|
|
|
. ($this->_allowShortTrack ? '' : 'min_duration:30|')
|
2015-09-13 07:21:17 +02:00
|
|
|
. 'audio_channels:1,2'
|
2015-08-31 16:19:23 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return CommandResponse::fail($validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
$track = new Track();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$track->user_id = $user->id;
|
|
|
|
$track->title = pathinfo($trackFile->getClientOriginalName(), PATHINFO_FILENAME);
|
|
|
|
$track->duration = $audio->getDuration();
|
|
|
|
$track->is_listed = true;
|
|
|
|
|
|
|
|
$track->save();
|
|
|
|
|
|
|
|
$destination = $track->getDirectory();
|
|
|
|
$track->ensureDirectoryExists();
|
|
|
|
|
|
|
|
$source = $trackFile->getPathname();
|
|
|
|
$index = 0;
|
|
|
|
|
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.');
|
|
|
|
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-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-10-27 17:21:03 +01:00
|
|
|
if (array_key_exists($name, Track::$CacheableFormats) && $trackFile->is_master == false) {
|
|
|
|
$trackFile->is_cacheable = true;
|
|
|
|
} else {
|
|
|
|
$trackFile->is_cacheable = false;
|
|
|
|
}
|
2015-08-31 16:19:23 +02:00
|
|
|
$track->trackFiles()->save($trackFile);
|
|
|
|
|
2015-10-27 17:21:03 +01:00
|
|
|
if ($trackFile->is_cacheable == false) {
|
|
|
|
$target = $destination . '/' . $trackFile->getFilename();
|
2015-08-31 16:19:23 +02:00
|
|
|
|
2015-10-27 17:21:03 +01:00
|
|
|
$command = $format['command'];
|
|
|
|
$command = str_replace('{$source}', '"' . $source . '"', $command);
|
|
|
|
$command = str_replace('{$target}', '"' . $target . '"', $command);
|
2015-08-31 16:19:23 +02:00
|
|
|
|
2015-10-27 17:21:03 +01:00
|
|
|
Log::info('Encoding ' . $track->id . ' into ' . $target);
|
|
|
|
$this->notify('Encoding ' . $name, $index / count(Track::$Formats) * 100);
|
2015-08-31 16:19:23 +02:00
|
|
|
|
2015-10-27 17:21:03 +01:00
|
|
|
$process = new Process($command);
|
|
|
|
$process->mustRun();
|
|
|
|
}
|
2015-08-31 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$track->updateTags();
|
|
|
|
|
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,
|
|
|
|
'name' => $track->name
|
|
|
|
]);
|
|
|
|
}
|
2015-09-13 07:21:17 +02:00
|
|
|
}
|