Update UploadTrackCommand with selective uploading using Process

This commit is contained in:
Kelvin Zhang 2015-10-27 16:21:03 +00:00
parent 5380d0b45a
commit 0970734648

View file

@ -26,6 +26,8 @@ use AudioCache;
use File; use File;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class UploadTrackCommand extends CommandBase class UploadTrackCommand extends CommandBase
{ {
@ -94,8 +96,6 @@ class UploadTrackCommand extends CommandBase
$source = $trackFile->getPathname(); $source = $trackFile->getPathname();
$index = 0; $index = 0;
$processes = [];
// Lossy uploads need to be identified and set as the master file // Lossy uploads need to be identified and set as the master file
// without being re-encoded. // without being re-encoded.
$audioObject = AudioCache::get($source); $audioObject = AudioCache::get($source);
@ -136,29 +136,31 @@ class UploadTrackCommand extends CommandBase
$trackFile = new TrackFile(); $trackFile = new TrackFile();
$trackFile->is_master = $name === 'FLAC' ? true : false; $trackFile->is_master = $name === 'FLAC' ? true : false;
$trackFile->format = $name; $trackFile->format = $name;
if (array_key_exists($name, Track::$CacheableFormats) && $trackFile->is_master == false) {
$trackFile->is_cacheable = true;
} else {
$trackFile->is_cacheable = false;
}
$track->trackFiles()->save($trackFile); $track->trackFiles()->save($trackFile);
$target = $destination . '/' . $trackFile->getFilename(); //$track->getFilenameFor($name); if ($trackFile->is_cacheable == false) {
$target = $destination . '/' . $trackFile->getFilename();
$command = $format['command']; $command = $format['command'];
$command = str_replace('{$source}', '"' . $source . '"', $command); $command = str_replace('{$source}', '"' . $source . '"', $command);
$command = str_replace('{$target}', '"' . $target . '"', $command); $command = str_replace('{$target}', '"' . $target . '"', $command);
Log::info('Encoding ' . $track->id . ' into ' . $target); Log::info('Encoding ' . $track->id . ' into ' . $target);
$this->notify('Encoding ' . $name, $index / count(Track::$Formats) * 100); $this->notify('Encoding ' . $name, $index / count(Track::$Formats) * 100);
$pipes = []; $process = new Process($command);
$proc = proc_open($command, [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'a']], $pipes); $process->mustRun();
$processes[] = $proc; }
}
foreach ($processes as $proc) {
proc_close($proc);
} }
$track->updateTags(); $track->updateTags();
} catch (\Exception $e) { } catch (ProcessFailedException $e) {
$track->delete(); $track->delete();
throw $e; throw $e;
} }