/**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $songIndex = $this->_playlist->tracks()->count() + 1;
     $this->_playlist->tracks()->attach($this->_track, ['position' => $songIndex]);
     Playlist::whereId($this->_playlist->id)->update(['track_count' => DB::raw('(SELECT COUNT(id) FROM playlist_track WHERE playlist_id = ' . $this->_playlist->id . ')')]);
     return CommandResponse::succeed(['message' => 'Track added!']);
 }
예제 #2
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $rules = ['title' => 'required|min:3|max:50', 'cover' => 'image|mimes:png|min_width:350|min_height:350', 'cover_id' => 'exists:images,id', 'track_ids' => 'exists:tracks,id'];
     $validator = Validator::make($this->_input, $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $album = new Album();
     $album->user_id = Auth::user()->id;
     $album->title = $this->_input['title'];
     $album->description = $this->_input['description'];
     if (isset($this->_input['cover_id'])) {
         $album->cover_id = $this->_input['cover_id'];
     } else {
         if (isset($this->_input['cover'])) {
             $cover = $this->_input['cover'];
             $album->cover_id = Image::upload($cover, Auth::user())->id;
         } else {
             if (isset($this->_input['remove_cover']) && $this->_input['remove_cover'] == 'true') {
                 $album->cover_id = null;
             }
         }
     }
     $trackIds = explode(',', $this->_input['track_ids']);
     $album->save();
     $album->syncTrackIds($trackIds);
     return CommandResponse::succeed(['id' => $album->id]);
 }
예제 #3
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $typeId = $this->_resourceType . '_id';
     $existing = Favourite::where($typeId, '=', $this->_resourceId)->whereUserId(Auth::user()->id)->first();
     $isFavourited = false;
     if ($existing) {
         $existing->delete();
     } else {
         $fav = new Favourite();
         $fav->{$typeId} = $this->_resourceId;
         $fav->user_id = Auth::user()->id;
         $fav->created_at = time();
         $fav->save();
         $isFavourited = true;
     }
     $resourceUser = ResourceUser::get(Auth::user()->id, $this->_resourceType, $this->_resourceId);
     $resourceUser->is_favourited = $isFavourited;
     $resourceUser->save();
     $resourceTable = $this->_resourceType . 's';
     // We do this to prevent a race condition. Sure I could simply increment the count columns and re-save back to the db
     // but that would require an additional SELECT and the operation would be non-atomic. If two log items are created
     // for the same resource at the same time, the cached values will still be correct with this method.
     DB::table($resourceTable)->whereId($this->_resourceId)->update(['favourite_count' => DB::raw('(
                 SELECT
                     COUNT(id)
                 FROM
                     favourites
                 WHERE ' . $typeId . ' = ' . $this->_resourceId . ')')]);
     return CommandResponse::succeed(['is_favourited' => $isFavourited]);
 }
예제 #4
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     foreach ($this->_playlist->pins as $pin) {
         $pin->delete();
     }
     $this->_playlist->delete();
     return CommandResponse::succeed();
 }
예제 #5
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $slug = Str::slug($this->_genreName);
     $rules = ['name' => 'required|unique:genres,name,NULL,id,deleted_at,NULL|max:50', 'slug' => 'required|unique:genres,slug,NULL,id,deleted_at,NULL'];
     $validator = Validator::make(['name' => $this->_genreName, 'slug' => $slug], $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     Genre::create(['name' => $this->_genreName, 'slug' => $slug]);
     return CommandResponse::succeed(['message' => 'Genre created!']);
 }
예제 #6
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     foreach ($this->_album->tracks as $track) {
         $track->album_id = null;
         $track->track_number = null;
         $track->updateTags();
         $track->save();
     }
     $this->_album->delete();
     return CommandResponse::succeed();
 }
예제 #7
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $rules = ['genre_to_delete' => 'required', 'destination_genre' => 'required'];
     // The validation will fail if the genres don't exist
     // because they'll be null.
     $validator = Validator::make(['genre_to_delete' => $this->_genreToDelete, 'destination_genre' => $this->_destinationGenre], $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $this->dispatch(new DeleteGenre($this->_genreToDelete, $this->_destinationGenre));
     return CommandResponse::succeed(['message' => 'Genre deleted!']);
 }
예제 #8
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     if ($this->_track->album_id != null) {
         $album = $this->_track->album;
         $this->_track->album_id = null;
         $this->_track->track_number = null;
         $this->_track->delete();
         $album->updateTrackNumbers();
     } else {
         $this->_track->delete();
     }
     return CommandResponse::succeed();
 }
예제 #9
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $slug = Str::slug($this->_newName);
     $rules = ['name' => 'required|unique:genres,name,' . $this->_genre->id . ',id,deleted_at,NULL|max:50', 'slug' => 'required|unique:genres,slug,' . $this->_genre->id . ',id,deleted_at,NULL'];
     $validator = Validator::make(['name' => $this->_newName, 'slug' => $slug], $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $this->_genre->name = $this->_newName;
     $this->_genre->slug = $slug;
     $this->_genre->save();
     $this->dispatch(new UpdateTagsForRenamedGenre($this->_genre));
     return CommandResponse::succeed(['message' => 'Genre renamed!']);
 }
예제 #10
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $rules = ['title' => 'required|min:3|max:50', 'is_public' => 'required', 'is_pinned' => 'required'];
     $validator = Validator::make($this->_input, $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $playlist = new Playlist();
     $playlist->user_id = Auth::user()->id;
     $playlist->title = $this->_input['title'];
     $playlist->description = $this->_input['description'];
     $playlist->is_public = $this->_input['is_public'] == 'true';
     $playlist->save();
     if ($this->_input['is_pinned'] == 'true') {
         $playlist->pin(Auth::user()->id);
     }
     return CommandResponse::succeed(['id' => $playlist->id, 'title' => $playlist->title, 'slug' => $playlist->slug, 'created_at' => $playlist->created_at, 'description' => $playlist->description, 'url' => $playlist->url, 'is_pinned' => $this->_input['is_pinned'] == 'true', 'is_public' => $this->_input['is_public'] == 'true']);
 }
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $user = Auth::user();
     $rules = ['display_name' => 'required|min:3|max:26', 'bio' => 'textarea_length:250'];
     if ($this->_input['sync_names'] == 'true') {
         $this->_input['display_name'] = $user->username;
     }
     if ($this->_input['uses_gravatar'] == 'true') {
         $rules['gravatar'] = 'email';
     } else {
         $rules['avatar'] = 'image|mimes:png|min_width:350|min_height:350';
         $rules['avatar_id'] = 'exists:images,id';
     }
     $validator = Validator::make($this->_input, $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     if ($this->_input['uses_gravatar'] != 'true') {
         if ($user->avatar_id == null && !isset($this->_input['avatar']) && !isset($this->_input['avatar_id'])) {
             $validator->messages()->add('avatar', 'You must upload or select an avatar if you are not using gravatar!');
             return CommandResponse::fail($validator);
         }
     }
     $user->bio = $this->_input['bio'];
     $user->display_name = $this->_input['display_name'];
     $user->sync_names = $this->_input['sync_names'] == 'true';
     $user->can_see_explicit_content = $this->_input['can_see_explicit_content'] == 'true';
     $user->uses_gravatar = $this->_input['uses_gravatar'] == 'true';
     if ($user->uses_gravatar) {
         $user->avatar_id = null;
         $user->gravatar = $this->_input['gravatar'];
     } else {
         if (isset($this->_input['avatar_id'])) {
             $user->avatar_id = $this->_input['avatar_id'];
         } else {
             if (isset($this->_input['avatar'])) {
                 $user->avatar_id = Image::upload($this->_input['avatar'], $user)->id;
             }
         }
     }
     $user->save();
     return CommandResponse::succeed();
 }
예제 #12
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $typeId = $this->_resourceType . '_id';
     $existing = Follower::where($typeId, '=', $this->_resourceId)->whereUserId(Auth::user()->id)->first();
     $isFollowed = false;
     if ($existing) {
         $existing->delete();
     } else {
         $follow = new Follower();
         $follow->{$typeId} = $this->_resourceId;
         $follow->user_id = Auth::user()->id;
         $follow->created_at = time();
         $follow->save();
         $isFollowed = true;
     }
     $resourceUser = ResourceUser::get(Auth::user()->id, $this->_resourceType, $this->_resourceId);
     $resourceUser->is_followed = $isFollowed;
     $resourceUser->save();
     return CommandResponse::succeed(['is_followed' => $isFollowed]);
 }
예제 #13
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $rules = ['title' => 'required|min:3|max:50', 'is_public' => 'required', 'is_pinned' => 'required'];
     $validator = Validator::make($this->_input, $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $this->_playlist->title = $this->_input['title'];
     $this->_playlist->description = $this->_input['description'];
     $this->_playlist->is_public = $this->_input['is_public'] == 'true';
     $this->_playlist->save();
     $pin = PinnedPlaylist::whereUserId(Auth::user()->id)->wherePlaylistId($this->_playlistId)->first();
     if ($pin && $this->_input['is_pinned'] != 'true') {
         $pin->delete();
     } else {
         if (!$pin && $this->_input['is_pinned'] == 'true') {
             $this->_playlist->pin(Auth::user()->id);
         }
     }
     return CommandResponse::succeed(['id' => $this->_playlist->id, 'title' => $this->_playlist->title, 'slug' => $this->_playlist->slug, 'created_at' => $this->_playlist->created_at, 'description' => $this->_playlist->description, 'url' => $this->_playlist->url, 'is_pinned' => $this->_input['is_pinned'] == 'true', 'is_public' => $this->_input['is_public'] == 'true']);
 }
예제 #14
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $rules = ['content' => 'required', 'track_id' => 'exists:tracks,id', 'albums_id' => 'exists:albums,id', 'playlist_id' => 'exists:playlists,id', 'profile_id' => 'exists:users,id'];
     $validator = Validator::make($this->_input, $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $comment = new Comment();
     $comment->user_id = Auth::user()->id;
     $comment->content = $this->_input['content'];
     if ($this->_type == 'track') {
         $column = 'track_id';
     } else {
         if ($this->_type == 'user') {
             $column = 'profile_id';
         } else {
             if ($this->_type == 'album') {
                 $column = 'album_id';
             } else {
                 if ($this->_type == 'playlist') {
                     $column = 'playlist_id';
                 } else {
                     App::abort(500);
                 }
             }
         }
     }
     $comment->{$column} = $this->_id;
     $comment->save();
     // Recount the track's comments, if this is a track comment
     if ($this->_type === 'track') {
         $entity = Track::find($this->_id);
     } elseif ($this->_type === 'album') {
         $entity = Album::find($this->_id);
     } elseif ($this->_type === 'playlist') {
         $entity = Playlist::find($this->_id);
     } elseif ($this->_type === 'user') {
         $entity = User::find($this->_id);
     } else {
         App::abort(400, 'This comment is being added to an invalid entity!');
     }
     $entity->comment_count = Comment::where($column, $this->_id)->count();
     $entity->save();
     return CommandResponse::succeed(Comment::mapPublic($comment));
 }
예제 #15
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $isVocal = isset($this->_input['is_vocal']) && $this->_input['is_vocal'] == 'true' ? true : false;
     $rules = ['title' => 'required|min:3|max:80', 'released_at' => 'before:' . date('Y-m-d', time() + 86400 * 2) . (isset($this->_input['released_at']) && $this->_input['released_at'] != "" ? '|date' : ''), 'license_id' => 'required|exists:licenses,id', 'genre_id' => 'required|exists:genres,id', 'cover' => 'image|mimes:png,jpeg|min_width:350|min_height:350', 'track_type_id' => 'required|exists:track_types,id|not_in:' . TrackType::UNCLASSIFIED_TRACK, 'songs' => 'required_when:track_type,2|exists:songs,id', 'cover_id' => 'exists:images,id', 'album_id' => 'exists:albums,id'];
     if ($isVocal) {
         $rules['lyrics'] = 'required';
     }
     if (isset($this->_input['track_type_id']) && $this->_input['track_type_id'] == 2) {
         $rules['show_song_ids'] = 'required|exists:show_songs,id';
     }
     $validator = \Validator::make($this->_input, $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $track = $this->_track;
     $track->title = $this->_input['title'];
     $track->released_at = isset($this->_input['released_at']) && $this->_input['released_at'] != "" ? strtotime($this->_input['released_at']) : null;
     $track->description = isset($this->_input['description']) ? $this->_input['description'] : '';
     $track->lyrics = isset($this->_input['lyrics']) ? $this->_input['lyrics'] : '';
     $track->license_id = $this->_input['license_id'];
     $track->genre_id = $this->_input['genre_id'];
     $track->track_type_id = $this->_input['track_type_id'];
     $track->is_explicit = $this->_input['is_explicit'] == 'true';
     $track->is_downloadable = $this->_input['is_downloadable'] == 'true';
     $track->is_listed = $this->_input['is_listed'] == 'true';
     $track->is_vocal = $isVocal;
     if (isset($this->_input['album_id']) && strlen(trim($this->_input['album_id']))) {
         if ($track->album_id != null && $track->album_id != $this->_input['album_id']) {
             $this->removeTrackFromAlbum($track);
         }
         if ($track->album_id != $this->_input['album_id']) {
             $album = Album::find($this->_input['album_id']);
             $track->track_number = $album->tracks()->count() + 1;
             $track->album_id = $this->_input['album_id'];
             Album::whereId($album->id)->update(['track_count' => DB::raw('(SELECT COUNT(id) FROM tracks WHERE album_id = ' . $album->id . ')')]);
         }
     } else {
         if ($track->album_id != null) {
             $this->removeTrackFromAlbum($track);
         }
         $track->track_number = null;
         $track->album_id = null;
     }
     if ($track->track_type_id == TrackType::OFFICIAL_TRACK_REMIX) {
         $track->showSongs()->sync(explode(',', $this->_input['show_song_ids']));
     } else {
         $track->showSongs()->sync([]);
     }
     if ($track->published_at == null) {
         $track->published_at = new \DateTime();
         DB::table('tracks')->whereUserId($track->user_id)->update(['is_latest' => false]);
         $track->is_latest = true;
     }
     if (isset($this->_input['cover_id'])) {
         $track->cover_id = $this->_input['cover_id'];
     } else {
         if (isset($this->_input['cover'])) {
             $cover = $this->_input['cover'];
             $track->cover_id = Image::upload($cover, Auth::user())->id;
         } else {
             if ($this->_input['remove_cover'] == 'true') {
                 $track->cover_id = null;
             }
         }
     }
     $track->updateTags();
     $track->save();
     User::whereId($this->_track->user_id)->update(['track_count' => DB::raw('(SELECT COUNT(id) FROM tracks WHERE deleted_at IS NULL AND published_at IS NOT NULL AND user_id = ' . $this->_track->user_id . ')')]);
     return CommandResponse::succeed(['real_cover_url' => $track->getCoverUrl(Image::NORMAL)]);
 }
예제 #16
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]);
 }