The rebuild:track command properly deals with unfinished uploads now.

This commit involved a huge refactoring of UploadTrackCommand.
This commit is contained in:
Peter Deltchev 2016-02-15 05:06:06 -08:00
parent 5588b836a0
commit 5edcde3a8d
5 changed files with 212 additions and 102 deletions

View file

@ -0,0 +1,187 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2016 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/>.
*/
namespace Poniverse\Ponyfm\Commands;
use FFmpegMovie;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Poniverse\Ponyfm\Exceptions\InvalidEncodeOptionsException;
use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
use Poniverse\Ponyfm\Models\Track;
use Poniverse\Ponyfm\Models\TrackFile;
use AudioCache;
use File;
use Illuminate\Support\Str;
use SplFileInfo;
use Validator;
/**
* This command is the "second phase" of the upload process - once metadata has
* been parsed and the track object is created, this generates the track's
* corresponding TrackFile objects and ensures that all of them have been encoded.
*
* @package Poniverse\Ponyfm\Commands
*/
class GenerateTrackFilesCommand extends CommandBase
{
use DispatchesJobs;
private $track;
private $autoPublish;
private $sourceFile;
static $_losslessFormats = [
'flac',
'pcm',
'adpcm',
];
public function __construct(Track $track, SplFileInfo $sourceFile, bool $autoPublish = false, int $reprocessTrackId = null)
{
$this->track = $track;
$this->autoPublish = $autoPublish;
$this->sourceFile = $sourceFile;
}
/**
* @throws \Exception
* @return CommandResponse
*/
public function execute()
{
try {
$source = $this->sourceFile->getPathname();
// Lossy uploads need to be identified and set as the master file
// without being re-encoded.
$audioObject = AudioCache::get($source);
$isLossyUpload = !$this->isLosslessFile($audioObject);
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 = new Validator();
$validator->messages()->add('track', 'The track does not contain audio in a known lossy format.');
$this->track->delete();
return CommandResponse::fail($validator);
}
// Sanity check: skip creating this TrackFile if it already exists.
$trackFile = $this->trackFileExists($masterFormat);
if (!$trackFile) {
$trackFile = new TrackFile();
$trackFile->is_master = true;
$trackFile->format = $masterFormat;
$trackFile->track_id = $this->track->id;
$trackFile->save();
}
// Lossy masters are copied into the datastore - no re-encoding involved.
File::copy($source, $trackFile->getFile());
}
$trackFiles = [];
foreach (Track::$Formats as $name => $format) {
// 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;
}
// Sanity check: skip creating this TrackFile if it already exists.
// But, we'll still encode it!
if ($trackFile = $this->trackFileExists($name)) {
$trackFiles[] = $trackFile;
continue;
}
$trackFile = new TrackFile();
$trackFile->is_master = $name === 'FLAC' ? true : false;
$trackFile->format = $name;
$trackFile->status = TrackFile::STATUS_PROCESSING_PENDING;
if (in_array($name, Track::$CacheableFormats) && $trackFile->is_master == false) {
$trackFile->is_cacheable = true;
} else {
$trackFile->is_cacheable = false;
}
$this->track->trackFiles()->save($trackFile);
// 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;
}
try {
foreach ($trackFiles as $trackFile) {
$this->dispatch(new EncodeTrackFile($trackFile, false, true, $this->autoPublish));
}
} catch (InvalidEncodeOptionsException $e) {
$this->track->delete();
return CommandResponse::fail(['track' => [$e->getMessage()]]);
}
} catch (\Exception $e) {
$this->track->delete();
throw $e;
}
return CommandResponse::succeed([
'id' => $this->track->id,
'name' => $this->track->name,
'title' => $this->track->title,
'slug' => $this->track->slug,
'autoPublish' => $this->autoPublish,
]);
}
/**
* @param FFmpegMovie|string $file object or full path of the file we're checking
* @return bool whether the given file is lossless
*/
private function isLosslessFile($file) {
if (is_string($file)) {
$file = AudioCache::get($file);
}
return Str::startsWith($file->getAudioCodec(), static::$_losslessFormats);
}
/**
* @param string $format
* @return TrackFile|null
*/
private function trackFileExists(string $format) {
return $this->track->trackFiles()->where('format', $format)->first();
}
}

View file

@ -26,12 +26,9 @@ use getID3;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Input;
use Poniverse\Ponyfm\Models\Album;
use Poniverse\Ponyfm\Exceptions\InvalidEncodeOptionsException;
use Poniverse\Ponyfm\Models\Genre;
use Poniverse\Ponyfm\Models\Image;
use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
use Poniverse\Ponyfm\Models\Track;
use Poniverse\Ponyfm\Models\TrackFile;
use AudioCache;
use File;
use Illuminate\Support\Str;
@ -39,24 +36,26 @@ use Poniverse\Ponyfm\Models\TrackType;
use Poniverse\Ponyfm\Models\User;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Validator;
class UploadTrackCommand extends CommandBase
{
use DispatchesJobs;
private $_allowLossy;
private $_allowShortTrack;
private $_customTrackSource;
private $_autoPublishByDefault;
private $_losslessFormats = [
'flac',
'pcm',
'adpcm',
];
public function __construct($allowLossy = false, $allowShortTrack = false, $customTrackSource = null, $autoPublishByDefault = false)
/**
* UploadTrackCommand constructor.
*
* @param bool $allowLossy
* @param bool $allowShortTrack allow tracks shorter than 30 seconds
* @param string|null $customTrackSource value to set in the track's "source" field; if left blank, "direct_upload" is used
* @param bool $autoPublishByDefault
*/
public function __construct(bool $allowLossy = false, bool $allowShortTrack = false, string $customTrackSource = null, bool $autoPublishByDefault = false)
{
$this->_allowLossy = $allowLossy;
$this->_allowShortTrack = $allowShortTrack;
@ -137,7 +136,6 @@ class UploadTrackCommand extends CommandBase
return CommandResponse::fail($validator);
}
// Process optional track fields
$autoPublish = (bool) ($input['auto_publish'] ?? $this->_autoPublishByDefault);
@ -184,91 +182,8 @@ class UploadTrackCommand extends CommandBase
$track->save();
try {
$source = $trackFile->getPathname();
// Lossy uploads need to be identified and set as the master file
// without being re-encoded.
$audioObject = AudioCache::get($source);
$isLossyUpload = !Str::startsWith($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.');
$track->delete();
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());
}
$trackFiles = [];
foreach (Track::$Formats as $name => $format) {
// 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;
}
$trackFile = new TrackFile();
$trackFile->is_master = $name === 'FLAC' ? true : false;
$trackFile->format = $name;
$trackFile->status = TrackFile::STATUS_PROCESSING_PENDING;
if (in_array($name, Track::$CacheableFormats) && $trackFile->is_master == false) {
$trackFile->is_cacheable = true;
} else {
$trackFile->is_cacheable = false;
}
$track->trackFiles()->save($trackFile);
// 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;
}
try {
foreach($trackFiles as $trackFile) {
$this->dispatch(new EncodeTrackFile($trackFile, false, true, $autoPublish));
}
} catch (InvalidEncodeOptionsException $e) {
$track->delete();
return CommandResponse::fail(['track' => [$e->getMessage()]]);
}
} catch (\Exception $e) {
$track->delete();
throw $e;
}
return CommandResponse::succeed([
'id' => $track->id,
'name' => $track->name,
'title' => $track->title,
'slug' => $track->slug,
'autoPublish' => $autoPublish,
]);
$generateTrackFiles = new GenerateTrackFilesCommand($track, $trackFile, $autoPublish);
return $generateTrackFiles->execute();
}
/**

View file

@ -22,6 +22,8 @@ namespace Poniverse\Ponyfm\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Poniverse\Ponyfm\Commands\GenerateTrackFilesCommand;
use Poniverse\Ponyfm\Commands\UploadTrackCommand;
use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
use Poniverse\Ponyfm\Models\Track;
@ -66,9 +68,15 @@ class RebuildTrack extends Command
$track = Track::with('trackFiles')->withTrashed()->find((int) $this->argument('trackId'));
if($this->option('upload')) {
foreach($track->trackFiles as $trackFile) {
$this->info("Re-encoding this track's {$trackFile->format} file...");
$this->dispatch(new EncodeTrackFile($trackFile, false, true, false));
$this->info("Attempting to finish this track's upload...");
$sourceFile = new \SplFileInfo($track->getTemporarySourceFile());
$generateTrackFiles = new GenerateTrackFilesCommand($track, $sourceFile, false);
$result = $generateTrackFiles->execute();
// The GenerateTrackFiles command will re-encode all TrackFiles.
if ($result->didFail()) {
$this->error("Something went wrong!");
$this->error(json_encode($result->getMessages(), JSON_PRETTY_PRINT));
}
} else {

View file

@ -87,7 +87,7 @@ class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue
{
DB::reconnect();
// Sanity-check: was this file just generated, or is it already being processed?
// Sanity check: was this file just generated, or is it already being processed?
if ($this->trackFile->status === TrackFile::STATUS_PROCESSING) {
Log::warning('Track file #'.$this->trackFile->id.' (track #'.$this->trackFile->track_id.') is already being processed!');
return;

View file

@ -682,7 +682,7 @@ class Track extends Model implements Searchable
*
* @return string
*/
public function getTemporarySourceFile() {
public function getTemporarySourceFile():string {
return Config::get('ponyfm.files_directory').'/queued-tracks/'.$this->id;
}