2015-10-27 18:20:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
2018-04-21 06:25:36 +02:00
|
|
|
* Copyright (C) 2015 Feld0
|
2015-10-30 16:29:18 +01:00
|
|
|
* Copyright (C) 2015 Kelvin Zhang
|
2015-10-27 18:20:43 +01:00
|
|
|
*
|
|
|
|
* 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\Jobs;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-08-24 13:48:02 +02:00
|
|
|
use Config;
|
2015-12-26 12:40:47 +01:00
|
|
|
use DB;
|
2015-12-18 09:56:13 +01:00
|
|
|
use File;
|
2015-10-27 18:20:43 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2016-08-24 13:48:02 +02:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Log;
|
|
|
|
use Poniverse\Ponyfm\Exceptions\InvalidEncodeOptionsException;
|
2016-01-01 01:12:30 +01:00
|
|
|
use Poniverse\Ponyfm\Models\Track;
|
|
|
|
use Poniverse\Ponyfm\Models\TrackFile;
|
2015-10-27 18:20:43 +01:00
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
class EncodeTrackFile extends Job implements ShouldQueue
|
2015-10-27 18:20:43 +01:00
|
|
|
{
|
|
|
|
use InteractsWithQueue, SerializesModels;
|
|
|
|
/**
|
|
|
|
* @var TrackFile
|
|
|
|
*/
|
2016-01-04 23:55:20 +01:00
|
|
|
protected $trackFile;
|
2015-10-27 18:20:43 +01:00
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
2016-01-04 23:55:20 +01:00
|
|
|
protected $isExpirable;
|
2016-08-24 13:48:02 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $autoPublishWhenComplete;
|
2015-12-18 09:56:13 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-01-04 23:55:20 +01:00
|
|
|
protected $isForUpload;
|
2015-12-26 12:40:47 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-08-24 13:48:02 +02:00
|
|
|
protected $isReplacingTrack;
|
2015-10-27 18:20:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
* @param TrackFile $trackFile
|
2015-12-18 09:56:13 +01:00
|
|
|
* @param bool $isExpirable
|
2015-12-26 12:40:47 +01:00
|
|
|
* @param bool $autoPublish
|
2016-08-24 13:48:02 +02:00
|
|
|
* @param bool $isForUpload indicates whether this encode job is for an upload
|
|
|
|
* @param bool $isReplacingTrack
|
2015-10-27 18:20:43 +01:00
|
|
|
*/
|
2016-08-24 13:48:02 +02:00
|
|
|
public function __construct(TrackFile $trackFile, $isExpirable, $autoPublish = false, $isForUpload = false, $isReplacingTrack = false)
|
2015-10-27 18:20:43 +01:00
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
if ((!$isForUpload && $trackFile->is_master) ||
|
2015-12-18 09:56:13 +01:00
|
|
|
($isForUpload && $trackFile->is_master && !$trackFile->getFormat()['is_lossless'])
|
|
|
|
) {
|
|
|
|
throw new InvalidEncodeOptionsException("Master files cannot be encoded unless we're generating a lossless master file during the upload process.");
|
|
|
|
}
|
|
|
|
|
2015-10-27 18:20:43 +01:00
|
|
|
$this->trackFile = $trackFile;
|
|
|
|
$this->isExpirable = $isExpirable;
|
2015-12-26 12:40:47 +01:00
|
|
|
$this->autoPublishWhenComplete = $autoPublish;
|
2016-08-24 13:48:02 +02:00
|
|
|
$this->isForUpload = $isForUpload;
|
|
|
|
$this->isReplacingTrack = $isReplacingTrack;
|
2015-10-27 18:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2016-02-22 05:47:52 +01:00
|
|
|
$this->beforeHandle();
|
2016-01-18 03:01:42 +01:00
|
|
|
|
2016-02-15 14:06:06 +01:00
|
|
|
// Sanity check: was this file just generated, or is it already being processed?
|
2016-01-04 17:42:30 +01:00
|
|
|
if ($this->trackFile->status === TrackFile::STATUS_PROCESSING) {
|
|
|
|
Log::warning('Track file #'.$this->trackFile->id.' (track #'.$this->trackFile->track_id.') is already being processed!');
|
|
|
|
return;
|
2016-08-24 13:48:02 +02:00
|
|
|
} elseif (!$this->trackFile->is_expired && File::exists($this->trackFile->getFile())) {
|
2016-01-04 17:42:30 +01:00
|
|
|
Log::warning('Track file #'.$this->trackFile->id.' (track #'.$this->trackFile->track_id.') is still valid! No need to re-encode it.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-27 18:20:43 +01:00
|
|
|
// Start the job
|
2015-12-18 09:56:13 +01:00
|
|
|
$this->trackFile->status = TrackFile::STATUS_PROCESSING;
|
2016-01-04 17:42:30 +01:00
|
|
|
$this->trackFile->save();
|
2015-10-27 18:20:43 +01:00
|
|
|
|
2015-11-01 17:49:28 +01:00
|
|
|
// Use the track's master file as the source
|
2015-12-18 09:56:13 +01:00
|
|
|
if ($this->isForUpload) {
|
2016-08-24 13:48:02 +02:00
|
|
|
$source = $this->trackFile->track->getTemporarySourceFileForVersion($this->trackFile->version);
|
2015-12-18 09:56:13 +01:00
|
|
|
} else {
|
|
|
|
$source = TrackFile::where('track_id', $this->trackFile->track_id)
|
|
|
|
->where('is_master', true)
|
2016-08-24 13:48:02 +02:00
|
|
|
->where('version', $this->trackFile->version)
|
2015-12-18 09:56:13 +01:00
|
|
|
->first()
|
|
|
|
->getFile();
|
|
|
|
}
|
2015-10-27 18:20:43 +01:00
|
|
|
|
|
|
|
// Assign the target
|
2015-10-27 18:35:38 +01:00
|
|
|
$this->trackFile->track->ensureDirectoryExists();
|
2015-11-01 17:49:28 +01:00
|
|
|
$target = $this->trackFile->getFile();
|
2015-10-27 18:20:43 +01:00
|
|
|
|
|
|
|
// Prepare the command
|
|
|
|
$format = Track::$Formats[$this->trackFile->format];
|
|
|
|
$command = $format['command'];
|
|
|
|
$command = str_replace('{$source}', '"' . $source . '"', $command);
|
|
|
|
$command = str_replace('{$target}', '"' . $target . '"', $command);
|
|
|
|
|
|
|
|
Log::info('Encoding track file ' . $this->trackFile->id . ' into ' . $target);
|
|
|
|
|
|
|
|
// Start a synchronous process to encode the file
|
|
|
|
$process = new Process($command);
|
|
|
|
try {
|
|
|
|
$process->mustRun();
|
|
|
|
} catch (ProcessFailedException $e) {
|
|
|
|
Log::error('An exception occured in the encoding process for track file ' . $this->trackFile->id . ' - ' . $e->getMessage());
|
2016-01-05 00:22:15 +01:00
|
|
|
Log::info($process->getOutput());
|
2015-10-27 18:20:43 +01:00
|
|
|
// Ensure queue fails
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the tags of the track
|
2015-11-01 17:49:28 +01:00
|
|
|
$this->trackFile->track->updateTags($this->trackFile->format);
|
2015-10-27 18:20:43 +01:00
|
|
|
|
|
|
|
// Insert the expiration time for cached tracks
|
2016-01-04 23:55:20 +01:00
|
|
|
if ($this->isExpirable && $this->trackFile->is_cacheable) {
|
2015-11-01 17:49:28 +01:00
|
|
|
$this->trackFile->expires_at = Carbon::now()->addMinutes(Config::get('ponyfm.track_file_cache_duration'));
|
2016-01-04 17:42:30 +01:00
|
|
|
$this->trackFile->save();
|
2015-10-27 18:20:43 +01:00
|
|
|
}
|
|
|
|
|
2015-10-29 17:10:55 +01:00
|
|
|
// Update file size
|
|
|
|
$this->trackFile->updateFilesize();
|
|
|
|
|
2015-11-01 17:49:28 +01:00
|
|
|
// Complete the job
|
2015-12-18 09:56:13 +01:00
|
|
|
$this->trackFile->status = TrackFile::STATUS_NOT_BEING_PROCESSED;
|
2016-01-04 17:42:30 +01:00
|
|
|
$this->trackFile->save();
|
2015-12-18 09:56:13 +01:00
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
if ($this->isForUpload || $this->isReplacingTrack) {
|
2015-12-18 09:56:13 +01:00
|
|
|
if (!$this->trackFile->is_master && $this->trackFile->is_cacheable) {
|
|
|
|
File::delete($this->trackFile->getFile());
|
|
|
|
}
|
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
// This was the final TrackFile for this track!
|
2015-12-18 09:56:13 +01:00
|
|
|
if ($this->trackFile->track->status === Track::STATUS_COMPLETE) {
|
2015-12-26 12:40:47 +01:00
|
|
|
if ($this->autoPublishWhenComplete) {
|
|
|
|
$this->trackFile->track->published_at = Carbon::now();
|
|
|
|
DB::table('tracks')->whereUserId($this->trackFile->track->user_id)->update(['is_latest' => false]);
|
|
|
|
|
|
|
|
$this->trackFile->track->is_latest = true;
|
|
|
|
$this->trackFile->track->save();
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
if ($this->isReplacingTrack) {
|
|
|
|
$oldVersion = $this->trackFile->track->current_version;
|
|
|
|
|
|
|
|
// Update the version of the track being uploaded
|
|
|
|
$this->trackFile->track->duration = \AudioCache::get($source)->getDuration();
|
|
|
|
$this->trackFile->track->current_version = $this->trackFile->version;
|
|
|
|
$this->trackFile->track->version_upload_status = Track::STATUS_COMPLETE;
|
|
|
|
$this->trackFile->track->update();
|
|
|
|
|
|
|
|
// Delete the non-master files for the old version
|
|
|
|
if ($oldVersion !== $this->trackFile->version) {
|
|
|
|
$trackFilesToDelete = $this->trackFile->track->trackFilesForVersion($oldVersion)->where('is_master', false)->get();
|
|
|
|
foreach ($trackFilesToDelete as $trackFileToDelete) {
|
|
|
|
if (File::exists($trackFileToDelete->getFile())) {
|
|
|
|
File::delete($trackFileToDelete->getFile());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isForUpload) {
|
|
|
|
File::delete($this->trackFile->track->getTemporarySourceFileForVersion($this->trackFile->version));
|
|
|
|
}
|
2015-12-18 09:56:13 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-27 18:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a job failure.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function failed()
|
|
|
|
{
|
2015-12-18 09:56:13 +01:00
|
|
|
$this->trackFile->status = TrackFile::STATUS_PROCESSING_ERROR;
|
2015-11-01 17:49:28 +01:00
|
|
|
$this->trackFile->expires_at = null;
|
2016-01-04 17:42:30 +01:00
|
|
|
$this->trackFile->save();
|
2016-08-24 13:48:02 +02:00
|
|
|
|
|
|
|
if ($this->isReplacingTrack) {
|
|
|
|
// If a new version is being uploaded to replace a file, yet the upload fails,
|
|
|
|
// all track files for that version should be deleted as it would other clutter the version
|
|
|
|
if ($this->isForUpload) {
|
|
|
|
$trackFiles = $this->trackFile->track->trackFilesForVersion($this->trackFile->version)->get();
|
|
|
|
foreach ($trackFiles as $trackFile) {
|
|
|
|
if (File::exists($trackFile->getFile())) {
|
|
|
|
File::delete($trackFile->getFile());
|
|
|
|
}
|
|
|
|
$trackFile->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->trackFile->track->version_upload_status = Track::STATUS_ERROR;
|
|
|
|
$this->trackFile->track->update();
|
|
|
|
}
|
2015-10-27 18:20:43 +01:00
|
|
|
}
|
|
|
|
}
|