/** * Validate the duration of the audio file, in seconds. * * @param string $attribute * @param array $value * @param array $parameters * @return bool */ public function validateMinDuration($attribute, $value, $parameters) { // attribute is the file field // value is the file array itself // parameters is an array containing one value: the minimum duration $file = AudioCache::get($value->getPathname()); return $file->getDuration() >= (double) $parameters[0]; }
/** * @throws \Exception * @return CommandResponse */ public function execute() { $user = \Auth::user(); $trackFile = \Input::file('track', null); if (null === $trackFile) { return CommandResponse::fail(['track' => ['You must upload an audio file!']]); } $audio = \AudioCache::get($trackFile->getPathname()); list($parsedTags, $rawTags) = $this->parseOriginalTags($trackFile, $user, $audio->getAudioCodec()); $track = new Track(); $track->user_id = $user->id; $track->title = Input::get('title', $parsedTags['title']); $track->duration = $audio->getDuration(); $track->save(); $track->ensureDirectoryExists(); if (!is_dir(Config::get('ponyfm.files_directory') . '/queued-tracks')) { mkdir(Config::get('ponyfm.files_directory') . '/queued-tracks', 0755, true); } $trackFile = $trackFile->move(Config::get('ponyfm.files_directory') . '/queued-tracks', $track->id); $input = Input::all(); $input['track'] = $trackFile; $validator = \Validator::make($input, ['track' => 'required|' . ($this->_allowLossy ? '' : 'audio_format:' . implode(',', $this->_losslessFormats) . '|') . ($this->_allowShortTrack ? '' : 'min_duration:30|') . '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', 'cover' => 'image|mimes:png,jpeg|min_width:350|min_height:350', 'metadata' => 'json']); if ($validator->fails()) { $track->delete(); return CommandResponse::fail($validator); } // Process optional track fields $autoPublish = (bool) ($input['auto_publish'] ?? $this->_autoPublishByDefault); if (Input::hasFile('cover')) { $track->cover_id = Image::upload(Input::file('cover'), $track->user_id)->id; } else { $track->cover_id = $parsedTags['cover_id']; } $track->title = $input['title'] ?? $parsedTags['title'] ?? $track->title; $track->track_type_id = $input['track_type_id'] ?? TrackType::UNCLASSIFIED_TRACK; $track->genre_id = isset($input['genre']) ? $this->getGenreId($input['genre']) : $parsedTags['genre_id']; $track->album_id = isset($input['album']) ? $this->getAlbumId($user->id, $input['album']) : $parsedTags['album_id']; if ($track->album_id === null) { $track->track_number = null; } else { $track->track_number = $input['track_number'] ?? $parsedTags['track_number']; } $track->released_at = isset($input['released_at']) ? Carbon::createFromFormat(Carbon::ISO8601, $input['released_at']) : $parsedTags['release_date']; $track->description = $input['description'] ?? $parsedTags['comments']; $track->lyrics = $input['lyrics'] ?? $parsedTags['lyrics']; $track->is_vocal = $input['is_vocal'] ?? $parsedTags['is_vocal']; $track->is_explicit = $input['is_explicit'] ?? false; $track->is_downloadable = $input['is_downloadable'] ?? true; $track->is_listed = $input['is_listed'] ?? true; $track->source = $this->_customTrackSource ?? 'direct_upload'; // 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->original_tags = ['parsed_tags' => $parsedTags, 'raw_tags' => $rawTags]; $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 = !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.'); $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]); }